Java Control
Java Control
We have mentioned that Java programs contain classes and that these classes contain
methods which contain statements that are executed by the computer. Now we look at
ways of putting statements together using control structures to organise the execution
of the program. A control structure might cause a statement to be executed once,
several times, or not at all. Control structures make up some of the statements of the
Java language. Statements are said to be sequentially composed when they are written
one after the other. Sequentially composed statements can be grouped together into a
block by using the left brace and right brace symbols ({ and }) to bracket them.
6.1 Assignments
An assignment statement in Java has two parts. It has a variable on the left-hand side
and an expression on the right. The expression is first evaluated to yield a value. That
value is then used as the new value of the variable. Here are some examples.
The equals sign is an assignment operator in Java, but it is not the only one. The op-
erators *=, /=, %=, += and -= are also assignment operators, and there are still others.
These operators combine the use of an operator and the execution of an assignment
into a single statement. These assignment operators provide a shorthand way of ex-
pressing assignment statements where the expression is used to modify the value of a
variable by making use of its previous value in order to calculate the new value. Here
are some examples of these kinds of assignment statements.
1
School of Informatics, University of Edinburgh Computer Science 1 Ah
Other kinds of updates occur so frequently that the Java language provides short-
hand versions of them. The operations of adding 1 to a variable and subtracting 1 from
a variable are abbreviated as shown below.
Statement Equivalent Effect x before x after
x++; x = x + 1; add one to x 5 6
x--; x = x - 1; subtract one from x 5 4
2
School of Informatics, University of Edinburgh Computer Science 1 Ah
?
true false
x == 0
System.out.println("zero");
System.out.println("non-zero");
-
All of the conditional statements which we have seen have just a single statement as
the then-statement or the else-statement. When we need to perform two actions in
some case then we need to bracket them together with left and right braces. Without
these the effect is quite different.
Statement Effect
if (x < 0) {
System.out.println("negative"); Changes the sign of x and prints
x *= -1; negative if x is negative.
}
if (x < 0) Prints negative if x is negative.
System.out.println("negative"); Changes the sign of x whether it
x *= -1; was negative or not.
When formatting computer programs we will normally use blank-space indentation to
convey hints to the reader about statement structure. However, as far as the compiler
for the Java language is concerned, one blank space is as good as ten so the different
effect of the two statements is achieved by the use of the left and right braces, and not
by the blank-space indentation at the start of the line.
3
School of Informatics, University of Edinburgh Computer Science 1 Ah
Statement Effect
switch (x) {
case 0: System.out.println ("zero"); Prints zero if x has the
break; value 0, prints non-zero
default: System.out.println ("non-zero"); otherwise.
break;
}
Without the break statements the flow of control falls through the statement so that
the first matching statement is executed and then all of the statements which follow it.
Since this is rarely useful, a switch statement almost always has a break statement at
the end of each case.
In the example shown above we have only one case treated specially in the switch.
We could have more. The if-then-else statement allows to choose between two possible
sub-statements but the switch allows us to choose between any number of them. The
other cases also appear in the body of the switch statement with the default case
coming at the end.
4
School of Informatics, University of Edinburgh Computer Science 1 Ah
6.4 Iteration
The statements which we have seen so far allow selective execution of statements.
To express repetitive execution of statements we use another construct, Javas while
statement. A while statement is often called a loop. A while loop contains a boolean-
valued expression which is known as its test (or sometimes its condition or sometimes
its guard). It also contains a statement which is the body of the loop.
The first thing to happen is that the boolean expression is evaluated. If it evaluates
to false then the body of the loop is not executed at all and the flow of control can
pass to the next statement in the program. If it evaluates to true then the loop body is
executed, and then the test is redone to see if the loop body is to be executed again (this
is the looping part), and this continues in this way until the test eventually evaluates
to false and the flow of control can then finally pass on to the next statement in the
program.
Here is a simple program fragment which prints out messages while decreasing the
value of x. After it has finished it prints a message to report this.
while (x > 0) {
System.out.println("decreasing");
x--;
}
System.out.println ("finished");
If we executed such a code fragment when the variable x held the value 3 then we
would see the following results:
decreasing
decreasing
decreasing
finished
The two statements which come between the open brace and the close brace are the
body of the loop. The body is repeatedly executed while the loop condition (x > 0) is
true.
There is a possibility that the loop body will never be executed at all. If the value
of x was zero or less upon reaching the while statement then the loop body will not be
executed. In general with a while loop the body is executed zero or more times.
As with the conditional statement, a while loop can be pictured in a diagram. Sim-
ilarly to the conditional statement again, a while loop has a boolean-valued condition
at the top. The result of this expression determines which statement will be executed
next. The picture is slightly more complicated because of the need to loop back to
repeat the test after the loop body has been completed.
5
School of Informatics, University of Edinburgh Computer Science 1 Ah
? false
x > 0
true
?
6
System.out.println("decreasing");
x--;
?