What does break statement do in java?

点点圈 提交于 2019-12-12 09:39:02

问题


Does anyone knows what the group_skip do?

Maybe it is a basic programming, but I've been programming using Java for some years and just found it today.

group_skip: do {
    event = stepToNextEvent(FormController.STEP_OVER_GROUP);
    switch (event) {
        case FormEntryController.EVENT_QUESTION:
        case FormEntryController.EVENT_END_OF_FORM:
            break group_skip;
    }
} while (event != FormEntryController.EVENT_END_OF_FORM);

Thanks!


回答1:


This is a labelled loop, when break group_skip; statement is executed, it will jump out of the do while loop which is labelled as group_skip

boolean isTrue = true;
    outer: for (int i = 0; i < 5; i++) {
        while (isTrue) {
            System.out.println("Hello");
            break outer;
        } // end of inner while
        System.out.println("Outer loop"); // does not print
    } // end of outer loop
   System.out.println("Good Bye");

This outputs

Hello
Good Bye

You can get the concept clear here.

  • There is a labelled for loop called outer and there is inner while loop
  • When inner for loop is being executed, it encounters break outer; statement
  • The outer for loop has a System.out.println"Outer loop" statement but that does not get printed.
  • This is because break outer causes the control to jump out of labelled for loop directly

Now this example for continue statement

outer: for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            System.out.println("Hello");
            continue outer;
        } // end of inner loop
        System.out.println("outer"); // this won't print
    } // end of outer loop
    System.out.println("Good bye");

This prints

Hello
Hello
Hello
Hello
Hello
Good bye
  • There is a labelled for loop here and an inner for loop
  • Inner for loop prints Hello and continues to the outer loop.
  • Because of this, the statements below inner for loop are skipped and outer loop continues to execute.
  • At the end of outer for loop, Good Bye is printed

Hope this makes everything clear.




回答2:


group_skip is a label. Labels allow you to break or continue specific loops when you've got them nested.

Here's what Oracle has to say on the subject.




回答3:


when ever we use simple break statement then we can only transfer control from inner most loop to the outer most (if we have nesting of loops). for exampel

for(int i=0; i < 10; i++){
    if(i==5){
      break;
    }
}
statement x;

will simply transfer the control to statement x. But if you use it inside nested loops then it will work differently.

for(int i=0; i < 10; i++){
 for(int j=0; j < 10; j++)
    if(i==5){
      break;
    }
 }
statement y;
}
statement x;

in this case it will send the control to statement y. If you want to send the control from innermost loop to either outermost loop or outside the loop then you need such a break statements with labels. Just do it from your self and you will see interesting output.. :)




回答4:


group_skip is a label used for things like break. (Also goto and jump in other languages)

In java specifically it would be used to break from a code block identified by the label, behaving just like a break statement in a while loop, except breaking from a labeled code block.

Here is some more discussion on the topic



来源:https://stackoverflow.com/questions/17334917/what-does-break-statement-do-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!