AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Again and again

Week 2 · 1.2 Iteration · 3 lessons
3

Trace the loop

Computing 9: Topic 1.2, pp. 25–45
GoalFollow a FOR loop with a trace table, including variables that change inside the loop, and change a loop to do something different.
New words
trace table · a table with a column for each variable and one row for each time round, used to follow an algorithm by handbody · the lines inside a loop that are run each time roundmodify · to change an algorithm, here by editing the loop header or the body
Explanation

To trace a loop, make a table with a column for the counter, a column for every other variable that changes, and a column for the output. Write down the values before the loop starts. Then add one row each time round the loop: update the counter, follow each line of the body in order and write the new values. Stop when the counter passes the end value, then follow the lines after NEXT. Reading the table down the page shows how a variable changes. To modify a loop you decide what must change. The start and end values change how many times it runs and which numbers the counter takes. The lines in the body change what happens each time. Trace the new version with a test value to check it.

weekheightOutput
(before)2
14
28
316
432
(after loop)32Height: 32
Worked examples
Pseudocode
1height ← 2
2FOR week ← 1 TO 4
3    height ← height * 2
4NEXT week
5OUTPUT "Height: ", height
Output
Height: 32
The table above traces a plant that doubles in height each week. The first line sets height to 2 and then the loop changes it four times. The last row is the line after NEXT. Run this check with beans or coins if you like: 2, 4, 8, 16, 32.
roundscore typedOutput
148
2918
324
Input inside a loop. The algorithm is FOR round ← 1 TO 3, then INPUT score and OUTPUT score * 2 inside the loop, then NEXT round. Each time round, it reads a score and shows it doubled. The user types 4, then 9, then 2. A different value is typed on every round, so the score column changes each time.
WishWhat to editNew first line
Seats 11 to 15start and end valuesFOR seat ← 11 TO 15
8 seats numbered from 1end value onlyFOR seat ← 1 TO 8
Show Chair instead of Seatthe body onlyFOR seat ← 1 TO 5 (unchanged)
Modifying a loop. The base loop is FOR seat ← 1 TO 5 with OUTPUT "Seat ", seat. Each wish needs a different part to change. Test the new header: FOR seat ← 11 TO 15 gives Seat 11 to Seat 15, which is 5 values.
Class activity

Paper trace race (no computer). Write a four-line algorithm with a FOR loop on a card, for example a battery that loses 10 each hour, using any numbers you like. Each player draws a trace table on paper and fills it in without help. Then compare tables. If they differ, find the first row where the numbers are different and decide who is right. Swap cards with another pair and repeat. If you have a computer or tablet, an adult can help you check one trace by typing the algorithm in Python.

Practice
1
Draw a trace table and follow the algorithm. What is output?
Pseudocode
1battery ← 100
2FOR hour ← 1 TO 4
3    battery ← battery - 15
4    OUTPUT battery
5NEXT hour
2
Predict the output.
Pseudocode
1FOR n ← 1 TO 6
2    IF n = 2 OR n = 5 THEN
3        OUTPUT "Pop"
4    ELSE
5        OUTPUT n
6    ENDIF
7NEXT n
3
This loop shows how many minutes a pupil practises over three days. Change it for 5 days and 6 minutes per day. Write the new loop and say what the last line of output is.
Pseudocode
1FOR day ← 1 TO 3
2    OUTPUT "Day ", day, ": ", day * 4
3NEXT day
4
Write a FOR loop that outputs 10, 20, 30, 40 and 50. Then trace it to check.