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.
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.
round
score typed
Output
1
4
8
2
9
18
3
2
4
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.
Wish
What to edit
New first line
Seats 11 to 15
start and end values
FOR seat ← 11 TO 15
8 seats numbered from 1
end value only
FOR seat ← 1 TO 8
Show Chair instead of Seat
the body only
FOR 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?
battery starts at 100. Hour 1: 85. Hour 2: 70. Hour 3: 55. Hour 4: 40. The output is 85, 70, 55 and 40, each on its own line.
2
Predict the output.
Pseudocode
1FOR n ← 1 TO 62 IF n = 2 OR n = 5 THEN
3 OUTPUT "Pop"4 ELSE
5 OUTPUT n
6 ENDIF
7NEXT n
n = 1: not 2 or 5, so 1. n = 2: Pop. n = 3: 3. n = 4: 4. n = 5: Pop. n = 6: 6. Output: 1, Pop, 3, 4, Pop, 6, each on its own line.
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 32 OUTPUT "Day ", day, ": ", day * 43NEXT day
FOR day ← 1 TO 5 / OUTPUT "Day ", day, ": ", day * 6 / NEXT day. The last line is Day 5: 30. Both the end value and the body had to change.
4
Write a FOR loop that outputs 10, 20, 30, 40 and 50. Then trace it to check.
FOR k ← 1 TO 5 / OUTPUT k * 10 / NEXT k. Trace: k = 1 gives 10, 2 gives 20, 3 gives 30, 4 gives 40, 5 gives 50. The counter goes up by 1 each time, so the loop uses a calculation to make steps of 10.