0

I don't understand the output of the following code:

public class testBreak
{

    public static void runTest()
    {
    high_level:
        {
            System.out.println("\nEnter in High Level");
        middle_level: 
            for(int i = 0; i < 4; i++)
            {
                System.out.println("Enter in Middle level cicle");
                int j = 0;
            low_level:  
                while(j++ < 2)
                {
                    System.out.println("Enter in Low level cicle");
                    System.out.println("i = " + i + ";j = " + j);
                    switch(i)
                    {
                        case 0 : break;
                        case 1 : break low_level;
                        case 2 : break middle_level;
                        case 3 : break high_level;
                    }
                    System.out.println("Exit from Low level cicle");
                }
                System.out.println("Exit from Middle level cicle");
            }
            System.out.println("Exit from High level cicle");
        }
    }
    
    public static void main(String args[])
    {
        runTest();
    }

}

Here is the output:

Enter in High Level
Enter in Middle level cicle
Enter in Low level cicle
i = 0;j = 1
Exit from Low level cicle
Enter in Low level cicle
i = 0;j = 2
Exit from Low level cicle
Exit from Middle level cicle
Enter in Middle level cicle
Enter in Low level cicle
i = 1;j = 1
Exit from Middle level cicle
Enter in Middle level cicle
Enter in Low level cicle
i = 2;j = 1
Exit from High level cicle

I understand up to the line i = 1;j = 1 but why does it go to Exit from Middle level cicle and not to Enter in low level cicle because we have j = 1 and while(j++ < 2) should take 1 < 2, then increase j to 2 and enter the low level cicle as it did in the first run??

editet: It seems break with label does not equal goto, it breaks out of the labeled loop? and the loop is not free in the code like in C/C++, but it is applied to that particular loop?

2
  • 1
    Have you already used a debugger?
    – Donat
    Commented Aug 1, 2020 at 13:49
  • 1
    @Donat tried the CLI java debugger, but it is clear where the code jumps, was looking for the reason
    – mihnea
    Commented Aug 1, 2020 at 13:52

2 Answers 2

0

When i = 1, you are telling it to break out of the low_level label. The loop below low_level stops looping, and then prints Exit from Middle level cicle. By doing this, you then loop again below the middle_level loop, which is why it then prints Enter in Middle level cicle.

2
  • Yeah, I initially understood that break works like a goto statement, not that it jump out of the named loop. if I would have used break high_level it would have jumped out of the { } block referred to by high_level
    – mihnea
    Commented Aug 1, 2020 at 14:09
  • Yep, break high_level; would just terminate the {} block beneath it, which ends the function in this case. Glad I could help! Commented Aug 1, 2020 at 14:13
0

Taken from the Java tutorial by Oracle:

The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

Does this answer your question?

2
  • Yeah, I understood it is an exact replacement for goto - seems not.
    – mihnea
    Commented Aug 1, 2020 at 14:11
  • This says in essence that it is not an exact replacement for goto. Goto would continue after the label. But it says that it continues after the labeled statement.
    – Donat
    Commented Aug 1, 2020 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.