
Java Break Continue in Java
break
and continue
in Java
In Java, the break
and continue
statements are used to control the flow of loops and switch statements. They help you modify the usual flow of execution, either by exiting a loop or skipping to the next iteration.
1. break
Statement
The break
statement is used to exit from a loop or a switch
statement prematurely. Once the break
statement is encountered, the program control immediately exits the loop or switch
block and continues executing the code that follows the loop or switch
.
In Loops:
The break
statement can be used to exit a loop (like for
, while
, or do-while
) before it has completed all iterations.
Example: break
in a for
loop
public class BreakExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { // Exiting the loop when i equals 5 break; } System.out.println(i); } System.out.println("Loop ended."); }}
Output:
1234Loop ended.
In the example above, when i
reaches 5, the break
statement causes the loop to exit immediately. The number 5 is not printed, and the loop ends prematurely.
In switch
Statements:
The break
statement is used in switch
statements to exit from a particular case once it has been executed.
public class BreakInSwitchExample { public static void main(String[] args) { int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid day"); } }}
Output:
Wednesday
In this example, when day
is 3
, the code inside case 3
executes and then the break
statement causes the program to exit the switch
block.
Without the break
, the program would "fall through" to the next case and continue executing.
2. continue
Statement
The continue
statement is used to skip the current iteration of a loop and continue with the next iteration. It doesn't exit the loop entirely but skips to the next cycle of the loop.
Example: continue
in a for
loop
public class ContinueExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { // Skipping the rest of the loop when i equals 5 continue; } System.out.println(i); } }}
Output:
1234678910
In the above example, when i
equals 5, the continue
statement skips the rest of the code inside the loop and moves to the next iteration, effectively skipping the printing of the number 5.
Example: continue
in a while
loop
public class ContinueInWhileExample { public static void main(String[] args) { int i = 1; while (i <= 10) { if (i == 5) { i++; continue; // Skip the rest of the loop iteration } System.out.println(i); i++; } }}
Output:
1234678910
Again, when i
equals 5, the continue
statement causes the rest of the loop's body to be skipped, and the loop proceeds with the next value of i
.
Key Differences Between break
and continue
Feature | break | continue |
---|---|---|
Purpose | Exits the loop or switch statement completely. | Skips the current iteration of the loop and moves to the next one. |
Used in | Loops and switch statements. | Loops only (i.e., for , while , do-while ). |
Impact on Flow | Exits the loop or switch immediately. | Skips the rest of the current loop iteration and continues with the next one. |
break
and continue
in Nested Loops
Both break
and continue
can be used in nested loops (loops inside other loops), but they only affect the innermost loop in which they are used. To break or continue from an outer loop, labels are used.
Example: break
in Nested Loops
public class BreakInNestedLoops { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { break; // Breaks the inner loop when j equals 2 } System.out.println("i = " + i + ", j = " + j); } } }}
Output:
i = 1, j = 1i = 2, j = 1i = 3, j = 1
In this example, the inner loop breaks when j
equals 2, so the inner loop does not complete all of its iterations for each value of i
.
Example: continue
in Nested Loops
public class ContinueInNestedLoops { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { continue; // Skips the rest of the inner loop when j equals 2 } System.out.println("i = " + i + ", j = " + j); } } }}
Output:
i = 1, j = 1i = 1, j = 3i = 2, j = 1i = 2, j = 3i = 3, j = 1i = 3, j = 3
In this example, when j
equals 2, the continue
statement skips the rest of the inner loop and proceeds with the next iteration of the inner loop.
Using Labels with break
and continue
You can use labeled break
and labeled continue
to control flow in nested loops.
Example: Labeled break
in Nested Loops
public class LabeledBreakExample { public static void main(String[] args) { outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { break outerLoop; // Breaks out of both loops when i=2 and j=2 } System.out.println("i = " + i + ", j = " + j); } } }}
Output:
i = 1, j = 1i = 1, j = 2i = 1, j = 3i = 2, j = 1
In this example, the labeled break
exits both loops when i = 2
and j = 2
.
Example: Labeled continue
in Nested Loops
public class LabeledContinueExample { public static void main(String[] args) { outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { continue outerLoop; // Skips to the next iteration of the outer loop } System.out.println("i = " + i + ", j = " + j); } } }}
Output:
i = 1, j = 1i = 2, j = 1i = 3, j = 1
In this example, the labeled continue
skips to the next iteration of the outer loop when j = 2
, effectively skipping the inner loop execution for that iteration.
Summary
break
: Exits the loop orswitch
statement immediately.continue
: Skips the current iteration of the loop and proceeds to the next iteration.Labels: Used to break or continue specific loops in nested loop scenarios.
These flow control statements are essential for efficiently managing loops and switch
cases in Java. Let me know if you need further clarification or examples! ?