Nested Selection and Iteration
Understand Nested Selection and Iteration for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). Selection and iteration can be nested inside one another to handle more complex logic.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
Nesting means placing one structure inside another. Both selection and iteration can be nested, and the two can be combined.
A nested IF tests a second condition only when the first was True. A nested loop runs its inner loop completely for every single iteration of the outer loop — so an outer loop of 3 containing an inner loop of 4 executes the inner body 3 × 4 = 12 times.
That multiplication is where trace questions are won or lost. Nested loops are used for two-dimensional data: the outer loop moves through rows and the inner loop through the columns of each row. Indentation is essential, because it is the only thing showing which statements belong to which structure.
Revision notes
Nested selection
An IF inside another IF tests a second condition only when the first was True.
This differs from ELSE IF, which tests alternatives at the same level. Use nesting when the second question only makes sense if the first was answered yes.
Nested iteration
The inner loop runs completely for every iteration of the outer loop.
An outer loop of 3 containing an inner loop of 4 executes the inner body 3 × 4 = 12 times. Multiplying the iteration counts gives the total.
Using nested loops
Two-dimensional data needs nested loops: the outer moves through rows, the inner through columns.
Indentation is essential, since it is the only indication of which statements belong to which loop. Each loop needs its own closing keyword.
Key points
- Nesting places one structure inside another.
- A nested IF tests a second condition.
- The inner loop runs fully each outer iteration.
- Multiply the counts for total iterations.
- Nested loops process two-dimensional data.
- Indentation shows the structure.
Worked examples
Example 1
An outer loop runs 5 times and contains an inner loop running 3 times. Work out how many times the inner body executes. [2 marks]
Working
Example 2
Explain why indentation is important in nested structures. [2 marks]
Working
Example 3
State which loop moves through rows and which through columns when processing a 2D array. [2 marks]
Working
Common mistakes
Adding the iteration counts instead of multiplying.
The inner loop runs fully each outer iteration, so multiply.
Not indenting nested structures.
Indentation is the only indication of what belongs where.
Forgetting a closing keyword.
Each nested structure needs its own ENDIF or ENDFOR.
Confusing nested IF with ELSE IF.
Nesting tests a second condition only if the first was True.
Exam tips
- Multiply iteration counts for nested loops.
- Indent each level of nesting clearly.
- Close every structure in the right order.
- Trace nested loops one full inner cycle at a time.
Key terms
- Nesting
- Placing one structure inside another.
- Nested loop
- A loop contained within another loop.
- Outer loop
- The enclosing loop, moving through rows.
- Inner loop
- The contained loop, running fully each outer iteration.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.