- Practice
- About 14 min
- 5 practice questions
Output prediction: loops, nested loops and counting problems
Predict the output of loops, count iterations and use a trace table to avoid off-by-one mistakes.
If something looks wrong, please tell us.
The most common programming question in objective papers shows a few lines of pseudocode and asks for the output or for the number of times a line runs. These questions are won by one habit: write a trace table with one column for each variable and one row for each pass of the loop. The problems below are all illustrative.
Method
- List every variable and its starting value.
- Check the loop condition before each pass (for WHILE and FOR) or after each pass (for DO-WHILE).
- Update the variables in the order the code states and write the new row.
- Stop exactly when the condition fails, and note the final values.
- Count rows to get the number of passes. In FOR i = a to b, the body runs b - a + 1 times if b is at least a.
Worked example 1: conditional sum
s = 0; FOR i = 1 TO 6: IF i % 2 == 1 THEN s = s + i * i. Only odd values of i act: 1, 3 and 5. s becomes 1, then 1 + 9 = 10, then 10 + 25 = 35. Even values of i do nothing. Printing s gives 35.
Worked example 2: multiplying loop
x = 3; WHILE x < 100: x = x * 3. After the loop ends, PRINT x. Trace: x = 3 (3 < 100 true) becomes 9; 9 becomes 27; 27 becomes 81; 81 < 100 is true, so it becomes 243. Now 243 < 100 is false and the loop stops. The loop body ran 4 times and the output is 243.
Worked example 3: nested loops
FOR i = 1 TO 4: print i stars on one line. The lines are , , , **. Total stars = 1 + 2 + 3 + 4 = 10. Now count the pairs (i, j) with i from 1 to 5, j from 1 to 5 and i < j. For i = 1 there are 4 values of j; for i = 2, 3; for i = 3, 2; for i = 4, 1; for i = 5, 0. Total = 4 + 3 + 2 + 1 + 0 = 10 pairs. The pairs with i = j number 5**.
Worked example 4: counting passes
n = 50; count = 0; WHILE n > 0: n = n - 7; count = count + 1. Trace of n: 43, 36, 29, 22, 15, 8, 1, -6. After the eighth subtraction n is -6, which is not above 0, so the loop stops. The final values are n = -6 and count = 8. A frequent mistake is to answer 7 by dividing 50 by 7 and rounding down.
Worked example 5: counting downwards
FOR i = 10 DOWN TO 1 STEP 3 takes the values 10, 7, 4 and 1. The next value would be -2, which is below 1, so the loop ends. The body runs 4 times and the sum of the values is 10 + 7 + 4 + 1 = 22.
Worked example 6: DO-WHILE
i = 5; DO PRINT i; i = i + 1; WHILE i < 3. A DO-WHILE tests after the body. The body runs once and prints 5, then i becomes 6 and 6 < 3 is false, so the loop ends. A WHILE loop with the same condition would not have printed anything.
Trace table, check the condition at the right moment, count rows.
Common traps
- Off-by-one: forgetting to include the last value of a FOR loop.
- Testing the condition at the wrong time in DO-WHILE.
- Updating variables in the wrong order inside the body.
- Counting the final failed test as a pass of the loop.
Check your understanding
-
1. How many times does FOR i = 3 TO 11 run its body?
- 8
- 10
- 9
- 11
Show answer
Answer: C. The count is 11 - 3 + 1 = 9.
-
2. x = 2; WHILE x < 50: x = x * 2. What is x when the loop ends?
- 32
- 64
- 50
- 100
Show answer
Answer: B. x takes 2, 4, 8, 16, 32 and then 64. 64 is not less than 50, so the loop stops with x = 64.
-
3. S = 0; FOR i = 1 TO 5: S = S + i * 2. What is S?
- 30
- 15
- 10
- 25
Show answer
Answer: A. 2 x (1 + 2 + 3 + 4 + 5) = 2 x 15 = 30.
-
4. n = 20; count = 0; WHILE n > 0: n = n - 6; count = count + 1. What is count?
- 3
- 4
- 6
- 5
Show answer
Answer: B. n takes 14, 8, 2 and -4. That is 4 subtractions, so count = 4.
-
5. A loop is written with the test after the body. A body that prints 'Hi' with an initially false test prints it how many times?
- For ever
- Never
- Twice
- Once
Show answer
Answer: D. A loop that tests after the body, such as DO-WHILE, always runs once.
Sign in to keep track of the lessons you finish.