Break and Continue Statement in JAVA
Break and Continue Statement in JAVA
Break
and continue
Statement
Program flow can be controlled using break and continue statement in java. To manage loop iterations, we can use them in a loop. These statements give us the ability to control loop and switch statements by allowing us to either exit the loop or skip the current loop iteration and jump to the next one.
In this tutorial, we will discuss each in details with examples.
Break Statement
Break is a Java statement that is used to interrupt a program’s current flow of execution. The break statement can be used inside of a loop, switch case, etc. If break is used inside of a loop, the loop will be ended. If break is used inside the innermost loop, only the innermost loop will be terminated, and execution will move on to the outer loop. The execution will stop after the matched case if break is used in the switch case. We discussed the use of breaks in our discussion of switch cases.
Syntax:
jump-statement;
break;
Data Flow Diagram of break statement
Example:
In this example, we are using break inside the loop, the loop will terminate when the value is 8.
public class BreakDemo1 {
public static void main(String[] args) {
for(inti=1;i<=10;i++){
if(i==8){
break;
}
System.out.println(i);
}
}
}
Example using break in do while loop
Loop can be any one whether it is for or while, break statement will do the same. Here, we are using break inside the do while loop.
public class BreakDoWhileDemo1
{
public static void main(String[] args)
{
inti=1;
do
{
if(i==15)
{
i++;
break;
}
System.out.println(i);
i++;
}while(i<=20);
}
}
Example: Break in innermost loop
In this example, we are using break inside the innermost loop. But the loop breaks each time when j is equal to 2 and control goes to outer loop that starts from the next iteration.
public class Demo{
public static void main(String[] args) {
for(int i=1;i<=2;i++){
for (int j = 0; j <=3; j++) {
if(j==2)
break;
System.out.println(j);
}
}
}
}
0 1 0 1
Continue Statement
To skip the current loop iteration in Java, use the continue statement. It immediately moves on to the following loop iteration. With the for loop, while loop, and do-while loop, we can also use the continue statement.
jump-statement;
continue;
Example:
In this example, we are using continue statement inside the for loop. See, it does not print 5 to the console because at fifth iteration continue statement skips the iteration that’s why print statement does not execute.
public class ContinueDemo1
{
public static void main(String[] args)
{
for(inti=1;i<=10;i++)
{
if(i==5)
{
continue;
}
System.out.println(i);
}
}
}
Example:
We can use label along with continue statement to set flow control. By using label, we can transfer control at specified location.
In this example, we are transferring control to outer loop by using label.
public class ContinueDemo2 {
public static void main(String[] args) {
xy:
for(inti=1;i<=5;i++){
pq:
for(int j=1;j<=5;j++){
if(i==2&&j==2){
continue xy;
}
System.out.println(i+" "+j);
}
}
}
}
Example: Continue in While loop
While loops can be used with the continue statement to manage programme flow. As is already known, the continue statement is employed to omit the current loop iteration. The execution will be skipped once more if the variable’s value is 5.
public class Demo{
public static void main(String[] args) {
int i=1;
while (i < 10) {
if (i == 5) {
i++;
continue;
}
System.out.println(i);
i++;
}
}
}
1 2 3 4 6 7 8 9
we can see the output, 5 is missing because at fifth iteration due to continue statement JVM skipped the print statement.