Repeating Actions

Test yourself on Iteration (Loops) with exam style questions, marked instantly.

Flashcards that come back just before you would forget them.

A revision plan that remembers what you know and shows what to do next.

See which topics you have nailed and which need another look.

Quiz yourself on Iteration (Loops)Get your flashcardsBuild my revision planTrack my progress

Repeating Actions

Iteration means repetition. Imagine writing "I will not be late" 100 times - boring! A loop does it for you. FOR loops repeat a set number of times (write it 100 times). WHILE loops repeat while a condition is true (keep writing until the teacher says stop). An UNTIL loop (REPEAT UNTIL on AQA, DO UNTIL on OCR) always runs at least once before checking. Loops are the power tools of programming!

Three loops drawn as flowcharts, one above the other. FOR: i is set to 1 and i <= 5 is tested. YES prints i, adds 1 to i and returns to the test. NO ends. WHILE: score < 10 is tested first. YES runs score = score + 1 and returns to the test. NO ends, so the body can run zero times. UNTIL: the body PRINT "try again" runs first, then answer = yes is tested. NO returns to the body. YES ends, so the body runs at least once.

FOR repeats a counted number of times. WHILE tests before the body, so it runs zero or more times. UNTIL tests after the body, so it runs at least once. The loop that tests at the end is REPEAT UNTIL on AQA and DO UNTIL on OCR. Here are the three loops in OCR style. The UNTIL loop adds an input line so that answer can change.

for i = 1 to 5
    print(i)
next i

score = 0
while score < 10
    score = score + 1
endwhile

do
    print("try again")
    answer = input("Again?")
until answer == "yes"

AQA style: FOR i ← 1 TO 5 ... ENDFOR, WHILE ... ENDWHILE and REPEAT ... UNTIL .... Edexcel does not set a strict pseudocode style.

Make this stick

Signing in gives access to all PrepWise features.

PrepWise is a trading name of A3M Quality Solutions Limited, registered in England and Wales, company number 14959172. Registered office: 202 Stroud Road, Gloucester, GL1 5LA. ICO registration ZC267069.