Break statement in Java - GeeksforGeeks

31 Mar, 2022
Break Statement is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop. Syntax: 
break;
Basically, break statements are used in 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, the 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.  
Example:
Java
// Java program to illustrate using// break to exit a loopclass 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.");    }}
Output: 
i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.
Time Complexity: O(1)
Auxiliary Space : O(1)
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 the label. A Label is used 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: 
Java
// Java program to illustrate// using break with gotoclass 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.");    }    }}
Output: 
Before the break statement.
This is after the second block.
Time Complexity: O(1)
Auxiliary Space : O(1)
YouTubeGeeksforGeeks500K subscribersBreak Continue | Java Programming | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 11:28•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=4uqb7-QpOAc" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
?list=PLqM7alHXFySF5ErEHA1BXgibGg7uqmA4_  
Related Articles: 
 
Decision making in Java
Break statement in C++
 
SumitBM
rishavnitro
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
Initialize an ArrayList in Java
How to iterate any Map in Java
Singleton Class in Java
Initializing a List in Java
Different ways of Reading a text file in Java
How to add an element to an Array in Java?
Stream In Java