Different types of statements are described in the following sections.
variable = expression;
This statement changes the value of the variable on the left side of the
equals sign to the value of the expression on the right-hand side.
The variable is often just specified by a variable name, but
there are also expressions that specify variables.
Java treats an assignment as both an expression and as a statement. As an expression, its value is the value assigned to the variable. This is done to allow multiple assignments in a single statement, such as
a = b = 5;
By treating b = 5 as an expression with value 5, Java makes
sense of this statement, assigning the value 5 to both a and
b.
receiver.method-name(parameters)
Here,
Messages can be used in three ways to form statements. First, if the method specified by a message returns a value then the message can be used as the expression in an assignment statement.
variable = message;
For messages with methods that do not return values, a statement can also be formed by just terminating the message with a semicolon.
message;
This statement form can also be used when the method returns a value,
although it is not usually a good idea to ignore a returned value.
Finally, if a message returns an object, then that object can be used directly as the receiver of a message. In an applet method, for example, getContentPane() returns a container to which components can be added. This container is the receiver of the add() message in the following statement.
getContentPane().add(button);
Statement blocks are used to define methods and to allow multiple statements in the control structures described in the following sections.
| If Statements | - | conditional execution of a single statement |
| If-Else Statements | - | conditional selection among two statements |
| Switch Statements | - | conditional selection among several statements |
| Extended If-Else Statements | - | conditional selection among several statements |
| While Loops | - | pretest loops |
| For Loops | - | pretest loops |
| Do-While Loops | - | posttest loops |
| Return Statements | - | return values from and terminate methods |
| Continue Statements | - | skip the remaining statements in an iteration of a loop |
| Break Statements | - | exit a loop or switch statement |
if (boolean-expression) {
then-clause
}
Here,
if (boolean-expression) {
then-clause
} else {
else-clause
}
Here,
switch (control-expression) {
case constant-expression-1:
statements-1
.
.
.
case constant-expression-n:
statements-n
default:
default-statements
}
Here,
When the switch statement is executed, control-expression is evaluated. The resulting value is compared to the values of constant-expression-1 through constant-expression-n in order until a matching value is found. If a match is found in constant-expression-i then statements-i through statements-n and default-statements are executed, with switch statement execution terminated if a break statement is encountered. Normally, the last statement in each sequence is a break statement so that only one sequence is executed.
The default clause is optional. If it is present then the default-statements are executed whenever the value of control-expression does not match any of the constant-expression-i values.
if (boolean-expression-1) {
statements-1
} else if (boolean-expression-2) {
statements-2
.
.
.
} else if (boolean-expression-n) {
statements-n
} else {
default-statements
}
Here,
When this extended if-else statement is executed, the boolean expressions are evaluated in order until one is found that is true. Then the corresponding sequence of statements is executed. If none of the boolean expressions is true then the default-statements are executed. In either case, execution continues with the next statement after the extended if-else statement.
When a loop is executed its nested statement can be executed any number of times. Each execution of the nested statement is called an iteration of the loop. The number of iterations is controlled by a boolean expression. The boolean expression is evaluated before each iteration (pretest) in while loops and for loops, and after each iteration (post-test) in do-while loops. When the boolean expression becomes false the loop is terminated.
while (boolean-expression) {
nested-statements
}
Here,
The boolean expression is tested before each iteration of the loop. The loop terminates when it is false.
for (initialization; boolean-expression; increment) {
nested-statements
}
Here,
When a for loop is executed the initialization expression is evaluted
first.
This expression is usually an assignment (for example, i = 0)
that sets the initial value of a loop control variable.
The boolean expression is tested before each iteration of the loop.
The loop terminates when it is false.
The boolean expression is frequently a comparison (for example,
i < 10).
At the end of each iteration, the increment expression is evaluated.
The expression is often an expression that increments the control variable
(for example, i++).
do {
nested-statements
} while (boolean-expression);
Here,
The boolean expression is tested after each iteration of the loop. The loop terminates when it is false.
void use the following form.
return;
Methods with non-void returned type use the following form.
return expression;
Here,
continue;
After a continue statement is executed in a for loop, its increment and boolean expression are evaluated. If the boolean expression is true then the nested statements are executed again.
After a continue statement is executed in a while or do-while loop, its boolean expression is evaluated. If the boolean expression is true then the nested statements are executed again.
break;
After a break statement is executed, execution proceeds to the statement that follows the enclosing loop or switch statement.