Java Statements


Methods and constructors are sequences of statements, along with variable definitions. The statements specify the sequence of actions to be performed when a method or constructor is invoked. They can alter the value of variables, generate output, process input, or respond to user mouse or keyboard actions.

Different types of statements are described in the following sections.

Assignment Statements

An assignment statement has the following form.
    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.

Statements involving Messages

Messages are the fundamental means of communication between objects in a Java program. A message has the following form.
    receiver.method-name(parameters)

Here,

The receiver can be omitted if it is the object that you are writing code for. That is, you do not need to specify the receiver for messages sent from an object to itself.

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

In Java, any sequence of statements can be grouped together to function as a single statement by enclosing the sequence in braces. These groupings are called statement blocks. A statement block may also include variable declarations.

Statement blocks are used to define methods and to allow multiple statements in the control structures described in the following sections.

Control Statements

Normally, statements in a method or constructor are executed sequentially. Java also has control statements that allow repetitive execution of statements and conditional execution of statements. Java has the following types of control statements.

Conditional Execution and Selection

Java has three kinds of statements that permit execution of a nested statement based on the value of a boolean expression or selection among several statements based on the value of a boolean expression or a control variable. These statements are the if statement, the if-else statement, and the switch statement.

If Statements
The if statement has the following form.
    if (boolean-expression) {
	then-clause
    }
Here,

If-Else Statements
The if-else statement has the following form.
    if (boolean-expression) {
	then-clause
    } else {
	else-clause
    }
Here,

Switch Statements
The switch statement allows execution of different statements depending on the value of an expression. It has the following form.
    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.

Extended If-Else Statements
Often, a programmer needs a construction that works like a switch statement, but the selection between choices is too complex for a switch statement. To make this work, a programmer can use a sequence of if statements where each if statement is nested in the else clause of the preceding if statement. This construction is called an extended is statement. It has the following form.
    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.

Repetition

Java has three kinds of loop statements: while loops, for loops, and do-while loops. Loop statements allow a nested statement to be executed repetitively. The nested statement can be a block statement, allowing repetition of a sequence of statements.

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 Loops
The while loop is a pretest loop statement. It has the following form.
    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 Loops
The for loop is a pretest loop statement. It has the following form.
    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-While Loops
The do-while loop is a post-test loop statement. It has the following form.
    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.

Special Control Statements

Return Statements
The return statement is used in the definition of a method to set its returned value and to terminate execution of the method. It has two forms. Methods with returned type void use the following form.
    return;

Methods with non-void returned type use the following form.

    return expression;
Here,

Continue Statements
The continue statement is used in while loop, for loop, or do-while loop to terminate an iteration of the loop. A continue statement has the following form.
    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 Statements
The break statement is used in loop (for, while, and do-while) statements and switch statements to terminate execution of the statement. A break statement has the following form.
    break;

After a break statement is executed, execution proceeds to the statement that follows the enclosing loop or switch statement.