Condition controlled iteration
Condition controlled iteration, or indefinite iterationA loop that repeats until a condition is either met or no longer met. This is usually implemented using a WHILE or REPEAT UNTIL loop. repeatedly executionThe process of a program being run on a computer. a section of code until a conditionIn computing, this is a statement that is either true or false. A computation depends on whether a condition equates to true or false. is met - or no longer met. There are two types of condition controlled iteration:
- while loopsA WHILE loop code is repeated based on a certain condition and is checked at the beginning of each iteration. - uses the statementThe smallest element of a programming language which expresses an action to be carried out. WHILE and ENDWHILE
- repeat until loopA REPEAT UNTIL loop code is repeated based on a certain condition and is checked at the end of each iteration. - 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 variableA memory location within a computer program where values are stored., 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 iterationThe repetition of a block of statements within a computer program. 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 dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions. 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.