Java break
and continue
Statements
In Java, the break
and continue
statements are used to alter the flow of control in loops. They provide mechanisms to exit loops prematurely or skip certain iterations, respectively. Understanding how to use these statements effectively can help in creating more flexible and efficient loops.
1. The break
Statement
The break
statement is used to exit from a loop (or switch statement) prematurely, regardless of whether the loop’s condition is true or false. It immediately terminates the loop and transfers control to the statement following the loop.
Syntax
break;
Example of break
in a for
Loop
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
System.out.println(i);
}
}
}
Output:
0
1
2
3
4
In this example, the break
statement is executed when i
equals 5, causing the loop to exit early. The output includes numbers from 0 to 4, and the loop terminates before reaching 5.
Example of break
in a while
Loop
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
if (i == 5) {
break; // Exit the loop when i is 5
}
System.out.println(i);
i++;
}
}
}
Output:
0
1
2
3
4
Similar to the for
loop example, the break
statement exits the while
loop when i
equals 5.
2. The continue
Statement
The continue
statement is used to skip the current iteration of a loop and proceed to the next iteration. Unlike the break
statement, continue
does not exit the loop; it only skips the remaining code in the current iteration and reevaluates the loop’s condition.
Syntax
continue;
Example of continue
in a for
Loop
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(i);
}
}
}
Output:
1
3
5
7
9
In this example, the continue
statement skips the System.out.println(i)
line for even numbers (when i % 2 == 0
). As a result, only odd numbers are printed.
Example of continue
in a while
Loop
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(i);
}
}
}
Output:
1
3
5
7
9
Here, the continue
statement skips the System.out.println(i)
for even numbers and continues with the next iteration of the loop.
3. break
and continue
in Nested Loops
In nested loops, the break
statement exits the innermost loop, while the continue
statement affects only the current loop level. To exit multiple levels of nested loops, you can use labeled breaks.
Example of break
in Nested Loops
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break; // Exits the inner loop when i is 1 and j is 1
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
The break
statement exits the inner loop when i
is 1 and j
is 1. The outer loop continues to execute.
Example of Labeled break
Statement
To break out of multiple nested loops, use a labeled break
.
public class Main {
public static void main(String[] args) {
outerLoop: // Labeled block
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break outerLoop; // Breaks out of both loops
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
The labeled break
exits both the inner and outer loops when i
is 1 and j
is 1.
Example of Labeled continue
Statement
Labeled continue
is used to skip the current iteration of an outer loop from within an inner loop.
public class Main {
public static void main(String[] args) {
outerLoop: // Labeled block
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
continue outerLoop; // Skips to the next iteration of the outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
Here, the labeled continue
skips the rest of the inner loop when i
is 1 and j
is 1, and proceeds to the next iteration of the outer loop.
4. Summary
break
Statement: Exits from the current loop or switch statement immediately.continue
Statement: Skips the remaining code in the current loop iteration and proceeds to the next iteration.- Nested Loops:
break
exits the innermost loop;continue
affects only the current loop level. - Labeled
break
andcontinue
: Useful for breaking out of or continuing specific levels of nested loops.
Using break
and continue
statements effectively helps in controlling the flow of loops and can simplify the logic in certain scenarios.