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 Java 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;
System.out.println(age < 18) // true;
System.out.println(age == 18) // false;
System.out.println(age != 18) // true;age = 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;
boolean hasTicket = true;
System.out.println(age >= 18 && hasTicket) // true;
System.out.println(age < 18 || hasTicket) // true;
System.out.println(!hasTicket) // false;It 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
Java supports chained comparisons:
int age = 16;
System.out.println(age > 0 && age < 18) // true;This is equivalent to:
System.out.println(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;
boolean passed = score >= 60 && attendance >= 80;
boolean excellent = score >= 90 || attendance >= 95;
System.out.println(passed) // true;
System.out.println(excellent) // false;Do not rush to squeeze every condition into one line. Naming smaller conditions makes the program easier to read and debug.
KNOWLEDGE QUIZ