7.3 Operators, Precedence, Expressions, and Formulas
Key Takeaways
- The standard operator groups are assignment (←), arithmetic (+ − * / % ^), relational (== < > ≤ ≥ ≠), and logical (and, or, not).
- Multiplication, division, and remainder are evaluated before addition and subtraction, left to right within each level; parentheses override everything.
- A formula's fractions need parentheses: the average of a and b is ( a + b ) / 2, not a + b / 2.
- With integer division, 1 / 2 * base * height is 0; write 0.5 * base * height or base * height / 2.0 instead.
- With integers, n % 10 gives the last digit, n / 10 removes the last digit, and n % 2 == 0 tests for an even number.
What this competency asks
ETS asks you to understand how to use standard operators (assignment, arithmetic, relational, logical) and operator precedence to write programs. The listed skills include tracing code, finding missing code, identifying equivalent statements, putting statements in order, using Boolean algebra (Section 7.4), and two skills specific to this topic:
- Identify the correct implementation of a given formula, including formulas with fractions.
- Evaluate expressions that include arithmetic operations.
The four operator families
| Family | ETS symbols | Produces | Example |
|---|---|---|---|
| Assignment | ← | Stores a value | x ← x + 1 |
| Arithmetic | + - * / % ^ | A number | total * 2, n % 2 |
| Relational | == < > ≤ ≥ ≠ | true or false | score ≥ 70 |
| Logical | and or not | true or false | ( age ≥ 13 ) and ( age < 18 ) |
+ is also string concatenation when a string is involved. ^ appears in ETS's list of arithmetic operators, where it conventionally means exponentiation. In Java and C, however, ^ means bitwise XOR, which is one more reason to read the notation you are given.
Precedence
ETS does not publish a precedence table for its pseudocode, and its code usually parenthesizes conditions fully. When parentheses are missing, the conventional order is:
| Priority | Operators | Grouping |
|---|---|---|
| 1 (highest) | Parentheses ( ) | Innermost first |
| 2 | Unary minus, not | — |
| 3 | ^ (exponentiation, where supported) | — |
| 4 | * / % | Left to right |
| 5 | + - | Left to right |
| 6 | < > ≤ ≥ | — |
| 7 | == ≠ | — |
| 8 | and | — |
| 9 | or | — |
| 10 (lowest) | ← | The right side is evaluated first |
Languages differ in the details. In Python, for example, not binds more loosely than comparisons. When in doubt, add parentheses to your own code and look for them in ETS code.
Worked evaluation (all values int; / is integer division)
14 + 6 / 2 * 3 - 5 % 2
*,/, and%first, left to right: 6 / 2 = 3, then 3 * 3 = 9, and separately 5 % 2 = 1.- The expression is now 14 + 9 − 1.
+and-left to right: 23 − 1 = 22.
19 / 4 + 19 % 4 * 3: 19 / 4 = 4; 19 % 4 = 3, then 3 × 3 = 9; 4 + 9 = 13.
The remainder operator in practice
| Goal | Expression | Example (n = 4726) |
|---|---|---|
| Test for even or odd | n % 2 == 0 | 4726 % 2 = 0, so even |
| Last digit | n % 10 | 6 |
| Drop the last digit | n / 10 (integer) | 472 |
| Wrap an index around a circle of size k | ( i + 1 ) % k | Index 4 with k = 5 wraps to 0 |
| Every third item | i % 3 == 0 | Items 0, 3, 6, … |
| Minutes to hours and minutes | m / 60 and m % 60 | 125 minutes gives 2 hours and 5 minutes |
A digit-summing loop combines the last two rows: repeatedly add n % 10 to a total, then set n ← n / 10, until n is 0.
Implementing formulas with fractions
Written math hides grouping that code must make explicit.
| Formula | Correct | Wrong (why) |
|---|---|---|
| Average of a and b | ( a + b ) / 2 | a + b / 2: only b is halved |
| Slope between two points | ( y2 - y1 ) / ( x2 - x1 ) | y2 - y1 / x2 - x1: division happens first |
| Quadratic root | ( -b + sqrt ( b * b - 4 * a * c ) ) / ( 2 * a ) | … / 2 * a divides by 2 and then multiplies by a |
| Triangle area | 0.5 * base * height or base * height / 2.0 | 1 / 2 * base * height: with integers, 1 / 2 is 0 |
| Celsius to Fahrenheit | 9.0 / 5 * c + 32 | 9 / 5 * c + 32 with integers: 9 / 5 is 1, so the result is c + 32 |
Two habits prevent most errors:
- Wrap every numerator and every denominator that contains more than one term in parentheses.
- Check the types. If every operand is an integer and the formula needs a fraction, make one operand floating-point (write
2.0, or convert a variable first). Order matters too: with integers,c * 9 / 5is closer to correct than9 / 5 * c, because the multiplication happens before the truncating division. It still truncates the result, though, since 37 * 9 / 5 = 333 / 5 = 66.
Equivalent statements
Some rewrites are always equivalent, and some are only equivalent with real-number arithmetic.
| Pair | Equivalent? |
|---|---|
x ← x + 1 and x ← 1 + x | Yes |
total ← total * 2 and total ← total + total | Yes |
( a + b ) / 2 and a / 2 + b / 2 (integers) | No. a = 3, b = 5 gives 4 versus 1 + 2 = 3 |
a * ( b + c ) and a * b + a * c | Yes (the distributive property holds for integers) |
( x > 5 ) and not ( x ≤ 5 ) | Yes |
x / 2 * 2 == x for every integer x | No. It is true only when x is even |
When asked whether two segments are equivalent, test boundary values, such as odd numbers, 0, and negative numbers, rather than just one convenient case.
Assignment versus equality
x ← 5 changes x. x == 5 asks whether x equals 5. In languages such as C and Java, = is assignment. Writing if (x = 5) is a classic bug: in C it assigns 5 and treats the condition as true, and in Java it fails to compile for an int x.
Putting statements in order
Many questions show the right statements in the wrong order. For example, to swap the values of a and b, you need a temporary variable, and the order matters:
int temp ← a
a ← b
b ← temp
Writing a ← b first would destroy a's original value before it was saved. Tracing with concrete values, such as a = 1 and b = 2, confirms the order.
All variables are int, and / performs integer division on int values. What is stored in result?
int x ← 19
int y ← 4
int z ← 3
int result ← x / y + x % y * z
base and height are int variables, and / performs integer division when both operands are int. Which expression correctly computes the area of a triangle, one-half times base times height, for all values?
Let a ← 3 and b ← 5, both int, with / performing integer division. Which expression's value is different from the value of ( a + b ) / 2?