Nested Statements (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

Nested Statements

  • Nested statements involve including one statement within another statement. This can be done with both selection (if/else) and iteration (for/while) statements
  • In programming languages like Python, Java, and Visual Basic, nested statements are often indicated by indenting the inner statement(s) relative to the outer statement(s)

Exam Tip

  • You will not be required to write more than three levels of nested statements

Nested Selection

  • Nested selection is an if statement inside an if statement
  • If the first if statement is true, it will run the if statement which is nested inside
  • Otherwise, it will skip to the "else if" or "else" which is part of that if statement

Pseudocode example:

if a > b then

    if b > c then

        output “a is the largest”

    else

        output “c is the largest”

else

    if a > c then

        output “b is the largest”

    else

        output “c is the largest”

Python example:

if a > b:

    if b > c:

        print("a is the largest")

    else:

        print("c is the largest")

else:

    if a > c:

        print("b is the largest")

    else:

        print("c is the largest")

Java example:

if (a > b) {

if (b > c) {

System.out.println("a is the largest");

} else {

System.out.println("c is the largest");

}

} else {

if (a > c) {

System.out.println("b is the largest");

} else {

System.out.println("c is the largest");

}

}

Visual Basic example:

If a > b Then

If b > c Then

Console.WriteLine("a is the largest")

Else

Console.WriteLine("c is the largest")

End If

Else

If a > c Then

Console.WriteLine("b is the largest")

Else

Console.WriteLine("c is the largest")

End If

End If

Exam Tip

  • Indentation is key so make sure it's clear in your answer which if statement line is part of which. It's more clear when you use end if in the appropriate places, too.

Nested Iteration

Nested iteration refers to a loop inside another loop.

Pseudocode example:

FOR i 1 TO 10

    FOR j 1 TO 5

        OUTPUT "i = ", i, " j = ", j

    END FOR

END FOR

Python example:

for i in range(1, 11):

    for j in range(1, 6):

        print("i = ", i, " j = ", j)

Java example:

for (int i = 1; i <= 10; i++) {

    for (int j = 1; j <= 5; j++) {

        System.out.println("i = " + i + " j = " + j);

    }

}

Visual Basic example:

For i As Integer = 1 To 10

    For j As Integer = 1 To 5

        Console.WriteLine("i = " & i & " j = " & j)

    Next j

Next i

Exam Tip

  • Nested iteration is useful when we need to perform a task for a specific range of values
  • It is important to keep track of the number of nested statements
  • It is important to keep nested statements organized and clear. Use consistent indentation and avoid going too many levels deep, as this can make the code difficult to read and understand

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.