- Practice
- About 13 min
- 5 practice questions
Expression evaluation drill: precedence, integer division, modulus and logical operators
Evaluate arithmetic, relational and logical expressions by applying precedence and left-to-right order, and avoid the usual slips with integer division.
If something looks wrong, please tell us.
Expression questions ask for a single value: a number, True or False. The only skill needed is applying the order of evaluation without skipping a step. Write the expression, mark the operator to do first, replace it with its value and repeat. In this lesson / between whole numbers means integer division, which keeps only the whole part.
Order of evaluation
| Order | Operators | Notes |
|---|---|---|
| 1 | Brackets ( ) | Innermost first |
| 2 | NOT | Reverses True and False |
| 3 | * , / , % | Equal rank; work left to right |
| 4 | + , - | Equal rank; work left to right |
| 5 | < , <= , > , >= , == , != | Give True or False |
| 6 | AND | True only if both sides are True |
| 7 | OR | True if at least one side is True |
| 8 | = (assignment) | Done last: right side first, then store |
Integer division and remainder go together: for whole numbers a and b, a = b x (a / b) + (a % b). For example, 17 / 5 = 3 and 17 % 5 = 2, because 5 x 3 + 2 = 17.
Worked example 1: arithmetic
Evaluate 8 + 6 / 2 3 - 4. The operators / and have equal rank, so work left to right: 6 / 2 = 3, then 3 * 3 = 9. The expression is now 8 + 9 - 4 = 17 - 4 = 13.
Worked example 2: the left-to-right trap
Evaluate 100 / 8 2. Left to right: 100 / 8 = 12 (integer division), then 12 2 = 24. A student who first multiplies 8 2 = 16 gets 100 / 16 = 6, which is wrong. Only brackets, as in 100 / (8 2), would give 6. Also evaluate 2 (3 + 4) % 5: the bracket gives 7, then 2 7 = 14, then 14 % 5 = 4.
Worked example 3: division and remainder together
Evaluate 17 % 5 + 17 / 5. The remainder is 2 and the integer quotient is 3. The sum is 2 + 3 = 5. And if a = 7 and b = 2, then a / b is 3 (integer), a % b is 1, but the real-number division 7 / 2 would be 3.5 in a language that uses real arithmetic for decimals.
Worked example 4: logical expression
Evaluate (7 % 2 == 1) AND NOT (4 > 5). Left bracket: 7 % 2 = 1, and 1 == 1 is True. In the right part, 4 > 5 is False, so NOT False is True. True AND True is True. Evaluate (5 < 3) OR (9 >= 9): False OR True is True.
Worked example 5: sequence of assignments
x = 10; x = x + 5; x = x * 2; x = x - 7. After each statement x is 15, then 30, then 23. The final value is 23. Each statement uses the value left by the one before it.
Brackets, NOT, then * / % left to right, then + -, then comparisons, then AND, then OR.
Common traps
- Doing multiplication before division when they appear as / then *.
- Treating integer division as rounding; it drops the fraction (7 / 2 is 3, not 4).
- Forgetting that a % b is smaller than b.
- Applying NOT to the wrong part because brackets were ignored.
Check your understanding
-
1. Evaluate 12 / 4 * 3.
- 9
- 1
- 3
- 36
Show answer
Answer: A. Work left to right: 12 / 4 = 3, then 3 * 3 = 9.
-
2. What is 23 % 6?
- 4
- 5
- 3
- 2
Show answer
Answer: B. 23 = 6 x 3 + 5, so the remainder is 5.
-
3. Evaluate 4 + 18 / 3 % 4.
- 5
- 8
- 6
- 4
Show answer
Answer: C. 18 / 3 = 6, then 6 % 4 = 2, then 4 + 2 = 6.
-
4. Evaluate (3 > 1) AND (2 > 5).
- True
- Error
- Unknown
- False
Show answer
Answer: D. The second comparison is false, so True AND False is False.
-
5. x = 4; x = x * 3; x = x - 5. What is x?
- 7
- 12
- 5
- 9
Show answer
Answer: A. x becomes 12 and then 12 - 5 = 7.
Sign in to keep track of the lessons you finish.