FOR…TO…NEXT loop (7d)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

FOR…TO…NEXT loop (7d)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How many times does this loop run? FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
A 5 times
B 4 times
2. Which line correctly ends a loop that begins FOR Index ← 1 TO 8?
A NEXT Index
B ENDFOR Index
3. Inside FOR Count ← 1 TO 10, what do you need to write to add 1 to Count each time round?
A Count ← Count + 1 on the last line
B Nothing, the loop does it automatically
4. What is the first value of Count in FOR Count ← 3 TO 7?
A 3
B 4
5. What does STEP 2 do in FOR Count ← 1 TO 9 STEP 2?
A The loop runs exactly 2 times
B Count goes up by 2 each time: 1, 3, 5, 7, 9

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1FOR Count ← 1 TO 5 AFOR Count ← 2 TO 10 STEP 2
2NEXT Count BNEXT does not name the loop variable. It should be NEXT Count.
3A FOR loop with STEP 2 jumps by 2 each time, so Count takes the values 2, 4, 6, 8, 10 CStarts a loop where Count is 1, 2, 3, 4, 5, so the lines inside repeat 5 times
4A complete FOR loop that outputs the numbers 1 to 3 using the loop variable DFOR Count ← 1 TO 3 OUTPUT Count NEXT Count
5FOR Count ← 1 TO 5 OUTPUT Count NEXT EMarks the end of the FOR loop and names the loop variable that is increased by 1
6FOR Count ← 1 TO 5 OUTPUT Count Count ← Count + 1 NEXT Count FCount is increased inside the loop by hand. A FOR loop changes the loop variable automatically, so this line is wrong and should be removed.

Definitions

FOR Count ← 1 TO 10

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
FOR Count ← 1 __ 5 OUTPUT Count NEXT Count
2.
___ Count ← 1 TO 10 OUTPUT Count * 2 NEXT Count
3.
FOR Total ← 1 TO 8 OUTPUT Total NEXT _____
4.
FOR Count ← 0 TO 10 ____ 2 OUTPUT Count NEXT Count
5.
FOR Count ← 10 TO 1 STEP __ OUTPUT Count NEXT Count

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Total ← 0
B FOR Count = 1 TO 5
C Total ← Total + Count
D NEXT Count
E OUTPUT Total
Question 2
A FOR Count ← 1 TO 5
B OUTPUT Count
C NEXT Counter
D OUTPUT "Done"
Question 3
A FOR Num ← 1 TO 4
B OUTPUT Num * 2
C ENDFOR
Question 4
A Total ← 0
B FOR Count ← 1 TO 4
C OUTPUT Count
D Count ← Count + 1
E NEXT Count
F OUTPUT Total
Question 5
A Total ← 0
B FOR Count ← 1 TO 10 STEP 2
C Total ← Total + Count
D NEXT
E OUTPUT Total

Answer Key

FOR…TO…NEXT loop (7d)
Section A: Quick Questions
Q1
A — 5 times
Q2
A — NEXT Index
Q3
B — Nothing, the loop does it automatically
Q4
A — 3
Q5
B — Count goes up by 2 each time: 1, 3, 5, 7, 9
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 5 → Starts a loop where Count is 1, 2, 3, 4, 5, so the lines inside repeat 5 times
Q2
NEXT Count → Marks the end of the FOR loop and names the loop variable that is increased by 1
Q3
A FOR loop with STEP 2 jumps by 2 each time, so Count takes the values 2, 4, 6, 8, 10 → FOR Count ← 2 TO 10 STEP 2
Q4
A complete FOR loop that outputs the numbers 1 to 3 using the loop variable → FOR Count ← 1 TO 3 OUTPUT Count NEXT Count
Q5
FOR Count ← 1 TO 5 OUTPUT Count NEXT → NEXT does not name the loop variable. It should be NEXT Count.
Q6
FOR Count ← 1 TO 5 OUTPUT Count Count ← Count + 1 NEXT Count → Count is increased inside the loop by hand. A FOR loop changes the loop variable automatically, so this line is wrong and should be removed.
Section C: Definitions
Q1
FOR Count ← 1 TO 10 — Repeats the lines up to NEXT Count a fixed number of times. Count starts at 1, goes up automatically, and both the start and end values are included. STEP changes the jump.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
FOR Count ← 1 TO 10 OUTPUT Count * 2 NEXT Count
Q3
FOR Total ← 1 TO 8 OUTPUT Total NEXT Total
Q4
FOR Count ← 0 TO 10 STEP 2 OUTPUT Count NEXT Count
Q5
FOR Count ← 10 TO 1 STEP -1 OUTPUT Count NEXT Count
Section E: Spot the Error
Q1
B — The start value of a FOR loop is given with the assignment arrow, not an equals sign. It should be FOR Count ← 1 TO 5.
Q2
C — NEXT must name the same loop variable that the FOR line uses. The loop variable is Count, so it should be NEXT Count.
Q3
C — Cambridge pseudocode ends a FOR loop with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Num.
Q4
D — The loop variable goes up automatically, so it must not be changed by hand inside the loop. This line also means Total is never added to. It should be Total ← Total + Count.
Q5
D — NEXT must be followed by the name of the loop variable. It should be NEXT Count.