The “for” statement looks like this:
for (
Initial; Test; Increment )
Statement
There are new versions of “for” to use with collection classes, arrays and enumerated types:
for (
SomeType varOfSomeType : SomeCollectionVariable )
Statement
for (
SomeType varOfSomeType : ArrayVariableOfSomeType )
Statement
for (
SomeType varOfSomeType : EnumSet )
Statement
These versions of “for” allow you to cycle easily through all elements in a collection or an array or an enumeration, or subranges of an enumeration. They are covered in Chapter 16, “Collections,” Chapter 9, “Arrays,” and Chapter 6, “Static, Final, and Enumerated Types.”
Statement Notes
✓ |
Initial, Test, and Increment are all expressions that control the loop. The semicolons are required, but any or all of the expressions are optional. The Test expression must be a boolean or a Boolean. A typical loop will look like this: for( i=0; i<100; i++ ) { /*loop body*/ } An infinite loop will have blank Initial Test and Increments, like this: for (;;) |
✓ |
It is possible and handy to declare the loop variable in the “for” statement, like this: for( int i=0; i<100; i++ ) { /*loop body*/ } This is a nice feature created for the convenience of the programmer. |
✓ |
The comma separator “,” is allowed in the Initial and Increment sections of loops. This is so you can string together several initializations or increments, like this: for(i=0,j=0; i<100; i++, j=i*2 ) { /*loop body*/ } |
The “while” statement comes in two varieties. The first looks like this:
while (
Expression )
Statement
Statement Notes
✓ |
While the boolean or Boolean-typed expression remains true, the Statement is executed. |
✓ |
This form of loop is for iterations that take place zero or more times. If the Expression is false on the first evaluation, the Statement will not execute. |
Example:
while ( moreData() ) {
readData();
processData();
}
The “do while” variety of the while statement looks like this:
do
Statement while (
Expression ) ;
Statement Notes
✓ |
The Statement is executed, and then the boolean or Boolean-typed expression is evaluated. If it is true, you loop through the Statement again and test again. If it is false, execution drops through to the following statement. |
✓ |
This form of loop is for iterations that need to be done at least one time. If the Expression is false on the first evaluation, the Statement will already have executed once. |
Example:
do {
getCreditAuthorization();
processSale();
} while ( tenderType().equals("credit card") );
There may be “continue” statements in a for or while loop. These look like:
continue;
continue
Identifier;
Continue statements occur only in loops. When a continue statement is executed, it causes the flow of control to pass to the next iteration of the loop. It's as though you say, “Well, that's it for iteration N; increment the loop variable (if this is a for loop), do the test, and continue with iteration N+1.”
The “continue Identifier” form is used when you have nested loops, and you want to break out of an inner one altogether and start with the next iteration of the outer loop.
The flow of control will continue with the test for the next iteration of the outer loop with the matching identifier label. Here's the right way to read it. When you see “continue articleMainLoop
;” look backwards in the code for a loop labelled like any one of these three:
articleMainLoop: for (int i=0; /*loop*/ or
articleMainLoop: while (int i=0; /*loop*/ or
articleMainLoop: do { /*loop*/
That is the place that the test and next iteration will continue. The following code shows an example continue statement:
There may be “break” statements in a loop or a switch. These look like this:
break;
Or they look like this:
break identifier;
Break is a more dramatic version of continue. Break with no identifier causes control to pass to just after the end of the immediately enclosing “for, do, while,” or “switch” statement. The loop or switch statement is “broken out of.” Break with no identifier can appear only in a statement by virtue of the whole thing being nested in an iteration or switch statement. You will break to the end of the iteration or switch, not any “if” statement it's immediately nested in.
If an identifier is included, it must be an identifier matching the label on some enclosing statement. The enclosing statement can be any kind of statement, not just an iterative or switch statement. In other words, it's OK to break out of any kind of enclosing block as long as you explicitly indicate it with a label.
In practice, it's almost always a loop or switch that you break out of, not an if or block. There is the slightly confusing feature that statements are labeled at their beginning, but “break” causes you to jump to their end. Here is an example:
18.119.158.134