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

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) {
    statements
}

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.

do {
    statement
} while (expression);

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

for (initialization ; termination ; increment) {
    statements
}

Decision-Making

A basic if statement whose single statement block is executed if the boolean expression is true.
if (boolean expression) {
    statements
}
An if statement with one boolean expression and two statement blocks. The first statement block is executed if the boolean expression is true and the second is executed if the boolean expression is false.
if (boolean expression) {
    statements
} else {
    statements
}
An if statement with multiple expressions.
if (boolean expression) {
    statements
} else if (boolean expression) {
    statements
}
An if statement with multiple expressions and an else statement.
if (boolean expression) {
    statements
} else if (boolean expression) {
    statements
} else {
    statements
}
The switch statement evaluates and integer expression and executes the appropriate case statement.
switch (integer expression) {
    case integer expression:
         statements
         break;
    ...
    default:
         statements
         break;
}

Exception-Handling

Throw an exception to indicate that an error occurred.
throw exception;
Use the try, catch, and finally statements to handle exceptions.
try {
    statements
} catch (exceptiontype name) {
    statements
} catch (exceptiontype name) {
    statements
} finally {
    statements
}

Branching

Use the unlabelled form of the break statement to terminate the innermost switch, for, while, or do statement.
break;
Use the labelled form of the break statement to terminate an outer switch, for, while, or do 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 labelled form of the continue statement terminates the current iteration of the loop with the given label and evaluates the boolean expression that controls the loop.
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