Flow Control

Java provides a full set of flow control statements. These flow controls are statements, not expressions—meaning that they don't have a value or a type.

if Statement

The simplest flow control statement is the if statement:

if (boolexpr)
{
 // any number of statements
}
else
{
 // any number of statements
}


First Java evaluates the boolean expression. If it's true, the statements contained in the first block are executed. If not, the statements contained in the else block are executed. Of course, the else clause is optional.

while loop

The easiest of the Java loop structures is the while loop. The while loop comes in two variations.

The first variation:

while (boolexpr)
{
 // statements
}


The second variation:

do
{
 // statements
} while (boolexpr);


In the first variation, the boolean expression boolexpr is evaluated. If it's true, the Java statements within the block are executed. Once all of the statements within the braces have been executed, control loops back up to the top of the while loop and the boolean expression is reevaluated, thereby starting the whole process over again. If boolexpr isn't true, control jumps to the first statement after the closed brace.

The second variation is similar to the first, except that the boolean expression isn't evaluated until after the Java statements within the block have executed, so the statements within the block will be executed at least once. If the expression is true, control passes up to the top of the block; if the expression is false, control passes to the statement immediately following the while.

for loop

The most common of the loops is the for loop. A for loop is defined as follows:

for (int a = 0; a < 10; a++)
{
 // statements
}


This is equivalent to the following:

{
 int a = 0;
 while(a < 10)
 {
 .
 .
 .
 a++;
 }
}


The break and continue Statements

The keywords break and continue allow the program to abort execution of the statements from within a loop. The unlabeled break statement passes control outside of the loop immediately. In contrast, the break statement can be labeled to allow control to pass out of multiple loops at one time.

NOTE
The labeled break addresses the primary reason that programmers say they need a goto statement. This controlled exit is about as close as Java comes to a goto statement.

The continue statement passes control to the closed brace of a loop, causing control to pass directly to the conditional expression. The continue statement can also carry a label, in which case control passes to the closing brace of the labeled loop.

switch Statement

The switch statement is useful when you are selecting from a number of alternatives:

switch(expr)
{
 case cexpr1:
 // Java statements
 break;
 case cexpr2:
 // more Java statements
 break;
 default:
 // even more Java statements
}


Java evaluates the expression expr. Its value is then compared to each of the constant integer expressions (cexprn) listed after the case statements. (A constant expression is an expression whose value can be computed at compile time.) Control passes to the case statement whose expression value matches. If none of the case statements match, control passes to the optional default case. If no default case is provided, then no case is selected and control passes to the first statement after the closed brace.

The break at the end of each case isn't required; however, without it control passes straight through to the next case. Comments