2.1 Relational and Logical Operators
Programs do not only run statements in order. They often need to check whether a condition holds, then choose what to do next. In C++, a condition's result is usually a bool: false means false, and true means true.
Relational operators
Relational operators compare two values. The result is true or false, not another number.
Common C++ relational operators:
<: less than>: greater than<=: less than or equal to>=: greater than or equal to==: equal to!=: not equal to
Important: one equals sign = means assignment. Two equals signs == mean comparison.
int age = 17;
std::cout << (age < 18) << std::endl; // 1
std::cout << (age == 18) << std::endl; // 0
std::cout << (age != 18) << std::endl; // 1By default, std::cout prints true as 1 and false as 0. You can print the words true and false by using std::boolalpha.
std::cout << std::boolalpha << (age < 18) << std::endl; // trueage = 17 stores 17 in the variable age. age == 18 checks whether the current value of age equals 18.
Logical operators
Logical operators combine multiple conditions. Their result is still true or false.
&&: the whole expression is true only when every condition is true.||: the whole expression is true when at least one condition is true.!: turns true into false, and false into true.
Truth tables
A truth table lists every possible input combination to make the rules clear.
&& is true only when both sides are true.
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
|| is true when at least one side is true.
| A | B | A `\ | \ | ` B |
|---|---|---|---|---|
| true | true | true | ||
| true | false | true | ||
| false | true | true | ||
| false | false | false |
! acts on a single value and flips the result.
| A | ! A |
|---|---|
| true | false |
| false | true |
int age = 20;
bool has_ticket = true;
std::cout << std::boolalpha;
std::cout << (age >= 18 && has_ticket) << std::endl; // true
std::cout << (age < 18 || has_ticket) << std::endl; // true
std::cout << (!has_ticket) << std::endl; // falseIt helps to read conditions like plain language: age >= 18 && has_ticket means "the age is at least 18, and the person has a ticket."
Combining conditions
Unlike math notation, C++ does not support chained comparisons with mathematical meaning. An expression such as 0 < age < 18 may compile, but it does not mean "age is between 0 and 18," so write it as two conditions joined by &&.
int age = 16;
std::cout << (0 < age && age < 18) << std::endl; // 1When a condition becomes complex, name each smaller condition first, then combine them.
int score = 85;
int attendance = 92;
bool passed = score >= 60 && attendance >= 80;
bool excellent = score >= 90 || attendance >= 95;
std::cout << std::boolalpha;
std::cout << passed << std::endl; // true
std::cout << excellent << std::endl; // falseDo not rush to squeeze every condition into one line. Naming smaller conditions makes the program easier to read and debug.