The break
statement is a control flow statement that allows you to exit from a loop or switch statement prematurely. It is commonly used to stop the execution of a loop or switch once a certain condition is met. The break
statement can be used in loops like for
, while
, and do-while
, as well as in switch
statements.
The primary purpose of the break
statement is to control the flow of program execution by terminating a loop or a switch case before it naturally completes. This can be particularly useful in situations where continuing the loop or switch would not be necessary or could lead to inefficiency.
- Exiting a loop early: If a loop condition is met and further iterations would be unnecessary, the
break
statement can immediately exit the loop. - Exiting a switch statement early: In a
switch
block, thebreak
statement prevents the execution from falling through to the next case after a match.
Syntax
1 |
break; |
- In Loops: When used inside a loop, the
break
statement causes the loop to terminate and the program control to jump to the next statement after the loop. - In Switch Statements: When used inside a
switch
, thebreak
statement causes the program to exit theswitch
block.
Examples
- Using
break
in afor
loop: Here’s how thebreak
statement works in a loop. Consider a loop that searches for a number in an array:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class BreakExample { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int target = 30; for (int i = 0; i < numbers.length; i++) { if (numbers[i] == target) { System.out.println("Found the number " + target + " at index " + i); break; // Exit the loop as the target is found } } } } |
Output
1 |
Found the number 30 at index 2 |
In this example, the loop stops iterating as soon as it finds the number 30
in the array, thanks to the break
statement. Without break
, the loop would continue to search through the entire array even after finding the target number.
- Using
break
in awhile
loop: You can also use thebreak
statement in awhile
loop. Here’s an example that stops a loop based on user input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; public class BreakWhileExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String userInput; while (true) { // Infinite loop System.out.print("Enter 'quit' to exit the loop: "); userInput = scanner.nextLine(); if (userInput.equalsIgnoreCase("quit")) { System.out.println("Exiting loop..."); break; // Exit the loop if 'quit' is entered } } scanner.close(); } } |
Output
1 2 3 |
Enter 'quit' to exit the loop: hello Enter 'quit' to exit the loop: quit Exiting loop... |
In this case, the program continually prompts the user for input. When the user enters “quit”, the break
statement exits the loop, preventing further prompts.
- Using
break
in aswitch
statement: Thebreak
statement is essential in aswitch
case to prevent the program from falling through to the next case unless explicitly desired:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class SwitchBreakExample { public static void main(String[] args) { int day = 3; switch (day) { case 1: System.out.println("Monday"); break; // Exits the switch block after case 1 case 2: System.out.println("Tuesday"); break; // Exits the switch block after case 2 case 3: System.out.println("Wednesday"); break; // Exits the switch block after case 3 default: System.out.println("Invalid day"); } } } |
Output
1 |
Wednesday |
In this example, after the program matches case 3
, it prints “Wednesday” and then exits the switch
statement due to the break
statement. Without break
, the program would continue executing the following cases, potentially leading to unintended behavior.