0

I have a break label in my java code but it doesn't jump to the label when I go to the break statement in my code:

  OUTERMOST:for(String s :soso){

        if(wasBreaked){
              //code never enters this loop
                Log.e("","WASBREAK = FALSE");
                wasBreaked = false;
        }

        if(true){
                Log.e("","WASBREAK = TRUE GOING TO OUTERMOST");
                wasBreaked = true;
                break OUTERMOST;
        }
  }
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
Vinit M
  • 53
  • 2
  • 9
  • 1
    That doesn't work because break `OUTERMOST;` does **not** mean `jump to OUTERMOST`. It means _break the loop which has label_ `OUTERMOST` – BackSlash Dec 24 '14 at 09:29
  • 2
    It's not supposed to jump to the label. `break` is not `goto` (and there is no `goto` in Java). – RealSkeptic Dec 24 '14 at 09:29
  • Take a look http://stackoverflow.com/questions/389741/continue-keyword-in-java – newuser Dec 24 '14 at 09:32

2 Answers2

0

Break statement is not really a goto statement as in other programming languages like C.

What your code does instead is, break from the loop which has label OUTERMOST. I would have thought you need continue OUTERMOST; instead of break. But to me it really doesn't makes sense as you dont have further any statement post continue (now break in your code) and it's going to continue anyways irrespective of whether you say explicitly continue or not.

SMA
  • 36,381
  • 8
  • 49
  • 73
0
public class Test {

   public static void main(String[] args) {

    String soso[]={"1","2"};
    boolean wasBreaked=false;
    OUTERMOST:for(String s :soso){

        if(wasBreaked){
                Log.e("","WASBREAK = FALSE");
                wasBreaked = false;
        }

        if(true){
                Log.e("","WASBREAK = TRUE GOING TO OUTERMOST");
                wasBreaked = true;
                System.out.println("before break");

                break OUTERMOST;//use continue in place of break here for going to label
                }
        System.out.println("Inside outermost loop");
}
    System.out.println("outside outermost loop");

}

}

I tried this and it works it gives output

   before break
   outside outermost loop

For going back to label you should use continue keyword rather than break in code. After using continue the output will be like

 before break
 before break
 outside outermost loop
Abhishek Kumar
  • 315
  • 1
  • 4
  • 10