Introduction
The break
statement in Java is a control flow statement that is used to exit from a loop or a switch statement prematurely. It can be particularly useful for terminating loops early when a certain condition is met, or for exiting a switch case once a specific case has been executed. In this chapter, we will explore the syntax, usage, and examples of the break
statement in Java.
Syntax
The basic syntax of the break
statement is as follows:
break;
Key Points:
- The
break
statement can be used inside loops (for
,while
,do-while
) andswitch
statements. - When the
break
statement is executed, control immediately exits the loop or switch statement and proceeds to the next statement following the loop or switch.
Using break in Loops
Example: Exiting a for Loop
Let’s consider an example where we use the break
statement to exit a for
loop when a specific condition is met.
Example Code:
public class BreakForLoopExample {
public static void main(String[] args) {
// Loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// Exit the loop if i equals 5
if (i == 5) {
break;
}
// Print the current value of i
System.out.println("i: " + i);
}
// This statement is executed after the loop terminates
System.out.println("Loop terminated.");
}
}
Output:
i: 1
i: 2
i: 3
i: 4
Loop terminated.
Example: Exiting a while Loop
Let’s consider an example where we use the break
statement to exit a while
loop when a specific condition is met.
Example Code:
public class BreakWhileLoopExample {
public static void main(String[] args) {
int count = 1;
// Loop while count is less than or equal to 10
while (count <= 10) {
// Exit the loop if count equals 5
if (count == 5) {
break;
}
// Print the current value of count
System.out.println("Count: " + count);
count++;
}
// This statement is executed after the loop terminates
System.out.println("Loop terminated.");
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Loop terminated.
Example: Exiting a do-while Loop
Let’s consider an example where we use the break
statement to exit a do-while
loop when a specific condition is met.
Example Code:
public class BreakDoWhileLoopExample {
public static void main(String[] args) {
int count = 1;
// Loop at least once, then while count is less than or equal to 10
do {
// Exit the loop if count equals 5
if (count == 5) {
break;
}
// Print the current value of count
System.out.println("Count: " + count);
count++;
} while (count <= 10);
// This statement is executed after the loop terminates
System.out.println("Loop terminated.");
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Loop terminated.
Using break in switch Statement
Example: Exiting a switch Case
Let’s consider an example where we use the break
statement to exit a switch
case after executing the corresponding block of code.
Example Code:
public class BreakSwitchExample {
public static void main(String[] args) {
int day = 3;
// Determine the day of the week
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Output:
Wednesday
Diagram: Flow Chart of break Statement in a Loop
Start
|
[initialize]
|
[condition]
|
/ \
/ \
True False
/ \
[check End
break]
/ \
True False
| |
End [execute]
|
[update]
|
[condition]
Conclusion
The break
statement is a powerful control flow statement in Java that allows you to exit loops and switch cases prematurely. It enhances the flexibility of your code by providing a way to terminate execution based on specific conditions. Understanding how to use the break
statement effectively can help you write more efficient and readable Java programs.