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;
printf("%d\n", (int)(age < 18)); // true
printf("%d\n", (int)(age == 18)); // false
printf("%d\n", (int)(age != 18)); // 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;
printf("%d\n", (int)(age >= 18 && hasTicket)); // true
printf("%d\n", (int)(age < 18 || hasTicket)); // true
printf("%d\n", (int)(!hasTicket)); // 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;
printf("%d\n", (int)(age > 0 && age < 18)); // trueThis is equivalent to:
printf("%d\n", age > 0 && age < 18);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;
printf("%d\n", (int)(passed)); // true
printf("%d\n", (int)(excellent)); // falseDo not rush to squeeze every condition into one line. Naming smaller conditions makes the program easier to read and debug.
KNOWLEDGE QUIZ