Iteration
Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements. A statement repeats until the controlling Boolean-expression evaluates to false. The form for a while loop is:
while(Boolean-expression)
statement
The Boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statement.
Here’s a simple example that generates random numbers until a particular condition is met:
//: control/WhileTest.java // Demonstrates the while loop. public class WhileTest { static boolean condition() { boolean result = Math.random() < 0.99; System.out.print(result + ", "); return result; } public static void main(String[] args) { while(condition()) System.out.println("Inside ‘while’"); System.out.println("Exited ‘while’"); } } /* (Execute to see output) *///:~
The condition( ) method uses the static method random( ) in the Math library, which generates a double value between 0 and 1. (It includes 0, but not 1.) The result value comes from the comparison operator <, which produces a boolean result. If you print a boolean value, you automatically get the appropriate string “true” or “false.” The conditional expression for the while says: “repeat the statements in the body as long as condition( ) returns true.”
do-while
The form for do-while is
do
statement
while(Boolean-expression);
The sole difference between while and do-while is that the statement of the do-while always executes at least once, even if the expression evaluates to false the first time. In a while, if the conditional is false the first time the statement never executes. In practice, do-while is less common than while.
for
A for loop is perhaps the most commonly used form of iteration. This loop performs initialization before the first iteration. Then it performs conditional testing and, at the end of each iteration, some form of “stepping.” The form of the for loop is:
for(initialization; Boolean-expression; step)
statement
Any of the expressions initialization, Boolean-expression or step can be empty. The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes.
for loops are usually used for “counting” tasks:
//: control/ListCharacters.java // Demonstrates "for" loop by listing // all the lowercase ASCII letters. public class ListCharacters { public static void main(String[] args) { for(char c = 0; c < 128; c++) if(Character.isLowerCase(c)) System.out.println("value: " + (int)c + " character: " + c); } } /* Output: value: 97 character: a value: 98 character: b value: 99 character: c value: 100 character: d value: 101 character: e value: 102 character: f value: 103 character: g value: 104 character: h value: 105 character: i value: 106 character: j ... Controlling Execution 95 *///:~
Note that the variable c is defined at the point where it is used, inside the control expression of the for loop, rather than at the beginning of main( ). The scope of c is the statement controlled by the for.
This program also uses the java.lang.Character “wrapper” class, which not only wraps the primitive char type in an object, but also provides other utilities. Here, the static isLowerCase( ) method is used to detect whether the character in question is a lowercase letter.
Traditional procedural languages like C require that all variables be defined at the beginning of a block so that when the compiler creates a block, it can allocate space for those variables. In Java and C++, you can spread your variable declarations throughout the block, defining them at the point that you need them. This allows a more natural coding style and makes code easier to understand.
[Thinking in Java, 94~]