Java Break StatementLast Updated : 30 Dec 2025 The `break` statement in Java is a powerful tool for controlling the flow of your program. It is used to exit a loop or switch statement prematurely. It allows us to customize the behavior of code based on specific conditions. How do break statements work in Loops?When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. Using break with Loops and Switch StatementsThe Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop. Practical Example of break StatementFor example, consider a situation if we are searching for a specific value in an array using a for loop. We can use the break statement to exit the loop as soon as we find the value, like this: In this example, as soon as the value 3 is found in the array, the break statement is executed, and the loop is terminated. Without the break statement, the loop would continue iterating through the remaining elements of the array that is unnecessary once the value has been found. Overall, the break statement is a versatile tool that allows us to customize the flow of Java programs, making them more efficient and easier to read. However, it is important to use break judiciously and understand its impact on the flow of code to avoid unintended consequences. Use of break StatementWe can use Java break statement in all types of loops such as for loop, while loop and do-while loop. Syntax of the break StatementFlowchart of break StatementThe flowchart of a break statement starts with the condition check of the loop. If the condition is true, the loop body is executed, and then the condition is checked again. If the condition becomes false, the loop is exited normally, and the program continues with the statement after the loop. However, if a break statement is encountered inside the loop body, the control jumps to the statement immediately after the loop, bypassing the normal exit condition check. This behavior allows us to prematurely exit a loop based on certain conditions, even if the loop's condition is still true. It is important to note that a break statement only affects the innermost loop that contains it. If we have nested loops and we use a break statement in the inner loop, only that inner loop will be exited, and the outer loops will continue executing normally unless they also have their own break statements. ![]() Java Break Statement with LoopThe break statement with loop is used to immediately terminate the execution of a loop when a specific condition is met and transfer control to the statement that follows the loop. Example: Java break statement with LoopThe following example shows the use of the break statement inside a for loop. The loop runs from 1 to 10, but when the value of i becomes 5, the break statement stops the loop. As a result, only the numbers 1 to 4 are printed. Output: 1 2 3 4 Java Break Statement with Inner LoopIt breaks inner loop only if you use break statement inside the inner loop. Example: Java Break Statement with Inner LoopThe following example shows the use of the break statement inside a for loop. The loop runs from 1 to 10, but when the value of i becomes 5, the break statement stops the loop. As a result, only the numbers 1 to 4 are printed. Output: 1 1 1 2 1 3 2 1 3 1 3 2 3 3 Java break Statement with Labelled for LoopWe can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop. Example: Java break Statement with Labelled for LoopThe following example demonstrates the use of a labeled break statement. Two loops are defined with labels aa (outer loop) and bb (inner loop). When the condition i == 2 and j == 2 becomes true, break aa terminates the outer loop, not just the inner loop. As a result, the program stops executing both loops immediately and prints the values generated before the break condition. Output: 1 1 1 2 1 3 2 1 Java break Statement with while loopThe break statement with a while loop is used to stop the loop execution immediately when a specified condition becomes true, and control moves to the statement after the loop. Example: Java break Statement with while loopThe following example demonstrates the use of the break statement inside a while loop. The loop runs while the value of i is less than or equal to 10. When i becomes 5, the break statement is executed, which immediately terminates the loop. As a result, only the values from 1 to 4 are printed. Output: 1 2 3 4 Java break Statement with do-while loopThe break statement with a do-while loop is used to terminate the loop execution instantly when a specific condition is met, even though the loop executes at least once before the condition is checked. Example: Java break Statement with do-while loopThe following example demonstrates the use of the break statement inside a do-while loop. The loop starts with i = 1 and executes at least once. When the value of i becomes 5, the break statement stops the loop immediately. As a result, only the numbers from 1 to 4 are printed. Output: 1 2 3 4 Java Break Statement with SwitchThe break statement can be utilized alongside switch statement in Java for successfully exiting Example: Java Break Statement with SwitchThe following example shows the use of the break statement in a switch statement. The program displays a menu and processes the user’s choice. The break statement exits the switch after each case, while selecting option 4 exits the program. Output: Choose an option: 1. Option 1 2. Option 2 3. Option 3 4. Exit Enter your choice: 1 You chose Option 1 Enter your choice: 2 You chose Option 2 Enter your choice: 3 You chose Option 3 Enter your choice: 4 Exiting... To understand the example of break with switch statement, go with the link: Java Switch Statement. Real-World Uses of Java break StatementThe break statement in Java is commonly used in various applications to control the flow of a program and manage loop iterations or switch cases. One common application is in searching algorithms, where break can be used to exit a loop once a desired condition is met. For example, in linear search, the loop can be terminated early if the target element is found, saving unnecessary iterations. Another application is in error handling, where break can be used to exit a loop if an error condition is encountered. It can prevent the program from continuing to execute potentially faulty code and causing further issues. Additionally, break can be used in switch statements to exit the switch block once a specific case is matched, improving the efficiency of the code. In user input validation, break can be used to exit a loop that prompts the user for input until a valid input is provided. Once the valid input is received, the loop can be exited using break, and the program can continue with the next steps. It helps in ensuring that the program does not proceed with invalid or unexpected input. Example: Real-World Uses of Java break StatementLet's understand about the break statement of Java with the help of an example program that demonstrates the usage of break statement in all types of scenarios in a program. Output: Using break in a for loop: Enter a number (or -1 to exit): 6 You entered: 6 Enter a number (or -1 to exit): 5 You entered: 5 Enter a number (or -1 to exit): 1 You entered: 1 Enter a number (or -1 to exit): 2 You entered: 2 Enter a number (or -1 to exit): 36 You entered: 36 Using break in a while loop: Enter a number (or -1 to exit): 5 You entered: 5 Enter a number (or -1 to exit): 2 You entered: 2 Enter a number (or -1 to exit): 2 You entered: 2 Enter a number (or -1 to exit): 2 You entered: 2 Enter a number (or -1 to exit): 2 You entered: 2 Using break in a do-while loop: Enter a number (or -1 to exit): 2 You entered: 2 Enter a number (or -1 to exit): 2 You entered: 2 Enter a number (or -1 to exit): 2 Next TopicJava continue statement |
We request you to subscribe our newsletter for upcoming updates.