大象传媒

Condition controlled iteration

Condition controlled iteration, or repeatedly a section of code until a is met - or no longer met. There are two types of condition controlled iteration:

  • - uses the WHILE and ENDWHILE
  • - uses the statements REPEAT and UNTIL

WHILE loops

WHILE loops test the condition at the beginning of the loop. If the condition is met, the code within the loop is executed before the program loops back to test the condition again. This program would print out a message six times:

count 鈫 0
WHILE count < 6
     OUTPUT 鈥淐oding is cool鈥
     count 鈫 count + 1
ENDWHILE

The WHILE statement defines the start of the loop. The ENDWHILE statement declares the end of the loop. By combining the start and the end, the scope of the statement is identified.

A , in this case count, is used for the condition. The WHILE statement also tests the condition - in this case to see if the value of count is less than six. If the result is TRUE, the code within the loop is executed, then the program loops back to the condition, which is tested again.

The continues until the condition test result is FALSE, at which point the loop ends and the program executes the next line of code in sequence after the loop.

Because the condition is tested at the start of the loop, it is possible for the code within it to never actually be executed. Consider this program:

password 鈫 USERINPUT
WHILE password <> 鈥淏1t3s1z拢鈥
     OUTPUT 鈥淧assword incorrect. Try again.鈥
     password 鈫 USERINPUT
ENDWHILE

The first time the condition is tested, the result may be FALSE if the value of password matches. Because of this, none of the code within the loop will be executed and the program will move onto the next line of code in sequence after the loop.

WHILE loops are particularly useful for validation of user inputs as the code can insist that they retry entering until it is correct.

REPEAT UNTIL loops

REPEAT UNTIL loops function in the same way as WHILE loops, with one major difference - the condition is tested at the end of the loop:

count 鈫 0
REPEAT
     OUTPUT 鈥淐oding is cool鈥
     count 鈫 count + 1
UNTIL count = 10

The REPEAT statement defines the start of the loop. The UNTIL statement tests the condition and declares the end of the statement scope. Because the condition is tested at the end, the code within the loop is always executed at least once, even if the result of the test is FALSE.

DO WHILE loops

DO WHILE loops function in the same way as REPEAT UNTIL loops, with the WHILE condition being tested at the end of the loop:

count 鈫 0 DO OUTPUT 鈥淐oding is cool鈥  
			count 鈫 count + 1 
    WHILE count < 10

The DO statement defines the start of the loop. The WHILE statement tests the condition and declares the end of the statement scope. Because the condition is tested at the end, the code within the loop is always executed at least once, even if the result of the test is FALSE.

NOTE: Some programming languages such as Python do not use end statements but use indents instead.