1
Lesson 4 – Elementary Programming
Goal(s):
● To write and evaluate expressions
● To use augmented assignment operators (+=, -=, *=, /=, %=)
● To distinguish between post-increment and pre-increment and between post-
decrement and pre-decrement
Minds On…
Calculate the perimeter and area of the circle with radius 10 cm.
Action …
Evaluating Expressions and Operator Precedence
Key Point
Java expressions are evaluated in the same way as arithmetic expressions.
Writing numerical expressions in Java involves a straightforward translation of an
arithmetic expression using Java operators.
Though Java has its own way to evaluate an expression behind the scenes, the result of a
Java expression and its corresponding arithmetic expression are the same. Therefore, you
can safely apply the arithmetic rule for evaluating a Java expression. Operators contained
within pairs of parentheses are evaluated first. Parentheses can be nested, in which case the
expression in the inner parentheses is evaluated first. When more than one operator is used
in an expression, the following operator precedence rule is used to determine the order of
evaluation.
● Multiplication, division, and Modulo operators are applied next. If an expression
contains several multiplication, division, and remainder operators, they are applied from
left to right.
● Addition and subtraction operators are applied last. If an expression contains several
addition and subtraction operators, they are applied from left to righ
Here is an example of how an expression is evaluated:
Augmented Assignment Operators
Key Point
The operators +, -, *, /, and % can be combined with the assignment operator (=) to form
augmented expressions.
● = x + 5 i equiva t x + 5
s lent o =
x
● = x - 5 i equiva t x - 5
s lent o =
x
● = x * 5 i equiva t x * 5
s lent o =
x
● = x / 5 i equiva t x / 5
s lent o =
x
● = x % 5 i equiva t x % 5
s lent o =
x
Increment and Decrement Operators
Key Point
The increment (++) and decrement (--) operators are for incrementing and
decrementing a variable by 1.
The ++ and -- are two shorthand operators for incrementing and
decrementing a variable by 1. There are handy, because that’s often how
much the value needs to be changed in many programming tasks. Fore
example, the following code increments i by 1 and decrements j by 1.
++i increments i by 1 and --j decrements j by 1. These operators are known as prefix
increment (pre-increment) and prefix decrement (pre-decrement).
4
Assessment
1. How would you write the following arithmetic expression in Java?
2. What is the result of (4 + 1) * ((5 - 2) / 2)?
3. Show the output of the following code:
int a = 6;
a -= a + 1;
System.out.println(a);
a *= 6;
System.out.println(a);
a /= 2;
System.out.println(a);
4. Use an augmented assignment operator to write a statement to add 5 into variable x.
5. To add a value 1 to variable x, you write:
6. Show the output of the following code:
int a = 6;
int b = a++;
System.out.println(a);
System.out.println(b);
int a = 6;
b = ++a
System.out.println(a);
System.out.println(b);
7. Show the output of the following code:
int a = 6;
int b = a--;
System.out.println(a);
System.out.println(b);
a = 6;
b = --a;
System.out.println(a);
System.out.println(b);
8. Word matching exercise.
9. Are the following four statements equivalent?
10. What will be the output of the following code?