Skip to main content

Conditions ยป Examples

Driver's license oracleโ€‹

This example was developed throughout the entire Conditions lesson.

#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 5500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

int num_crashes;
std::cout << "Please enter the number of crashes you have had: \n";
std::cin >> num_crashes;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases
std::string illegal_reason;

if (num_crashes < 2) {
// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} else {
illegal_reason = "you are not between 18 and 64 years old";
}
} else {
illegal_reason = "you have " + std::to_string(num_crashes) + " accidents";
}

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because " << illegal_reason << "\n";
}
}

Interactive boolean logic calculatorโ€‹

#include <iostream>
#include <iomanip>
#include <string>

int main() {
// Ask for the two boolean inputs
std::cout << "Welcome to the Boolean Logic Calculator!\n"
<< "Please enter two values of either true or false exactly:\n";

bool input_a;
bool input_b;

// Use std::boolalpha to accept input as either "true" or "false"
std::cin >> std::boolalpha >> input_a >> input_b;

// Ask for the operation
std::cout << "\n\nNow enter a logical operation.\n"
<< "Valid options are AND, NAND, OR, NOR, XOR, XNOR (exactly):\n";

std::string op;
std::cin >> op;

// Now compute the result based on op
bool result;

if (op == "AND") {
result = input_a and input_b;
} else if (op == "NAND") {
result = not (input_a and input_b);
} else if (op == "OR") {
result = input_a or input_b;
} else if (op == "NOR") {
result = not (input_a or input_b);
} else if (op == "XOR") {
result = input_a != input_b; // XOR is equivalent to "not equal" for booleans
} else if (op == "XNOR") {
result = input_a == input_b; // XNOR is equivalent to "equals" for booleans
} else {
std::cout << "Error! Invalid choice. Quitting.";

// Using a return statement here to gracefully end the main function,
// which in turn quits the program.
// The 1 signifies a failure to the operating system.
// You will learn more about returns in the Functions chapter.
return 1;
}

std::cout << std::boolalpha; // Set output to show as true/false for booleans
std::cout << "\n\nThe answer of " << input_a << " " << op << " " << input_b << " is " << result;
}

Computing properties of numbersโ€‹

#include <iostream>

int main() {
int value1;
bool is_value1_even;

std::cout << "Input a number: ";
std::cin >> value1;

// Compute the parity (even vs odd) of the number
if (value1 % 2 == 0) {
is_value1_even = true;

std::cout << "Your number is even!\n";
if (value1 == 0) {
// Many people are confused by the parity of zero
std::cout << "But does zero count as an even number?\n";
}
} else {
is_value1_even = false;
std::cout << "Your number is odd!\n";
}

// Compute the sign of the number
if (value1 > 0) {
std::cout << "Your number is positive!\n";
} else if (value1 < 0) {
std::cout << "Your number is negative!\n";
} else {
std::cout << "Zero is neither positive nor negative.\n";
}

// Ask the user for a second number to compare
int value2;

std::cout << "\nNow, input a number again: ";
std::cin >> value2;

// Compute the relative magnitude of the second number compared to the first
if (value1 != value2) {
std::cout << "You entered a different number... ";

if (value1 > value2) {
std::cout << "And it was smaller than the previous one!\n";
} else {
std::cout << "And it was bigger than the previous one!\n";
}
} else {
std::cout << "You entered the same number!\n";
}

// Now check for the "special property"
// Your numbers are special if they are both even and their difference is divisible by seven
int diff = value1 - value2;
bool is_value2_even= value2 % 2 == 0;

if (is_value1_even && is_value2_even&& (diff % 7 == 0)) {
std::cout << "Your numbers are special!";
}
}

Conditions ยป Examples

Driver's license oracleโ€‹

This example was developed throughout the entire Conditions lesson.

#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 5500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

int num_crashes;
std::cout << "Please enter the number of crashes you have had: \n";
std::cin >> num_crashes;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases
std::string illegal_reason;

if (num_crashes < 2) {
// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} else {
illegal_reason = "you are not between 18 and 64 years old";
}
} else {
illegal_reason = "you have " + std::to_string(num_crashes) + " accidents";
}

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because " << illegal_reason << "\n";
}
}

Interactive boolean logic calculatorโ€‹

#include <iostream>
#include <iomanip>
#include <string>

int main() {
// Ask for the two boolean inputs
std::cout << "Welcome to the Boolean Logic Calculator!\n"
<< "Please enter two values of either true or false exactly:\n";

bool input_a;
bool input_b;

// Use std::boolalpha to accept input as either "true" or "false"
std::cin >> std::boolalpha >> input_a >> input_b;

// Ask for the operation
std::cout << "\n\nNow enter a logical operation.\n"
<< "Valid options are AND, NAND, OR, NOR, XOR, XNOR (exactly):\n";

std::string op;
std::cin >> op;

// Now compute the result based on op
bool result;

if (op == "AND") {
result = input_a and input_b;
} else if (op == "NAND") {
result = not (input_a and input_b);
} else if (op == "OR") {
result = input_a or input_b;
} else if (op == "NOR") {
result = not (input_a or input_b);
} else if (op == "XOR") {
result = input_a != input_b; // XOR is equivalent to "not equal" for booleans
} else if (op == "XNOR") {
result = input_a == input_b; // XNOR is equivalent to "equals" for booleans
} else {
std::cout << "Error! Invalid choice. Quitting.";

// Using a return statement here to gracefully end the main function,
// which in turn quits the program.
// The 1 signifies a failure to the operating system.
// You will learn more about returns in the Functions chapter.
return 1;
}

std::cout << std::boolalpha; // Set output to show as true/false for booleans
std::cout << "\n\nThe answer of " << input_a << " " << op << " " << input_b << " is " << result;
}

Computing properties of numbersโ€‹

#include <iostream>

int main() {
int value1;
bool is_value1_even;

std::cout << "Input a number: ";
std::cin >> value1;

// Compute the parity (even vs odd) of the number
if (value1 % 2 == 0) {
is_value1_even = true;

std::cout << "Your number is even!\n";
if (value1 == 0) {
// Many people are confused by the parity of zero
std::cout << "But does zero count as an even number?\n";
}
} else {
is_value1_even = false;
std::cout << "Your number is odd!\n";
}

// Compute the sign of the number
if (value1 > 0) {
std::cout << "Your number is positive!\n";
} else if (value1 < 0) {
std::cout << "Your number is negative!\n";
} else {
std::cout << "Zero is neither positive nor negative.\n";
}

// Ask the user for a second number to compare
int value2;

std::cout << "\nNow, input a number again: ";
std::cin >> value2;

// Compute the relative magnitude of the second number compared to the first
if (value1 != value2) {
std::cout << "You entered a different number... ";

if (value1 > value2) {
std::cout << "And it was smaller than the previous one!\n";
} else {
std::cout << "And it was bigger than the previous one!\n";
}
} else {
std::cout << "You entered the same number!\n";
}

// Now check for the "special property"
// Your numbers are special if they are both even and their difference is divisible by seven
int diff = value1 - value2;
bool is_value2_even= value2 % 2 == 0;

if (is_value1_even && is_value2_even&& (diff % 7 == 0)) {
std::cout << "Your numbers are special!";
}
}