What is the difference between a break statement and a continue statement

A break statement results in the termination of the statement to which it applies (switch,for, do, or while). A continue statement is used to end the current loop iteration andreturn control to the loop statement.

Showing Answers 1 - 4 of 4 Answers

Richa

  • Sep 8th, 2005
 

break statement  
A statement that terminates the current loop iteration and causes execution to break out of a loop. 
 
continue statement  
A statement that terminates the current loop iteration and persists the loop with the next iteration.

  Was this answer useful?  Yes

When you are dealing with loops in your code (like for , while , do-while not if or switch)
continue will go back and increase the loop counter and start executing. all the statements below the continue in that loop would be skipped.

In case of break we exit out of the loop and go ahead.

The break keyword halts the execution of the current loop and forces control out of the loop.

The continue is similar to break, except that instead of halting the execution of the loop, it starts the next iteration.

  Was this answer useful?  Yes

Arvindanathan

  • Dec 3rd, 2013
 

Code
  1. public class BreakAndContinue

  2. {

  3.         public static void main(String[] args)

  4.         {

  5.  

  6.                 for (int i = 0; i < 100; i++)

  7.                 {

  8.                         if(i==10)

  9.                         {

  10.                                 break;

  11.                         }

  12.                         if(i==5)

  13.                         {

  14.                                 continue;

  15.                         }

  16.                         System.out.println("I : "+i);          

  17.                 }      

  18.         }

  19.  

  20. }

  21. //Output :

  22. I : 0

  23. I : 1

  24. I : 2

  25. I : 3

  26. I : 4

  27. I : 6

  28. I : 7

  29. I : 8

  30. I : 9

  31.  

  32.  


Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions