Programs do not only run statements in order. They often need to check whether a condition is true, then choose what to do next. A condition usually produces a Boolean value: true or false.
Relational operators
Relational operators compare two values. The result of a comparison is a Boolean value, 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;
cout << (age < 18) << endl; // true
cout << (age == 18) << endl; // false
cout << (age != 18) << endl; // trueage = 17 stores 17 in the variable age. age == 18 checks whether the current value of age equals 18.
KNOWLEDGE QUIZ
Logical operators
Logical operators combine multiple conditions. Their result is still a Boolean value.
&&: the whole expression istrueonly when every condition istrue.||: the whole expression istruewhen at least one condition istrue.!: turnstrueintofalse, andfalseintotrue.
int age = 20;
bool hasTicket = true;
cout << (age >= 18 && hasTicket) << endl; // true
cout << (age < 18 || hasTicket) << endl; // true
cout << !hasTicket << endl; // falseIt helps to read conditions like plain language: age >= 18 && hasTicket means “the age is at least 18, and the person has a ticket.”
Stadium Smart Gate
Choose a visitor, then scan their ID and ticket. The gate opens only when both checks are true.
Age check20 >= 18 → ?
Ticket checkhasTicket → ?
Gate resultWaiting
KNOWLEDGE QUIZ
Combining conditions
C++ does not support mathematical chained-comparison syntax; join two comparisons with &&:
int age = 16;
cout << (age > 0 && age < 18) << endl; // trueThis is equivalent to:
int age = 16;
cout << (age > 0 && age < 18) << endl;When a condition becomes complex, use parentheses or meaningful variable names to keep the logic readable.
int score = 85;
int attendance = 92;
bool passed = score >= 60 && attendance >= 80;
bool excellent = score >= 90 || attendance >= 95;
cout << passed << endl; // true
cout << excellent << endl; // falseDo not rush to squeeze every condition into one line. Naming smaller conditions makes the program easier to read and debug.
KNOWLEDGE QUIZ