Condition-controlled iteration
condition-controlled 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 executes 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 three types of condition-controlled iteration:
- while loop - uses the statementThe smallest element of a programming language which expresses an action to be carried out.
while repeat - repeat loop - uses the statements
repeat until - do loop – uses the statements
do loopordo while
While condition-controlled 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 programSequences of instructions for a computer. loops back to test the condition again. This program would print out a message six times:
set count = 0 while count < 6 print “Coding is cool” count = count + 1 repeatThe while statement defines the start of the loop. The repeat statement declares the end of the loop. 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 6. 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 iteration continues until the condition test result is FALSE. Once a rogue valueA value that falls outside the range of the TRUE expression of a loop. outside of the condition has been found it will terminate the loop and the program will execute 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:
set count = 6 while count < 6 print “Coding is cool” count = count + 1 repeatThe first time the condition is tested, the result will be FALSE, as the value of count is not less than 6. 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.
Repeat condition-controlled loops
Repeat loops function in the same way as while loops, with one major difference - the condition is tested at the end of the loop:
set count = 0 repeat print “Coding is cool” count = count + 1 until count = 10The repeat statement defines the start of the loop. The until statement tests the condition. 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 loops
A do while construct works in the same way as a repeat until construct and a do loop construct works in the same way as a while repeat construct.
The condition for a do construct is placed at the end of the construct, for example do … until x > 6 or do … while x < 6.
Algorithms quoted by Eduqas are unlikely to use ‘do’ constructs.