Search code examples
javanested-loopsbreaklabeled-statements

In a three nested for loop with labeled break, why is the output the same when the labeled break is positioned in the inner and middle loop?


Why is the output exactly the same when the labeled break is positioned before the start of the middle and inner loop?

Example code 1 - When break point is positioned at the start of the inner loop

        for(int i = 1;  i <= 3; i++)
        {
            //breakpoint: 
            for (int j = 1; j <= 3; j++)
            {
                breakpoint:                
                for(int k = 1; k <= 3; k++)
                {
                    if (i==2)
                        break breakpoint;
                        
                    System.out.println(i + " " + j + " " + k);

                }
            }
        }

The output is:

1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3

Example code 2 - When break point is positioned at the start of the middle loop

        // breakpoint: 
        for(int i = 1;  i <= 3; i++)
        {
            breakpoint: 
            for (int j = 1; j <= 3; j++)
            {
               // breakpoint:                
                for(int k = 1; k <= 3; k++)
                {
                    if (i==2)
                        break breakpoint;
                        
                    System.out.println(i + " " + j + " " + k);

                }
            }
        }

The output is the same:

1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3

Example code 3 - When break point is positioned at the start of the outer loop

        breakpoint: 
        for(int i = 1;  i <= 3; i++)
        {
            //breakpoint: 
            for (int j = 1; j <= 3; j++)
            {
               // breakpoint:                
                for(int k = 1; k <= 3; k++)
                {
                    if (i==2)
                        break breakpoint;
                        
                    System.out.println(i + " " + j + " " + k);

                }
            }
        }

The output is:

1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3

I changed the position of the breakpoint from the inner to the middle to the outer loop. I was expecting the output to be different from each other when the breakpoint was positioned at the start of the inner and the middle loop.

Why is it that whether the breakpoint is positioned at the inner or the middle loop, the output is the same?


Solution

  • The output is the same because you immediately break before the System.out.println statement and the condition i == 2 won't change until the outer loop moves on to the next iteration. It doesn't matter if the middle loop is run more times in this case.

    When the label is placed on the inner loop and i == 2:

    • On each iteration of the middle loop, we start an iteration of the inner loop
    • On the first iteration of the inner loop each time, we immediately break out of the inner loop without outputting anything
    • The next iteration of the middle loop is run in exactly the same way as above
    • After the last iteration of the middle loop, the outer loop continues the next iteration with i = 3

    When the label is placed on the middle loop and i == 2:

    • On the first iteration of the middle loop, we start an iteration of the inner loop
    • On the first iteration of the inner loop, we break out of the middle loop without outputting anything
    • The next iteration of the outer loop continues, and now i = 3