Iteration Statements

   

Programmers use iteration statements to control sequences of statements that are repeated according to runtime conditions.

Java supports three types of iteration statements:

  • while

  • do

  • for

while Loops

The while statement tests an expression and, if it's true, executes the next statement or block repeatedly until the expression becomes false. When the variable or expression is false, control is passed to the next statement after the while statement. The syntax for a while loop looks very similar to that of an if statement:

 while (expression)   statement; 

The following example shows a while loop in action:

 boolean done = false; // read characters from the keyboard until an x is typed while (!done) {   int c = System.in.read();   done = (c == 'x'); } 

In this example, the condition evaluates to true initially, so the loop is entered. The loop continues until a specific value of the variable c causes the state of the control expression to change. A while loop will never execute if the condition evaluates to false the first time it is checked.

while loops can execute indefinitely, either intentionally or by accident . It is common to use a loop like the following to run a program until it is terminated externally or by an embedded break statement:

 // an intentional "infinite" loop while (true) {   statement;  } 

do Loops

The do statement is similar to the while statement. In fact, it has a while clause at the end. Like the while expression in the previous section, the expression here must be a Boolean. A do loop executes its statement(s) and then evaluates the while expression. If the while expression is true, execution returns to the do statement and the loop continues to iterate until the expression becomes false. The complete syntax for a do - while loop is as follows :

 do   statement; while (expression) 

The primary reason a programmer chooses to use a do statement instead of a while statement is that the statement will always be executed at least once, regardless of the value of the expression. This is also known as post-evaluation.

for Loops

The most complicated of the iteration statements is the for loop. The for statement gives the programmer the capabilities of each of the other iteration statements. The complete syntax of a for loop is as follows:

 for (initialization; expression; step)   statement; 

The for loop first runs the initialization code (like a do ) and then evaluates the expression (like an if or while ). If the expression is true, the statement is executed and then the step is performed. A for loop will not execute any iterations if the expression is false when it is first evaluated. A for loop can also be written as a while loop as follows:

 initialization; while (expression){   statement;   step; } 

An example of a for loop appears in the following code fragment:

 // for loop that limits a batter to 10 pitches for (int balls=0, strikes=0, pitches=0; (balls<4) && (strikes<3) && (pitches<10); graphics/ccc.gif pitches++) {   pitcher.pitch();   player.swing();   // do something that updates the ball and strike counts   ... } 

This example demonstrates the fact that the initialization clause can include a variable declaration statement that creates and initializes several local variables .

You can only specify a single declaration statement here, so multiple variables can only be declared if they are of a single type. Integer literals are used in this example, but you can also use expressions to initialize for statement variables. If you do use an expression here and it terminates abruptly, the for statement completes abruptly as well. Any variables declared in the initialization clause have scope limited to the for statement block and the expression and statement portions of the for clause. They are no longer accessible after the loop completes executing.

You are not required to provide an expression for any of the three sections of a for loop. You can also use empty statements for any or all of them in situations where the loop is controlled by the statements it executes. An empty statement is a semicolon without any associated expression to evaluate. For example, our previous infinite loop example can be rewritten as:

 // an intentional "infinite" loop for (;;) {   statement;  } 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net