Loop Lab
WHILE and FOR, one iteration at a time
Your mission
Loops let a program repeat itself. Step through a WHILE loop and a FOR loop with a live trace table, bend the rules to change how often they repeat, trap a runaway infinite loop, then predict, count and fix loops of your own.
Explore
Step through the WHILE loop and the FOR loop, one line at a time, and watch the trace table fill in.
count <= 3?| Value of count | Condition (count <= 3) | Action |
|---|
Press Step to run the program one line at a time.
number =–Press Step to run one iteration at a time.
Repetition lets a program execute instructions more than once, without writing them out again and again. Step through a WHILE loop and a FOR loop, one line at a time, and watch the trace table fill in.
0 of 6 discoveries made
Experiment
Build your own loops: change the start, the condition and the update, try a range and a list, and trap an infinite loop.
Loop experiments
Try each one. They tick themselves off.
0 of 6 experiments done
Challenge
Six loops: predict the output, count the iterations, choose WHILE or FOR, and fix a loop that never ends.
Loop cheat sheet
- WHILE checks its condition before every iteration
- The body runs while the condition is TRUE
- Something must change, or the loop never ends
- FOR takes each value of a sequence in turn
range(1, 4)→ 1, 2, 3 (stops before 4)
Finish all 6 loops to finish this stage.
Understand
Here is what the loops were showing you, in the words you need for the exam.
Repetition: why programs need loops
Repetition allows instructions to be executed more than once. Loops avoid writing identical instructions repeatedly, and are used for repeated calculations, checking values and processing collections of data, such as a weather station reading the temperature again and again. A loop must have a way of determining whether another repetition is required.
Key takeaway: loops repeat a set of instructions until a stopping condition is met.
The WHILE loop
A while loop checks its condition before every iteration. Its body executes while the condition is TRUE. When the condition becomes FALSE, execution continues after the loop. Output: 1, 2, 3.
Tracing it step by step
| Value of count | count <= 3 | Action |
|---|---|---|
| 1 | True | Print 1, increase to 2 |
| 2 | True | Print 2, increase to 3 |
| 3 | True | Print 3, increase to 4 |
| 4 | False | Exit loop |
The condition is evaluated again after each completed iteration.
The FOR loop
A for loop iterates over a sequence of values. Each iteration assigns the next value to the loop variable. It is convenient when processing a known collection or range. range(1, 4) starts at 1 and stops before 4, so the values are 1, 2 and 3.
Infinite loop warning
A while loop can become infinite if its condition never becomes FALSE. In while count <= 3, if count never changes, the loop repeats forever. To stop the loop, the variable must change, for example count = count + 1.
WHILE or FOR?
| WHILE loop | FOR loop | |
|---|---|---|
| Repeats | According to a condition | Over the values in a sequence |
| Often used when | The number of iterations is initially unknown | You have a known range or collection |
| Watch out for | How the condition changes (or it may never end) | Where the range stops |
| Example | Keep reading the sensor while there are more readings | Print each temperature in a list |
Words to know
- Repetition
- Executing instructions more than once, using a loop.
- Iteration
- One run of the loop body.
- Condition
- A TRUE/FALSE test that decides whether the loop repeats.
- Loop variable
- The variable a FOR loop sets to each value in turn.
- Infinite loop
- A loop whose condition never becomes FALSE, so it never stops.
- range(a, b)
- The whole numbers from a up to, but not including, b.
Exam tip
When asked for the output of a loop, draw a trace table: one row per check of the condition, with the variable's value, TRUE/FALSE and what happens. Remember the final check that is FALSE (count is 4, not 3, when the loop ends), and that range never includes its stop value.