Nested loops in pseudocode

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

Nested loops in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A FOR loop from 1 TO 3 contains an inner FOR loop from 1 TO 4. How many times does the inner loop body run in total?
A 7
B 12
2. Which statement describes a nested loop correctly?
A The inner loop runs completely for every pass of the outer loop
B The inner loop runs once, then the outer loop runs completely
3. Which pseudocode correctly closes two nested FOR loops (Row outside, Column inside)?
A NEXT Column NEXT Row
B NEXT Row NEXT Column
4. How many spaces should the inner FOR loop be indented inside the outer FOR loop?
A The same as the outer FOR
B Four spaces more than the outer FOR
5. An outer loop FOR Row ← 1 TO 5 contains an inner loop FOR Column ← 1 TO 2. How many times does the outer loop run?
A 5 times
B 10 times

Match the Pairs

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

Left columnRight column
1FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Column NEXT Row AThe NEXT statements are in the wrong order. The inner loop must be closed first, so NEXT Column comes before NEXT Row.
2NEXT Column NEXT Row BFOR Row ← 1 TO 2 FOR Column ← 1 TO 3 OUTPUT Column NEXT Column NEXT Row
3The inner loop is indented four more spaces than the outer loop, and its body four more again CA nested loop: the inner Column loop runs 4 times for each of the 3 Row passes, so OUTPUT runs 3 x 4 = 12 times
4Total passes = outer passes x inner passes, so an outer loop of 5 and an inner loop of 2 give 10 passes DEach loop closes with its own NEXT, the inner loop first and then the outer loop
5FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row EThe inner loop has no NEXT Column. Each loop needs its own NEXT, so NEXT Column must be added before NEXT Row.
6FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row NEXT Column FFOR Row ← 1 TO 5 FOR Column ← 1 TO 2 OUTPUT "*" NEXT Column NEXT Row

Definitions

nested loop

Fill in the Blanks

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

1.
FOR Row ← 1 TO 3 ___ Column ← 1 TO 4 OUTPUT Row * Column
2.
OUTPUT Row * Column ____ Column NEXT Row
3.
FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT ______
4.
OUTPUT Row * Column NEXT Column NEXT ___
5.
Rows ← 3 Cols ← 4 Total ← Rows _ Cols

Spot the Error

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

Question 1
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 4
C Total = Total + 1
D NEXT Column
E NEXT Row
Question 2
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 2
C OUTPUT Row * Count
D NEXT Column
E NEXT Row
Question 3
A FOR Row ← 1 UNTIL 5
B FOR Column ← 1 TO 3
C OUTPUT Row, Column
D NEXT Column
E NEXT Row
Question 4
A FOR Row ← 1 TO 2
B FOR Column = 1 TO 4
C OUTPUT "#"
D NEXT Column
E NEXT Row
Question 5
A FOR Row ← 1 TO 3
B FOR Row ← 1 TO 3
C OUTPUT Row + Column
D NEXT Column
E NEXT Row

Answer Key

Nested loops in pseudocode
Section A: Quick Questions
Q1
B — 12
Q2
A — The inner loop runs completely for every pass of the outer loop
Q3
A — NEXT Column NEXT Row
Q4
B — Four spaces more than the outer FOR
Q5
A — 5 times
Section B: Match the Pairs
Q1
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Column NEXT Row → A nested loop: the inner Column loop runs 4 times for each of the 3 Row passes, so OUTPUT runs 3 x 4 = 12 times
Q2
NEXT Column NEXT Row → Each loop closes with its own NEXT, the inner loop first and then the outer loop
Q3
The inner loop is indented four more spaces than the outer loop, and its body four more again → FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 OUTPUT Column NEXT Column NEXT Row
Q4
Total passes = outer passes x inner passes, so an outer loop of 5 and an inner loop of 2 give 10 passes → FOR Row ← 1 TO 5 FOR Column ← 1 TO 2 OUTPUT "*" NEXT Column NEXT Row
Q5
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row → The inner loop has no NEXT Column. Each loop needs its own NEXT, so NEXT Column must be added before NEXT Row.
Q6
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Row NEXT Column → The NEXT statements are in the wrong order. The inner loop must be closed first, so NEXT Column comes before NEXT Row.
Section C: Definitions
Q1
nested loop — A loop placed inside another loop. The inner loop runs completely for every pass of the outer loop, so total passes = outer passes x inner passes.
Section D: Fill in the Blanks
Q1
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Row * Column
Q2
OUTPUT Row * Column NEXT Column NEXT Row
Q3
FOR Column ← 1 TO 4 OUTPUT Row * Column NEXT Column
Q4
OUTPUT Row * Column NEXT Column NEXT Row
Q5
Rows ← 3 Cols ← 4 Total ← Rows * Cols
Section E: Spot the Error
Q1
C — Assignment in pseudocode uses ←, not =. The line should be Total ← Total + 1 so the value is stored in Total on every pass of the inner loop.
Q2
C — Count is not a loop variable here and has not been declared. The inner loop variable is Column, so the line should be OUTPUT Row * Column.
Q3
A — A FOR loop sets its range with TO, not UNTIL (UNTIL belongs to REPEAT). The line should be FOR Row ← 1 TO 5.
Q4
B — A FOR loop sets its starting value with the assignment arrow ←, not =. The line should be FOR Column ← 1 TO 4.
Q5
B — The inner loop reuses the outer loop's variable Row, so the two loops would clash. The inner loop needs its own variable: FOR Column ← 1 TO 3.