Break & continue statement
Break statement is used to terminate any process. In loops it is used to terminate the loop and proceed to the next statement. In switch-case, it is used to move out of the switch statement.
The following program shows the use of break statement in a for loop using if statement.
package com.company;
public class BreakAndContinueStatements {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
}
}
output:
1
2
In a nested for loop, the break statement can be used to terminate the innermost loop. The following program shows the use of break statement in a nested for loop.
package com.company;
public class BreakAndContinueStatements {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
for (int k = 1; k < 5; k++) {
System.out.println("i = " + i + ",j = "+ j + ",k = "+k );
if (k == 3) {
break;
}
}
}
}
}
}
Output:
i = 1,j = 1,k = 1
i = 1,j = 1,k = 2
i = 1,j = 1,k = 3
i = 1,j = 2,k = 1
i = 1,j = 2,k = 2
i = 1,j = 2,k = 3
i = 1,j = 3,k = 1
i = 1,j = 3,k = 2
i = 1,j = 3,k = 3
i = 1,j = 4,k = 1
i = 1,j = 4,k = 2
i = 1,j = 4,k = 3
i = 2,j = 1,k = 1
i = 2,j = 1,k = 2
i = 2,j = 1,k = 3
i = 2,j = 2,k = 1
i = 2,j = 2,k = 2
i = 2,j = 2,k = 3
i = 2,j = 3,k = 1
i = 2,j = 3,k = 2
i = 2,j = 3,k = 3
i = 2,j = 4,k = 1
i = 2,j = 4,k = 2
i = 2,j = 4,k = 3
i = 3,j = 1,k = 1
i = 3,j = 1,k = 2
i = 3,j = 1,k = 3
i = 3,j = 2,k = 1
i = 3,j = 2,k = 2
i = 3,j = 2,k = 3
i = 3,j = 3,k = 1
i = 3,j = 3,k = 2
i = 3,j = 3,k = 3
i = 3,j = 4,k = 1
i = 3,j = 4,k = 2
i = 3,j = 4,k = 3
i = 4,j = 1,k = 1
i = 4,j = 1,k = 2
i = 4,j = 1,k = 3
i = 4,j = 2,k = 1
i = 4,j = 2,k = 2
i = 4,j = 2,k = 3
i = 4,j = 3,k = 1
i = 4,j = 3,k = 2
i = 4,j = 3,k = 3
i = 4,j = 4,k = 1
i = 4,j = 4,k = 2
i = 4,j = 4,k = 3
A break statement is also used as a labelled break. It can be used to break the outermost loop. The following program shows the use of a labelled break. Here, the loop gets terminated labelled as second on condition of k = 3.
package com.company;
public class BreakAndContinueStatements {
public static void main(String[] args) {
first:
for (int i = 1; i < 5; i++) {
second:
for (int j = 1; j < 5; j++) {
third:
for (int k = 1; k < 5; k++) {
System.out.println("i = " + i + ",j = "+ j + ",k = "+k );
if (k == 3) {
break second;
}
}
}
}
}
}
Output:
i = 1,j = 1,k = 1
i = 1,j = 1,k = 2
i = 1,j = 1,k = 3
i = 2,j = 1,k = 1
i = 2,j = 1,k = 2
i = 2,j = 1,k = 3
i = 3,j = 1,k = 1
i = 3,j = 1,k = 2
i = 3,j = 1,k = 3
i = 4,j = 1,k = 1
i = 4,j = 1,k = 2
i = 4,j = 1,k = 3
Continue Statement is used to used to skip any particular statement or iteration and jump to the next one. The following program shows the use of continue statement. Here, iteration of the loop stops when i=3 and jumps to the next one.
package com.company;
public class BreakAndContinueStatements {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output:
1
2
4
In a nested loop continue statement skips the iteration of the current loop and moves to the next iteration of that loop. The following example shows the use of continue statement in a nested loop. Here, when k = 3 of the innermost loop, the iteration jumps to the next one.
package com.company;
public class BreakAndContinueStatements {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
for (int k = 1; k < 5; k++) {
if (k == 3) {
continue;
}
System.out.println("i = " + i + ",j = "+ j + ",k = "+k );
}
}
}
}
}
Output:
i = 1,j = 1,k = 1
i = 1,j = 1,k = 2
i = 1,j = 1,k = 4
i = 1,j = 2,k = 1
i = 1,j = 2,k = 2
i = 1,j = 2,k = 4
i = 1,j = 3,k = 1
i = 1,j = 3,k = 2
i = 1,j = 3,k = 4
i = 1,j = 4,k = 1
i = 1,j = 4,k = 2
i = 1,j = 4,k = 4
i = 2,j = 1,k = 1
i = 2,j = 1,k = 2
i = 2,j = 1,k = 4
i = 2,j = 2,k = 1
i = 2,j = 2,k = 2
i = 2,j = 2,k = 4
i = 2,j = 3,k = 1
i = 2,j = 3,k = 2
i = 2,j = 3,k = 4
i = 2,j = 4,k = 1
i = 2,j = 4,k = 2
i = 2,j = 4,k = 4
i = 3,j = 1,k = 1
i = 3,j = 1,k = 2
i = 3,j = 1,k = 4
i = 3,j = 2,k = 1
i = 3,j = 2,k = 2
i = 3,j = 2,k = 4
i = 3,j = 3,k = 1
i = 3,j = 3,k = 2
i = 3,j = 3,k = 4
i = 3,j = 4,k = 1
i = 3,j = 4,k = 2
i = 3,j = 4,k = 4
i = 4,j = 1,k = 1
i = 4,j = 1,k = 2
i = 4,j = 1,k = 4
i = 4,j = 2,k = 1
i = 4,j = 2,k = 2
i = 4,j = 2,k = 4
i = 4,j = 3,k = 1
i = 4,j = 3,k = 2
i = 4,j = 3,k = 4
i = 4,j = 4,k = 1
i = 4,j = 4,k = 2
i = 4,j = 4,k = 4
Continue statement in the labelled nested loop skips the iteration of the labelled loop and move to the next iteration. The following program shows the use continue statement in the labelled nested loop. The iteration of the second loop at j = 3 is skipped and move to the next iteration.
package com.company;
public class BreakAndContinueStatements {
public static void main(String[] args) {
first:
for (int i = 1; i < 5; i++) {
second:
for (int j = 1; j < 5; j++) {
third:
for (int k = 1; k < 5; k++) {
if (j == 3) {
continue second;
}
System.out.println("i = " + i + ",j = "+ j + ",k = "+k );
}
}
}
}
}
Output:
i = 1,j = 1,k = 1
i = 1,j = 1,k = 2
i = 1,j = 1,k = 3
i = 1,j = 1,k = 4
i = 1,j = 2,k = 1
i = 1,j = 2,k = 2
i = 1,j = 2,k = 3
i = 1,j = 2,k = 4
i = 1,j = 4,k = 1
i = 1,j = 4,k = 2
i = 1,j = 4,k = 3
i = 1,j = 4,k = 4
i = 2,j = 1,k = 1
i = 2,j = 1,k = 2
i = 2,j = 1,k = 3
i = 2,j = 1,k = 4
i = 2,j = 2,k = 1
i = 2,j = 2,k = 2
i = 2,j = 2,k = 3
i = 2,j = 2,k = 4
i = 2,j = 4,k = 1
i = 2,j = 4,k = 2
i = 2,j = 4,k = 3
i = 2,j = 4,k = 4
i = 3,j = 1,k = 1
i = 3,j = 1,k = 2
i = 3,j = 1,k = 3
i = 3,j = 1,k = 4
i = 3,j = 2,k = 1
i = 3,j = 2,k = 2
i = 3,j = 2,k = 3
i = 3,j = 2,k = 4
i = 3,j = 4,k = 1
i = 3,j = 4,k = 2
i = 3,j = 4,k = 3
i = 3,j = 4,k = 4
i = 4,j = 1,k = 1
i = 4,j = 1,k = 2
i = 4,j = 1,k = 3
i = 4,j = 1,k = 4
i = 4,j = 2,k = 1
i = 4,j = 2,k = 2
i = 4,j = 2,k = 3
i = 4,j = 2,k = 4
i = 4,j = 4,k = 1
i = 4,j = 4,k = 2
i = 4,j = 4,k = 3
i = 4,j = 4,k = 4