External Exam Download Resources Web Applications Games Recycle Bin

Iteration

Iteration (also known as 'looping') can be used to repeat instructions. There are three types of loops:

post-test loops

REPEAT
  statements
UNTIL condition

pre-tested loops

WHILE condition
  statements
ENDWHILE

counted loops

FOR count = startVal TO endVal
  statements
NEXT count
ENDFOR

There is no 'repeat until' loop in Python, because a properly constructed 'while' loop can do the same. The closest to coding a 'repeat until' loop in Python is to use a break.

From the syllabus: "In Digital Solutions, using BREAK to force exit a loop is considered ineffective because it disrupts loop behaviour and overrides conditions. This also affects maintainability as it impacts the readability of the algorithm or code":

REPEAT UNTIL (post-test loops).py

x = 1
while True:
  print(x) #body of loop, post-test will execute 1 or more times
  x = x + 1
  if x > 3: #test or control condition comes after execution of loop
     break


WHILE ENDWHILE (pre-tested loops).py

x = 1
while x <= 3: #test or control condition comes before execution of loop
  print(x) #body of loop, pre-tested may execute 0 or more times
  x = x + 1


FOR NEXT ENDFOR (counted loops).py

for x in range(1,4,+1): #controlled looping (start val, stop val, increment val)
  print(x)