Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)

8.2 Repetition with the While Loop

The repeat structure requires a loop condition and a repeat group. With the while-loop construct, the loop condition is tested first. If the condition is true, the operations in the repeat group are carried out. This continues until the condition evaluates to false.

8.2.1 While-Loop Construct

Figure 8.1 shows a flowchart segment that illustrates the while loop construct. The repeat group consists of the operations in Block1.

Figure 8.1: A flowchart segment with the while-loop construct.

If the condition is true, the operations in Block1 are executed, then the condition is again evaluated, and the operations are carried out if the condition is still true. This continues until the condition changes to false, at which point the loop terminates, and then the control flow continues with the operation that follows the while-loop construct.

In KJP, the while statement is written with the keywords while, do, and endwhile. The repeat group consists of all operations in Block1, which is placed after the do keyword and before the endwhile keyword. The following portion of code shows the general structure for the while-loop construct with the while statement in KJP that corresponds to the portion of flowchart shown in Figure 8.1.

while 〈 condition 〉 do 〈 statements in Block1 〉 endwhile

8.2.2 Loop Condition and Loop Counter

Note that in the while-loop construct, the condition is tested first, and then the repeat group is carried out. If this condition is initially false, the operations in the repeat group are not carried out.

The number of times that the loop is carried out is normally a finite integer value. This implies that the condition will eventually be evaluated to false, that is, the loop will eventually terminate. This condition is sometimes called the loop condition, and it determines when the loop terminates. Only in some very special cases, the programmer can decide to write an infinite loop; this will repeat the operations in the repeat loop forever.

A counter variable has the purpose of storing the number of times (iterations) that some condition occurs in a function. The counter variable is of type integer and is incremented every time some specific condition occurs. The variable must be initialized to a given value.

Note

A loop counter is an integer variable that is incremented every time the operations in the repeat group are carried out. Before starting the loop, this counter variable must be initialized to some particular value.

The following portion of code has a while statement with a counter variable called loop_counter. This counter variable is used to control the number of times the repeat group will be carried out. The counter variable is initially set to 1, and every time through the loop, its value is incremented.

constants integer Max_Num = 15 // constant, maximum number // of times through the loop ... variables integer loop_counter // counter variable ... begin set loop_counter = 1 // initial value of counter while loop_counter < Max_Num do increment loop_counter display "Value of counter: ", loop_counter endwhile ...

The first time the operations in the repeat group are carried out, the loop counter variable loop_counter has a value equal to 1. The second time through the loop, variable loop_counter has a value equal to 2. The third time through the loop, it has a value of 3, and so on. Eventually, the counter variable will have a value equal to the value of Max_Num. When this occurs, the loop terminates.

Категории