Year 11 Exam  >  Year 11 Notes  >  Computer for GCSE/IGCSE  >  Nested Statements

Nested Statements | Computer for GCSE/IGCSE - Year 11 PDF Download

Nested Statements

  • Nested statements involve incorporating one statement within another. This can be achieved with both selection (if/else) and iteration (for/while) statements.
  • In programming languages like Python, Java, and Visual Basic, nested statements are typically represented by indenting the inner statement(s) in relation to the outer statement(s).

Nested Selection

  • Nested selection comprises an if statement enclosed within another if statement.
  • If the initial if statement is true, it will execute the nested if statement.
  • Otherwise, it will proceed to the "else if" or "else" segment associated with 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

Nested Iteration

Nested iteration involves a loop contained within 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

The document Nested Statements | Computer for GCSE/IGCSE - Year 11 is a part of the Year 11 Course Computer for GCSE/IGCSE.
All you need of Year 11 at this link: Year 11
92 docs|30 tests

Top Courses for Year 11

92 docs|30 tests
Download as PDF
Explore Courses for Year 11 exam

Top Courses for Year 11

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Nested Statements | Computer for GCSE/IGCSE - Year 11

,

practice quizzes

,

Sample Paper

,

shortcuts and tricks

,

Objective type Questions

,

Nested Statements | Computer for GCSE/IGCSE - Year 11

,

Exam

,

mock tests for examination

,

Extra Questions

,

Semester Notes

,

Free

,

Viva Questions

,

Summary

,

video lectures

,

MCQs

,

Nested Statements | Computer for GCSE/IGCSE - Year 11

,

ppt

,

Important questions

,

study material

,

past year papers

,

pdf

,

Previous Year Questions with Solutions

;