๐ WAYPOINT LESSON
Arithmetic, relational, increment
โญ beginnerโณ 12 min read๐ Lesson 9 of 256
Operator families, == vs =, and ++ / -- as statements.
Arithmetic operators: + - * / % with the precedence you expect
(*, /, % bind tighter than +, -), left to right among equals.
System.out.println(2 + 3 * 4); // 14, not 20
System.out.println((2 + 3) * 4); // 20 โ parentheses win
System.out.println(10 - 4 - 3); // 3 โ left to right
Relational operators produce a boolean: < > <= >= == !=.
int x = 5;
System.out.println(x < 10); // true
System.out.println(x == 5); // true โ == compares values
System.out.println(x != 5); // false
The single = assigns; the double == compares. if (x = 5) does not even
compile in Java (an int is not a boolean) โ a lucky protection.
Increment and decrement:
int n = 5;
n++; // n is 6 (statement form: same as n = n + 1)
n--; // n is 5
As a statement (the AP-exam form), n++ and ++n are identical. Only
inside a larger expression do ++n (increment then use) and n++ (use then
increment) differ โ a distinction the exam tests by asking what a
statement sequence prints, so trace one statement at a time.