Menu
Question Index
...

What will be the output of the following program?

class NestedFor
{
    public static void main(String s[])
    {
        for(int i = 2; i <= 5; i++ )
        {
            for(int j = i; j <= 5; j++ )
            {
                System.out.println("i = " + i + " j = " + j);    
            }
        }

    }
}


i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
i = 2 j = 5
i = 3 j = 1
i = 3 j = 2
i = 3 j = 3
i = 3 j = 4
i = 3 j = 5
i = 4 j = 1
i = 4 j = 2
i = 4 j = 3
i = 4 j = 4
i = 4 j = 5
i = 5 j = 1
i = 5 j = 2
i = 5 j = 3
i = 5 j = 4
i = 5 j = 5
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
i = 2 j = 5
i = 3 j = 3
i = 3 j = 4
i = 3 j = 5
i = 4 j = 4
i = 4 j = 5
i = 5 j = 5
Compilation error since variable i can not be used in the inner for loop.
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
i = 2 j = 5

Doubts

Problems

Topic: Nested for Loop In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App