Break statement in Java

Break Statement is a loop control statement which is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop.
Syntax:

break;

Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.

Break: In Java, break is majorly used for:

  • Terminate a sequence in a switch statement (discussed above).
  • To exit a loop.
  • Used as a “civilized” form of goto.

Using break to exit a Loop

Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.
Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.
using-break-to-exit-a-loop-in-java
Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate using
// break to exit a loop
class BreakLoopDemo {
    public static void main(String args[])
    {
        // Initially loop is set to run from 0-9
        for (int i = 0; i < 10; i++) {
            // terminate loop when i is 5.
            if (i == 5)
                break;
  
            System.out.println("i: " + i);
        }
        System.out.println("Loop complete.");
    }
}

chevron_right


Output:

i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.

Using break as a Form of Goto

Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses label. A Label is use to identifies a block of code.
Syntax:

label:
{
  statement1;
  statement2;
  statement3;
  .
  .
}

Now, break statement can be use to jump out of target block.
Note: You cannot break to any label which is not defined for an enclosing block.
Syntax:

break label;

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate using break with goto
class BreakLabelDemo {
    public static void main(String args[])
    {
        boolean t = true;
  
    // label first
    first : {
    // Illegal statement here as label second is not
    // introduced yet break second;
    second : {
    third : {
        // Before break
        System.out.println("Before the break statement");
  
        // break will take the control out of
        // second label
        if (t)
            break second;
        System.out.println("This won't execute.");
    }
        System.out.println("This won't execute.");
    }
  
        // First block
        System.out.println("This is after second block.");
    }
    }
}

chevron_right


Output:

Before the break.
This is after the second block.

Related Articles:

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Senior Technical Content Engineer | GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.