Difference between break and continue in Java

Introduction

In Java programming language, break and continue are called jump statements. The jumping statements are the control statements that transfer the program execution control to a specific statement. In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere. Jump statements are primarily used to interrupt loop or switch-case instantly.

The break Statement in Java

Sometimes, jumping out of a loop is necessary, irrespective of the conditional test value. The break statement is used inside any loop to allow the control to jump to the instant statement following the loop. 

Syntax 

if(condition)    
{    
   break;    
}   

How can we use the break statement in Java? 

When we use a nested loop, the break jumps the control from the loop where it has been used. The break statement can be used inside any loop, i.e., while, do-while, for, and switch statements. In Java, the use of break, the statement is to forcibly terminate the loop and resume at the next statement of the loop. As soon as the break statement is encountered inside the loop, that loop is immediately terminated without executing the remaining code in the body of that loop. The program control reaches the next statement written after that loop.

break_statement_flowchart 

The break statement is different from the exit. Break jumps the control out of the loop, and exit stops the execution of the entire program. The complete program of the break statement is listed below.

public class BreakExample1 {  
    public static void main(String args[]) {  
        int[] test = {0, 1, 2, 3, 4};  
        for (int s : test) {  
            if (s == 3) {  
                break;  
            }  
            System.out.print(s);  
            System.out.print("\n");  
        }  
        System.out.println("break statement execution");  
    }  
}   

Output

break-example1-output 

Explanation

In this program, the for loop is executed starting from s = 0 to 4 in steps of 1. When the condition (i==3) in the loop's body is satisfied, the break statement causes the control to move out of the loop.

Here is one more example program for understanding the working of the break statement in Java. The complete program is listed below.

public class BreakExample3 {  
    //using the break to exit the loop  
    public static void main(String args[]) {  
        for (int i = 0; i < 100; i++) {  
            System.out.println(+i);  
            if (i == 10)  
                break; // as the condition inside 'if' satisfies control comes out of 'for' loop.  
        }  
        System.out.println(" Loop breaks here");  
    }  
} 

Output

break-example3-output

Break statement with a nested loop

break only let the program exit the loop which encloses it. If the break is used with a nested loop, it breaks out only the innermost loop and does not affect the outer loop. It breaks the nested loop only if we use a break statement inside the nested loop. The complete program of break statements with a nested loop is listed below.

public class BreakExample2 {  
    public static void main(String[] args) {  
        for (int a = 1; a <= 3; a++) {  
            for (int b = 1; b <= 3; b++) {  
                if (a == 2 && b == 2) {  
                    break;  
                }  
                System.out.println(a + " " + b);  
            }  
        }  
    }  
}  

Output 

break-example2-output 

Explanation

In this program, the nested for loop execution started from a = 0 to 3 and b = 1 to 3 in steps of 1. When the condition (a == 2 && b ==2) in the loop's body is satisfied, the break statement causes the control to move out of the loop.

The labeled break statement

Java uses a break as a form of goto without its problem. As goto provides an unstructured branching which is hard to understand and also goto prohibits compiler optimization, Java uses an expanded form of break which is similar to goto and helps to exit more than one block at a time and resume the control of the program to the end of the labeled block, mentioned with the break statement.

Syntax

if(condition)    
break label_name;  

The complete program is listed below.

public class BreakExample4 {  
    //using break as a form of goto   
    public static void main(String args[]) {  
        boolean t = true;  
        first:{  
            second:{  
                third:{  
                    System.out.println(" this third block");  
                    if (t) break first;  
                }  
                System.out.println(" this second block");  
            }  
            System.out.println(" this first block");  
        }  
        System.out.println(" this main block");  
    }  
} 

Output

break-example4-output

The continue statement in Java 

Continue statement is sometimes required to skip some part of the loop and to continue the execution with the next loop iteration. Here, continue works somewhat as a break. As break terminates the remaining iteration of the loop and lets the control exit the loop. The continue statement is mainly used inside the loop to bypass the section of a loop and pass the control to the start of the loop to continue the execution with the next loop iteration.

Syntax

if(condition)    
{    
   continue;    
}   

How can we use the continue statement in Java? 

This statement is used only within looping statements. When the continue statement is encountered, it skips the current iteration, and the next iteration starts. 

continue_statement_feedback 

The remaining statements in the loop are skipped. The execution starts from the top of the loop again. We can use the continue statement to skip the current iteration and continue the next iteration inside loops. The complete program of continue statement is listed below.

public class ContinueExample {  
    public static void main(String[] args) {  
        int i;  
        for (i = 1; i <= 20; i++) {  
            if ((i % 5) == 0) {  
                continue;  
            }  
            System.out.println(i);  
        }  
    }  
}  

Output 

continue-example1-output 

Explanation

Here, the print statement is bypassed each time the value stored in (i) is divisible by 5.

Here is one more example program for understanding the workings of the break statement in Java. The complete program is listed below.

public class ContinueExample3 {  
//it prints the numbers which are divisible by 6 till 50.  
    public static void main(String args[]) {  
        for (int i = 0; i <= 50; i++) {  
            if (i % 6 != 0) continue; //  continue resume the control  to the next iteration in for loop  
            System.out.println(" i :  " + i);  
        }  
    }  
} 

Output

continue-example3-output

Explanation

 As the output, the following program prints the divisible numbers by 6 to 50.

Continue the statement with a nested loop 

The continue nested loop is used only if the continue statement is used inside the nested loop. The complete program of the continue statements with a nested loop is listed below.

public class ContinueExample2 {  
 public static void main(String[] args) {  
  for (int a = 1; a <= 3; a++) {  
   for (int b = 1; b <= 3; b++) {  
    if (a == 2 && b == 2) {  
     continue;  
    }  
    System.out.println(a + " " + b);  
   }  
  }  
 }  
}  

Output 

continue-example2-output

The labeled continue statement

The Java labeled loops allow transferring to a particular line or statement. We should go for labeled continue statements or nested loops to continue a particular loop. The complete program of labeled continue statement is listed below.

public class ContinueExample4 {  
    public static void main(String[] args) {  
        out:  
        for (int i = 1; i <= 3; i++) {  
            System.out.println(+i);  
            for (int j = 1; j <= 3; j++) {  
                System.out.println(+j);  
                if (j == 2) {  
                    // continue; this will skip second(j==2) iteration of inner for loop only  
                    continue out; // this will skip current iteration of both for loops  
                }  
            }  
        }  
    }  
} 

Output

continue-example4-output 

Explanation

The output outer for loop will iterate 100 times, but the inner for loop will iterate twice each time.

break vs continue 
 

break statement continue statement
It terminates the execution of the remaining iteration of the loop. It terminates only the current iteration of the loop.
Break resumes the program's control to the end of the loop, enclosing that 'break.' Continue resumes the program's control to the next iteration of that loop, enclosing continue.
It causes early termination of a loop. It causes early execution of the next iteration.
Break stops the continuation of the loop. Continuation of the loop. Continue does not stop the continuation of the loop; it only stops the current iteration.
The break can be used with a switch or label. Continue can not be executed with switches and labels.

Summary

In this article, we learned about the jump statements of the Java programming language, break statements, and continue statements and how we can use these statements in Java Programs. 


Similar Articles