0

OK, so I was looking up the goto statement in java, and I stumbled on this post: Is there a goto statement in Java? (I know, I'm a scrub :P )

Anyways, I stumbled across this chuck of code in the comments:

public static void main(String [] args) {

 boolean t = true;

 first: 
 {
    second: 
    {
       third: 
       {
           System.out.println("Before the break");

           if (t) {
              break second;
           }

           System.out.println("Not executed");

       }

       System.out.println("Not executed - end of second block");

    }

    System.out.println("End of third block");

 }
}

And I am confused as to why the second block wouldn't have been executed after the break. Can someone explain this to me?

I would've commented to ask, but I don't have a high enough Rep to do so.

Thank you!

Community
  • 1
  • 1
Austen Clay
  • 39
  • 1
  • 8
  • "*And I am confused as to why the second block wouldn't have been executed after the break.*" I am confused about your confusion. That is kind of whole point of `break second;`, to break code block labelled with `second` (to move flow of control right after this block). What other behaviour did you expect? – Pshemo Feb 24 '15 at 00:57
  • I misunderstood the usage of break then, I thought it was to break away from where you are in the code, then start at the specified label. – Austen Clay Feb 24 '15 at 01:00
  • @AustenClay you confused break with goto. – zubergu Feb 24 '15 at 01:01
  • @zubergu, I realize that now :P – Austen Clay Feb 24 '15 at 01:01

1 Answers1

2

And I am confused as to why the second block wouldn't have been executed after the break.

Because the break statement with label terminates the labeled block and does not take the execution back to the label.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136