What does the break statement do in java?

Does anyone know what group_skip does?

This may be basic programming, but I have been programming Java for several 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!

+6
source share
4 answers

This is the marked loop when the break group_skip; statement is break group_skip; , it will jump out of the do while loop, which is marked 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"); 

Displays

 Hello Good Bye 

Here you can understand the concept.

  • There is a labeled for loop called outer , and there is an inner while
  • When the inner for loop executes, it encounters the break outer; statement break outer;
  • outer for loop has the System.out.println"Outer loop" statement, but it does not print.
  • This is because break outer forces the control to automatically exit the for loop

Now this example for the 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"); 

Will print

 Hello Hello Hello Hello Hello Good bye 
  • The for snowstorm and inner for loop are listed here.
  • The inner for loop prints Hello and continues the outer loop.
  • Because of this, instructions below the inner for loop are skipped and the outer loop continues to run.
  • At the end of the outer loop, Good Bye printed for the loop.

Hope this is all clear.

+15
source

group_skip is a label. Shortcuts allow you to break or continue certain cycles when you nest them.

Here is what Oracle has to say on this.

+8
source

when we use the simple break statement, we can only transfer control from the inner majority of loops to the outer majority (if we have nesting loops). for example

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

just transfers the control to the x operator. 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 the y operator. If you want to send a control from the innermost loop to any of the outer loops or outside the loop, then you need break statements with labels. Just do it on your own and you will see an interesting result. :)

+5
source

group_skip is a label used for things like break. (Also go and go to other languages)

In java, specifically, it will be used to break with a code block identified by a label that behaves like a break statement in a while loop, with the exception of breaking from tagged code.

Here is another discussion on the topic.

0
source

Source: https://habr.com/ru/post/948188/


All Articles