Computing · Year 6
Bell.Study
Nested loops and selection
Putting one loop or IF inside another to handle more complicated patterns
- 1
What is a 'nested' loop? A) A loop that is inside another loop B) A loop with no end C) A loop that only runs once D) A loop that is broken
Answer: - 2
True or false? When loops are nested, the inner loop finishes all of its passes before the outer loop moves on. A) True B) False
Answer: - 3
True or false? You are allowed to put an IF (selection) inside a loop. A) True B) False
Answer: - 4
What is a nested loop? A) A loop inside another loop B) A loop that runs once C) A loop with no body D) Two loops side by side
Answer: - 5
An outer loop runs 4 times. Inside it, an inner loop runs 3 times. How many times does the inner loop's body run in total? A) 12 B) 7 C) 4 D) 3
Answer: - 6
Why are nested loops useful? A) They handle 2D patterns like grids and tables B) They make code shorter than a single loop C) They stop the program from running D) They are only used for sound effects
Answer: - 7
A program loops through every pupil in a class and uses an IF to count those over 10 years old. Which describes this? A) Selection inside a loop B) Two nested loops C) No loop at all D) A bug
Answer: - 8
What does this pseudocode print? FOR row FROM 1 TO 2 FOR col FROM 1 TO 3 OUTPUT '*' END FOR OUTPUT new line END FOR A) Two lines of three * characters each B) Two * characters C) Three * characters D) Six lines of one *
Answer: - 9
Look at this code. FOR i FROM 1 TO 3 FOR j FROM 1 TO 2 OUTPUT i, j END FOR END FOR How many lines of output appear? A) 6 B) 3 C) 2 D) 5
Answer: - 10
Order the OUTPUT of this code. FOR i FROM 1 TO 2 FOR j FROM 1 TO 2 OUTPUT i, j END FOR END FOR Put these in order: 2,2, 1,1, 1,2, 2,1
Answer:
Answer key
Nested loops and selection · for parents and teachers
- 1
A loop that is inside another loop
A nested loop is a loop that lives inside another loop. The inner loop runs all the way through every time the outer loop steps once.
- 2
True
Yes. The inner loop completes every pass for one step of the outer loop, then the outer loop steps and the inner loop runs again.
- 3
True
Yes. Putting an IF inside a loop is very common. For example, looping through scores and counting only the ones above 50.
- 4
A loop inside another loop
A nested loop is a loop placed inside another loop.
- 5
12
For each of the 4 outer passes, the inner loop runs 3 times. 4 * 3 = 12 total inner runs.
- 6
They handle 2D patterns like grids and tables
Nested loops are perfect for 2D things - an outer loop for each row and an inner loop for each column.
- 7
Selection inside a loop
A loop holds the IF, so each pupil is checked. This is selection (IF) inside a loop, not nested loops.
- 8
Two lines of three * characters each
Outer loop runs 2 times (2 rows). Inner loop prints '*' 3 times per row. So you get 2 rows of 3 stars each.
- 9
6
The inner loop runs 2 times for each of the 3 outer passes. 3 x 2 = 6 lines of output.
- 10
1,1, 1,2, 2,1, 2,2
When i=1, j is 1 then 2. When i=2, j is 1 then 2. So the order is 1,1 - 1,2 - 2,1 - 2,2.