Difference between continue and break statements in Java


As we know in programming execution of code is done line by line.Now in order to alter this flow C++ provides two statements break and coninue which mainly used to skip some specific code at specific line.

Following are the important differences between continue and break.

Sr. No.KeyBreakContinue
1FunctionalityBreak statement mainly used to terminate the enclosing loop such as while, do-while, for or switch statement wherever break is declared.Continue statement mainly skip the rest of loop wherever continue is declared and execute the next iteration.
2Executional flowBreak statement resumes the control of the program to the end of loop and made executional flow outside that loop.Continue statement resumes the control of the program to the next iteration of that loop enclosing 'continue' and made executional flow inside the loop again.
3UsageAs mentioned break is used for the termination of enclosing loop.On other hand continue causes early execution of the next iteration of the enclosing loop.
4CompatibilityBreak statement can be used and compatible with 'switch', 'label'.We can't use continue statement with 'switch','lablel' as it is not compatible with them.

Example of Continue vs Break

JavaTester.java

Example

 Live Demo

public class JavaTester{
   public static void main(String args[]){
      // Illustrating break statement (execution stops when value of i becomes to 4.)
      System.out.println("Break Statement\n");
      for(int i=1;i<=5;i++){
         if(i==4) break;
         System.out.println(i);
      }
      // Illustrating continue statement (execution skipped when value of i becomes to 1.)
      System.out.println("Continue Statement\n");
      for(int i=1;i<=5;i++){
         if(i==1) continue;
         System.out.println(i);
      }
   }
}

Output

Break Statement
1
2
3

Continue Statement
2
3
4
5

Updated on: 02-Mar-2020

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements