Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does break statement do in java?

Tags:

java

break

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!

like image 649
Rendy Avatar asked Jun 27 '13 05:06

Rendy


People also ask

What is the use of break statement?

The break statement is frequently used to terminate the processing of a particular case within a switch statement. Lack of an enclosing iterative or switch statement generates an error.

What happens with a break statement?

The break statement terminates the current loop, switch , or label statement and transfers program control to the statement following the terminated statement.

What is the use of break and continue statement in Java?

The break statement is used to terminate the loop immediately. The continue statement is used to skip the current iteration of the loop. break keyword is used to indicate break statements in java programming. continue keyword is used to indicate continue statement in java programming.

Can we use break statement in Java?

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).


1 Answers

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.

like image 133
Prasad Kharkar Avatar answered Oct 06 '22 01:10

Prasad Kharkar