The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Summary of Control Flow Statements

For controlling the flow of a program, the Java programming language has three loop constructs, a flexible if-else statement, a switch statement, exception-handling statements, and branching statements.

Loops

Use the while statement to loop over a block of statements while a boolean expression remains true. The expression is evaluated at the top of the loop.
while (boolean expression) {
    statement(s)
}

Use the do-while statement to loop over a block of statements while a boolean expression remains true. The expression is evaluated at the bottom of the loop, so the statements within the do-while block execute at least once:

do {
    statement(s)
} while (expression);

The for statement loops over a block of statements and includes an initialization expression, a termination condition expression, and an increment expression.

for (initialization ; termination ; increment) {
    statement(s)
}

Decision-Making Statements

The Java programming language has two decision-making statements: if-else and switch. The more general-purpose statement is if; use switch to make multiple-choice decisions based on a single integer value. The following is the most basic if statement whose single statement block is executed if the boolean expression is true:
if (boolean expression) {
    statement(s)
}
Here's an if statement with a companion else statement. The if statement executes the first block if the boolean expression is true; otherwise, it executes the second block:
if (boolean expression) {
    statement(s)
} else {
    statement(s)
}
You can use else if to construct compound if statements:
if (boolean expression) {
    statement(s)
} else if (boolean expression) {
    statement(s)
} else if (boolean expression) {
    statement(s)
} else {
    statement(s)
}
The switch statement evaluates an integer expression and executes the appropriate case statement.
switch (integer expression) {
    case integer expression:
         statement(s)
         break;
    ...
    default:
         statement(s)
         break;
}

Exception-Handling Statements

Use the try, catch, and finally statements to handle exceptions.
try {
    statement(s)
} catch (exceptiontype name) {
    statement(s)
} catch (exceptiontype name) {
    statement(s)
} finally {
    statement(s)
}

Branching Statements

Some branching statements change the flow of control in a program to a labeled statement. You label a statement by placing a legal identifier (the label) followed by a colon (:) before the statement:
statementName: someJavaStatement;
Use the unlabeled form of the break statement to terminate the innermost switch, for, while, or do-while statement.
break;
Use the labeled form of the break statement to terminate an outer switch, for, while, or do-while statement with the given label:
break label;
A continue statement terminates the current iteration of the innermost loop and evaluates the boolean expression that controls the loop.
continue;
The labeled form of the continue statement terminates the current iteration of the loop with the given label:
continue label;
Use return to terminate the current method.
return;
You can return a value to the method's caller, by using the form of return that takes a value.
return value;

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form