Relational operators compare two values and produce a Boolean value, either TRUE or FALSE. Boolean operators combine or reverse Boolean conditions, allowing programs to make more complex decisions.
Relational operators
A relational expression evaluates the relationship between two operands.
| Operator | Meaning | Example and result |
|---|---|---|
< | Less than | 4 < 7 is TRUE |
<= | Less than or equal to | 7 <= 7 is TRUE |
> | Greater than | 9 > 12 is FALSE |
>= | Greater than or equal to | 10 >= 8 is TRUE |
== | Equal to | 5 == 5 is TRUE |
!= | Not equal to | 5 != 3 is TRUE |
These operators commonly form the conditions used in selection and iteration. For example, IF score >= 50 THEN executes its branch when score is at least 50.
Boolean operators
Boolean operators act on expressions that already evaluate to TRUE or FALSE.
| Operator | Result |
|---|---|
AND | TRUE only when both conditions are TRUE |
OR | TRUE when at least one condition is TRUE |
NOT | Reverses the Boolean value |
For example, age >= 13 AND age <= 19 is TRUE only when both comparisons are true. day == "Saturday" OR day == "Sunday" is true for either weekend day. NOT loggedIn is true when loggedIn is false.
A common misconception is that OR requires exactly one true condition. In standard Boolean logic, inclusive OR is also true when both conditions are true. Another error is confusing ==, which tests equality, with assignment syntax such as =.
Exam technique
For IB B2.3 programming constructs questions, evaluate each relational expression first, then apply NOT, AND, and OR according to the stated precedence or parentheses. Show intermediate Boolean values when tracing code to reduce errors.