Break Statement
Java break
Statement
break
StatementThe break
statement in Java is a control flow statement that allows you to exit a loop or switch statement prematurely. It is particularly useful when you need to stop the execution of a loop based on a certain condition or exit a switch
case. In this tutorial, we'll explore how the break
statement works in different contexts and provide detailed examples to help you understand its usage.
1. What is a break
Statement?
break
Statement?The break
statement is used to terminate the execution of the nearest enclosing loop (for
, while
, or do-while
) or a switch
statement. When a break
statement is encountered, the control flow immediately exits the loop or switch block, skipping any remaining code in the current iteration.
Syntax:
2. Using break
in Loops
break
in LoopsThe break
statement is often used in loops to exit the loop when a specific condition is met. This can be helpful when you want to stop the loop early, without waiting for the loop's natural termination condition.
2.1 Example: break
in a for
Loop
Let's start with a simple example where we use the break
statement to exit a for
loop when a certain condition is met:
Explanation:
The loop starts with
i
equal to 1 and continues untili
is less than or equal to 10.When
i
reaches 5, thebreak
statement is executed, and the loop is terminated.
Output:
In this example, the loop stops executing when i
equals 5, so the numbers 1 to 4 are printed.
2.2 Example: break
in a while
Loop
You can also use the break
statement in a while
loop to exit the loop based on a condition:
Output:
In this example, the loop stops executing when i
equals 5, just like in the for
loop example.
2.3 Example: break
in a do-while
Loop
The break
statement works similarly in a do-while
loop:
Output:
In this example, the loop stops executing when i
equals 5, just like in the previous examples.
3. Using break
in switch
Statements
break
in switch
StatementsThe break
statement is also commonly used in switch
statements to exit a specific case after executing its code. Without the break
statement, the control flow would "fall through" to the next case, which is usually not desired.
Example:
Explanation:
The
switch
statement evaluates the value ofday
(which is 3).The case for
3
is executed, printing "Wednesday."The
break
statement then exits theswitch
block, preventing any subsequent cases from being executed.
Output:
Without the break
statement, the code would continue executing the subsequent cases, which is known as "fall-through."
4. Nested Loops and break
break
When you have nested loops, the break
statement will only terminate the innermost loop in which it is used. If you want to exit multiple levels of loops, you need to use labeled break
statements.
4.1 Example: break
in Nested Loops
Explanation:
The outer loop iterates through
i
values from 1 to 3.The inner loop iterates through
j
values from 1 to 3.When
j
equals 2, thebreak
statement terminates the inner loop, but the outer loop continues.
Output:
In this example, the inner loop stops executing when j
equals 2, but the outer loop continues.
5. Labeled break
Statement
break
StatementIn situations where you want to exit an outer loop from within a nested loop, you can use a labeled break
statement. A label is simply an identifier followed by a colon (:
) placed before a loop.
Example:
Explanation:
The label
outerLoop
is attached to the outer loop.When
j
equals 2, thebreak outerLoop
statement terminates the outer loop, not just the inner loop.
Output:
In this example, both loops stop executing when j
equals 2 because the labeled break
statement exits the outer loop.
Conclusion
The break
statement is a powerful tool in Java that allows you to control the flow of your programs by exiting loops or switch
statements prematurely. Whether you're using it to stop a loop based on a condition, prevent fall-through in a switch
statement, or exit nested loops with labeled break
statements, mastering the break
statement will make your code more efficient and easier to understand.
For more Java tutorials and resources, visit codeswithpankaj.com.
Last updated