Recap
A Boolean expression evaluates to true or false. The simplest ones use a relational operator to compare two values.
| Operator | Meaning | Works on |
|---|---|---|
== |
equal to | primitives (values) and references (references) |
!= |
not equal to | primitives and references |
< > |
less than, greater than | numeric values |
<= >= |
less than or equal to, greater than or equal to | numeric values |
The expression 5 > 3 is a complete Boolean expression whose value is true. Its type is boolean.
Key relationships
| Idea | Rule |
|---|---|
| Result type | A relational expression is always boolean |
| Precedence | Arithmetic first (* / % then + -), then the relational operator |
| Words to operators | at least >=; at most <=; more than >; fewer than <; exactly ==; different != |
| Multiple of k | n % k == 0 |
| Storing the result | boolean isEven = n % 2 == 0; |
| Close enough (doubles) | Math.abs(x - y) < tolerance |
Assumptions
<,>,<=,>=are used only with numeric values.- A condition must be of type
boolean:x = 5is not a condition. - int / int is integer division; if either operand is a double the quotient is a double.
Mistakes to avoid
- Writing
=where==is meant. - Chaining comparisons such as
3 < x < 9. - Using
>when the question says “at least”, or>=when it says “more than”. - Forgetting that
7 / 2is 3 when both areint. - Testing computed doubles with
==. - Evaluating the relational operator before the arithmetic.
Worked mini-example
Let m = 14 and k = 4. Evaluate each expression on its own.
m / k >= 3:m / kis int division, 14 / 4 = 3, and 3 >= 3 is true.m % k == 2: 14 % 4 = 2, and 2 == 2 is true.m / (double) k > 3.5: 14 / 4.0 = 3.5, and 3.5 > 3.5 is false.m - k * 2 != 6: 14 - 8 = 6, and 6 != 6 is false.
Method: replace each variable by its value, finish the arithmetic on both sides (watching for int division), then apply the relational operator last. If the question uses a boundary word such as “at least”, test the boundary value itself.
Quick self-check
- Evaluate
8 % 3 == 2. Answer: 8 % 3 is 2, so 2 == 2 is true. - Evaluate
9 / 2 > 4. Answer: 9 / 2 is the int 4, so 4 > 4 is false. - Write “n is at most 100” as a Boolean expression. Answer:
n <= 100. - Why does
1 < y < 5fail to compile? Answer:1 < yis a boolean, and a boolean cannot be compared to 5 with<. - Which value does
3 + 4 * 2 >= 11give? Answer: 3 + 8 = 11, and 11 >= 11 is true.
Full explanations and diagrams are in the Topic 2.2 study guide.