Pseudocode FOR ... TO ... NEXT loop

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

Pseudocode FOR ... TO ... NEXT loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts a loop that repeats 5 times in Cambridge pseudocode?
A FOR Count ← 1 UNTIL 5
B FOR Count ← 1 TO 5
2. Which line correctly closes the loop FOR Count ← 1 TO 10?
A NEXT Count
B ENDFOR Count
3. A FOR loop starts with FOR Count ← 1 TO 10. What does the NEXT Count line do?
A It adds 1 to Count and goes back to the top of the loop
B It stops the loop straight away
4. How many times does the loop body run? FOR Count ← 1 TO 10
A 9 times
B 10 times
5. Which FOR loop is written correctly?
A FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
B FOR Count ← 1 TO 10 OUTPUT Count NEXT Total

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 AThe statement inside the loop is not indented. The loop body should be indented, e.g. 3 spaces before OUTPUT Count.
2NEXT Count BFOR Number ← 1 TO 10 OUTPUT "Hello" NEXT Number
3A FOR loop that counts from 1 to 3 and outputs the counter each time, with the loop body indented CStarts a count-controlled loop where Count begins at 1 and goes up to 5, so the loop body repeats 5 times
4A FOR loop that repeats an action exactly 10 times, using the same variable name after NEXT DFOR Count ← 1 TO 3 OUTPUT Count NEXT Count
5FOR Count ← 1 TO 5 OUTPUT Count NEXT i ENEXT uses a different variable name (i) from the one in the FOR line (Count). It should be NEXT Count.
6FOR Count ← 1 TO 5 OUTPUT Count NEXT Count FMarks the end of the FOR loop and adds 1 to Count before the loop checks whether to repeat again

Definitions

FOR Count ← 1 TO 10

Fill in the Blanks

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

1.
___ Count ← 1 TO 10 OUTPUT Count NEXT Count
2.
FOR Count ← 1 TO 10 OUTPUT Count ____ Count
3.
FOR Num ← 1 __ 5 OUTPUT "Hi" NEXT Num
4.
FOR Count ← 1 TO _ // repeat 3 times OUTPUT "Go" NEXT Count
5.
FOR Num ← 1 TO 4 OUTPUT Num * 2 NEXT ___

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 Total
E OUTPUT Total
Question 2
A FOR Count ← 1 UNTIL 10
B OUTPUT Count
C NEXT Count
Question 3
A Total ← 0
B FOR Number ← 5 TO 1
C INPUT Mark
D Total ← Total + Mark
E NEXT Number
Question 4
A OUTPUT "Start"
B FOR Count ← 1 TO 3
C OUTPUT Counter * 2
D NEXT Count
Question 5
A Total ← 0
B FOR Count ← 1 TO 10
C Total = Total + Count
D NEXT Count

Answer Key

Pseudocode FOR ... TO ... NEXT loop
Section A: Quick Questions
Q1
B — FOR Count ← 1 TO 5
Q2
A — NEXT Count
Q3
A — It adds 1 to Count and goes back to the top of the loop
Q4
B — 10 times
Q5
A — FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 5 → Starts a count-controlled loop where Count begins at 1 and goes up to 5, so the loop body repeats 5 times
Q2
NEXT Count → Marks the end of the FOR loop and adds 1 to Count before the loop checks whether to repeat again
Q3
A FOR loop that counts from 1 to 3 and outputs the counter each time, with the loop body indented → FOR Count ← 1 TO 3 OUTPUT Count NEXT Count
Q4
A FOR loop that repeats an action exactly 10 times, using the same variable name after NEXT → FOR Number ← 1 TO 10 OUTPUT "Hello" NEXT Number
Q5
FOR Count ← 1 TO 5 OUTPUT Count NEXT i → NEXT uses a different variable name (i) from the one in the FOR line (Count). It should be NEXT Count.
Q6
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count → The statement inside the loop is not indented. The loop body should be indented, e.g. 3 spaces before OUTPUT Count.
Section C: Definitions
Q1
FOR Count ← 1 TO 10 — A count-controlled loop that repeats the indented statements a fixed number of times, with Count going from 1 to 10. NEXT Count closes the loop and moves Count up by 1.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Q2
FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Q3
FOR Num ← 1 TO 5 OUTPUT "Hi" NEXT Num
Q4
FOR Count ← 1 TO 3 // repeat 3 times OUTPUT "Go" NEXT Count
Q5
FOR Num ← 1 TO 4 OUTPUT Num * 2 NEXT Num
Section E: Spot the Error
Q1
D — NEXT must use the same variable as the FOR line. The loop counter is Count, so the line should be NEXT Count.
Q2
A — A FOR loop uses the keyword TO between the start and end values. UNTIL belongs to REPEAT ... UNTIL. It should be FOR Count ← 1 TO 10.
Q3
B — A FOR loop counts up from the start value to the end value. With 5 TO 1 the start is bigger than the end, so the loop would not run. It should be FOR Number ← 1 TO 5.
Q4
C — The loop variable is Count, so the body must use Count. Counter is a different variable that has no value. It should be OUTPUT Count * 2.
Q5
C — Cambridge pseudocode uses the assignment arrow ←, not =. The line should be Total ← Total + Count.