Repetition and Loops
Do It Again (and Again)
Computers never get bored. Ask one to do something a million times and it will β perfectly, every time. Repetition (also called iteration or looping) is the building block that makes this possible.
Instead of writing:
draw a star
draw a star
draw a star
draw a star
draw a star
we write:
REPEAT 5 TIMES:
draw a star
Shorter, clearer, and if you need 500 stars instead of 5, you change one number instead of copying 495 more lines.
Two Kinds of Loop
Count-controlled β "repeat N times"
Use when you know in advance how many repeats you need:
REPEAT 10 TIMES:
do a star jump
The loop keeps its own count and stops after exactly 10 rounds.
Condition-controlled β "repeat untilβ¦"
Use when the number of repeats depends on what happens:
REPEAT UNTIL the water boils:
keep heating
You cannot know beforehand how many rounds this takes β the loop checks its condition (the same true/false tests from selection) and stops when it becomes true.
| Count-controlled | Condition-controlled | |
|---|---|---|
| Repeats | a fixed number of times | until a condition is met |
| Use when | the count is known in advance | the count depends on events |
| Example | print 20 tickets | keep guessing until correct |
Loops in Flowcharts
In a flowchart, a loop is an arrow that goes back up:
ββββββββββββββββ
βββββΊβ Heat the β
β β water β
β ββββββββ¬ββββββββ
β βΌ
β ββββββββββββββββ
β NO β Boiling? β
ββββββ β
ββββββββ¬ββββββββ
β YES
βΌ
ββββββββββββββββ
β Make tea β
ββββββββββββββββ
Each pass: heat, then test. If not boiling, loop back; if boiling, continue on.
The Infinite Loop Trap
A loop must be able to stop. If its exit condition can never become true, the loop runs forever β an infinite loop, one of the most famous bugs in computing.
Trace this, starting with number = 3:
REPEAT UNTIL number > 10:
subtract 1 from number
3 β 2 β 1 β 0 β β1 β β¦ The number only shrinks, so number > 10 can never be true. The loop never ends and the program freezes.
The fix: make sure something inside the loop moves the condition toward true β here, add 1 instead of subtracting. When debugging a frozen program, a broken loop condition is a prime suspect.
Loops + Patterns = Power
Repetition is where pattern recognition pays off. Spot that all 20 snowmen are drawn the same way, and the whole job becomes:
REPEAT 20 TIMES:
draw body
draw head
add eyes, nose, hat
Three building blocks β sequence, selection, repetition β and you can now describe astonishingly complex behaviour.
Worked Example β A Guessing Game Loop
Condition-controlled loops shine brightest when the number of repeats is genuinely unpredictable. Trace this number-guessing game, where the secret number is 7:
guess β 0
attempts β 0
REPEAT UNTIL guess = 7
INPUT guess
attempts β attempts + 1
ENDREPEAT
OUTPUT "You got it in ", attempts, " tries!"
| Attempt | guess entered | guess = 7? | attempts |
|---|---|---|---|
| 1 | 3 | No | 1 |
| 2 | 9 | No | 2 |
| 3 | 7 | Yes | 3 |
The loop repeats exactly as many times as the player needs β three tries here, but it could just as easily be one or fifteen. That unpredictability is the whole point of choosing a condition-controlled loop over a count-controlled one: nobody could have written REPEAT 3 TIMES in advance, because nobody knew how many guesses this particular game would take until it was actually played.
Two Timings for the Same Condition
There is a subtle design choice hiding inside every condition-controlled loop: does the condition get checked before the first run, or only after? Compare:
WHILE tickets remain > 0 REPEAT
sell a ticket sell a ticket
ENDWHILE UNTIL tickets remain = 0
A WHILE loop checks first β if there are already 0 tickets when the loop begins, the body never runs at all, which is correct behaviour for a sold-out show. A REPEAT...UNTIL loop checks after β the body always runs at least once, even if the condition was already true at the very start. Neither style is universally "better"; the right choice depends on whether the situation can ever legitimately need zero repeats. Getting this choice wrong is a subtle but real source of off-by-one-style bugs, so when designing a loop, always ask: could this legitimately need to run zero times?
Key Words
- Repetition / iteration / loop β running a group of steps more than once
- Count-controlled loop β repeats a fixed number of times
- Condition-controlled loop β repeats until a condition becomes true
- Infinite loop β a loop whose exit condition is never met, so it never stops
Questions & comments
Comments are public and are checked before they appear. Please donβt post your full name, email, phone number, school, or anything else that identifies you. How we use this.
Loading commentsβ¦