Nested selection and iteration
Selection: IF, ELSE IF, and ELSE
Before looking at nested structures, it is important to understand the full form of selection. An IF statement runs a block of code only if a condition is true. Adding ELSE IF (written elif in Python) chains additional conditions that are only checked if the ones above were false. ELSE provides a final default branch that runs when no condition above was true.
IF score >= 70 THEN
OUTPUT "Distinction"
ELSE IF score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Each branch is mutually exclusive - only one branch executes per run. ELSE IF saves you from writing separate, overlapping IF statements, and ELSE ensures no case is missed. This single-level selection is the foundation; nesting places one complete IF structure inside another.
Nesting
Nesting means placing one structure entirely inside another of the same or different type. The inner structure only executes when the outer condition allows it. Each level of nesting is indented further - indentation is essential for reading nested code correctly and avoiding misplaced closing keywords such as ENDIF or ENDFOR.
A nested IF places a second IF block inside the body of an outer IF. The inner IF is only reached if the outer condition is true - the outer IF acts as a gate. Any code after the inner IF (but still inside the outer IF body) runs whenever the outer condition is true, regardless of the inner condition.
AQA pseudocode:
IF GameWon THEN
IF Score > HighScore THEN
OUTPUT "New high score!"
ENDIF
OUTPUT "Well done!"
ENDIF
# Python — nested selection
game_won = True
score = 950
high_score = 800
if game_won: # outer IF — checked first
if score > high_score: # inner IF — only reached if game_won is True
print("New high score!")
print("Well done!") # runs whenever game_won is True
' VB.NET — nested selection
Dim gameWon As Boolean = True
Dim score As Integer = 950
Dim highScore As Integer = 800
If gameWon Then ' outer IF — checked first
If score > highScore Then ' inner IF — only reached if gameWon is True
Console.WriteLine("New high score!")
End If
Console.WriteLine("Well done!") ' runs whenever gameWon is True
End If
// C# — nested selection
bool gameWon = true;
int score = 950;
int highScore = 800;
if (gameWon) // outer IF — checked first
{
if (score > highScore) // inner IF — only reached if gameWon is true
{
Console.WriteLine("New high score!");
}
Console.WriteLine("Well done!"); // runs whenever gameWon is true
}
A nested loop places one loop entirely inside the body of an outer loop. For every single iteration of the outer loop, the inner loop runs completely from start to finish. Total executions of the inner body = outer iterations × inner iterations. This structure is essential for working through combinations - for example, every row and column in a grid.
AQA pseudocode (WHILE containing FOR, as per AQA specification):
lives ← 3
WHILE lives > 0
FOR i ← 1 TO 3
OUTPUT "Round " + i
ENDFOR
lives ← lives - 1
ENDWHILE
# Python — nested iteration (WHILE containing FOR)
lives = 3
while lives > 0: # outer WHILE loop — condition-controlled
for i in range(1, 4): # inner FOR loop — runs completely each outer iteration
print("Round", i)
lives = lives - 1
# Total lines printed: 3 outer iterations x 3 inner = 9 lines
' VB.NET — nested iteration (WHILE containing FOR)
Dim lives As Integer = 3
Do While lives > 0 ' outer WHILE loop — condition-controlled
For i As Integer = 1 To 3 ' inner FOR loop — runs completely each outer iteration
Console.WriteLine("Round " & i)
Next i
lives = lives - 1
Loop
' Total lines printed: 3 outer iterations x 3 inner = 9 lines
// C# — nested iteration (WHILE containing FOR)
int lives = 3;
while (lives > 0) // outer WHILE loop — condition-controlled
{
for (int i = 1; i <= 3; i++) // inner FOR loop — runs completely each outer iteration
{
Console.WriteLine("Round " + i);
}
lives = lives - 1;
}
// Total lines printed: 3 outer iterations x 3 inner = 9 lines
Key Takeaways
- ELSE IF chains alternative conditions at the same level; only one branch executes. ELSE is the final default when no condition above was true.
- Nesting means placing one structure completely inside the body of another - the inner structure is only reached when the outer condition allows it.
- In nested selection, the inner IF is only evaluated when the outer condition is true - the outer IF acts as a gate before the inner check.
- In nested iteration, the inner loop runs completely for every single outer iteration. Total inner body executions = outer iterations x inner iterations.
- Indentation is critical for nested structures - each level is indented further, and every inner structure must be fully closed before the outer structure closes.