Topic 8 ยท Programming ยท 8.11

Nested Statements Explained Simply

A nested statement is one programming statement placed inside another. It allows a program to make a second decision only after the first condition has been met.

Invitation

What happens when one decision is not enough?

Sometimes a program must make a decision, then make another decision inside the first one.

A nested statement is a statement placed inside another statement. A common example is an IF statement inside another IF statement.

First check one condition. Then, only if needed, check another.
Figure 1
Two stages of checking
Is the username correct?
โ†“ YES
Is the password correct?
โ†“ YES
Allow access
Big Idea

The inner statement only runs when it is reached

The outer statement is checked first. If its condition is false, the program skips everything inside it.

If the outer condition is true, the program reaches the inner statement and checks its condition.

Outer condition first. Inner condition second.
Figure 2
The order of execution
Outer IF
โ†“ TRUE
Inner IF
โ†“ TRUE
Run the inner code
FutureLogic Bridge

Nested statements are like boxes inside boxes

Look at Figure 3. The large cyan box is the outer IF statement. The program checks this condition first.

If the age check is true, the program enters the large box and reaches the smaller gold box. That inner box contains the second decision.

The action at the bottom can only run after the program has passed through both boxes.

Follow the numbers: 1. Check the outer box. 2. Enter the inner box. 3. Run the action.
Figure 3
Follow the nested boxes
1Outer IF: Age โ‰ฅ 16
2Inner IF: Permission = TRUE
3  OUTPUT "Access granted"
The inner box is only reached if the outer condition is true.
Worked Example

Check age, then check permission

The program first checks whether the user is at least 16. Only then does it check whether permission has been given.

PseudocodeNested IF
IF Age >= 16 THEN
  IF Permission = TRUE THEN
    OUTPUT "Access granted"
  ELSE
    OUTPUT "Permission needed"
  ENDIF
ELSE
  OUTPUT "Too young"
ENDIF
PythonSame structure
if age >= 16:
    if permission == True:
        print("Access granted")
    else:
        print("Permission needed")
else:
    print("Too young")

Follow the decisions

Age = 17The outer condition is true.
Permission = TRUEThe inner condition is true.
Access grantedThe inner output runs.
The second condition is checked only because the first condition was true.
Exam Tip

Use indentation to show what is inside

Indentation makes the code match Figure 3. The outer IF begins at the left. The inner IF moves to the right because it sits inside the outer box. Its output moves further right because it sits inside both boxes.

Match the code to the diagram:
1. Outer box = first indentation level.
2. Inner box = move right once.
3. Inner action = move right again.

In an exam, clear indentation helps the examiner see which ELSE and ENDIF belong to each IF statement.

Common Mistake

Matching an ELSE with the wrong IF

When statements are nested, students sometimes attach an ELSE to the wrong IF statement.

Incorrect idea: Assume every ELSE belongs to the first IF.
Correct: โ€œUse indentation to match each ELSE with the IF at the same level.โ€

Also remember that the inner statement may never run if the outer condition is false.

Summary

The nested statement pattern

Outer statementThe first condition is checked.
Inner statementA second statement sits inside the first.
OrderThe outer condition is always checked first.
SkipIf the outer condition is false, the inner statement is skipped.
IndentationShows which statements belong together.
PurposeAllows more detailed decisions.