Can anyone tell me the difference between break and continue statements?Thanks.
break completely exits the loop. Continue skips the statements after the continue statement and keeps looping.
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 and return control to the loop statement.
The break
statement breaks out of the loop (the next statement to be executed is the first one after the closing brace), while continue
starts the loop over at the next iteration.
Simply put: break will terminate the current loop, and continue execution at the first line after the loop ends. continue jumps back to the loop condition and keeps running the loop.
The break
statement exists the current looping control structure and jumps behind it while the continue
exits too but jumping back to the looping condition.
See Branching Statements for more details and code samples:
break
The break statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch statement. You can also use an unlabeled break to terminate a for, while, or do-while loop [...]
An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.
continue
The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. [...]
A labeled continue statement skips the current iteration of an outer loop marked with the given label.
here's the semantic of break:
int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 };
// find 9
for(int i = 0; i < a.Length; i++)
{
if (a[i] == 9)
goto goBreak;
Console.WriteLine(a[i].ToString());
}
goBreak:;
here's the semantic of continue:
int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 };
// skip all odds
for(int i = 0; i < a.Length; i++)
{
if (a[i] % 2 == 1)
goto goContinue;
Console.WriteLine(a[i].ToString());
goContinue:;
}
Excellent answer simple and accurate.
I would add a code sample.
C:\oreyes\samples\java\breakcontinue>type BreakContinue.java
class BreakContinue {
public static void main( String [] args ) {
for( int i = 0 ; i < 10 ; i++ ) {
if( i % 2 == 0) { // if pair, will jump
continue; // don't go to "System.out.print" below.
}
System.out.println("The number is " + i );
if( i == 7 ) {
break; // will end the execution, 8,9 wont be processed
}
}
}
}
C:\oreyes\samples\java\breakcontinue>java BreakContinue
The number is 1
The number is 3
The number is 5
The number is 7
so you are inside a for or while loop. Using break; will put you outside of the loop. As in, it will end. Continue; will tell it to run the next iteration.
No point in using continue in if statement, but break; is useful. In switch...case, always use break; to end a case, so it does not executes another case.
Consider the following:
int n;
for(n = 0; n < 10; ++n) {
break;
}
System.out.println(n);
break causes the loop to terminate and the value of n is 0.
int n;
for(n = 0; n < 10; ++n) {
continue;
}
System.out.println(n);
continue causes the program counter to return to the first line of the loop (the condition is checked and the value of n is increment) and the final value of n is 10.
It should also be noted that break only terminates the execution of the loop it is within:
int m;
for(m = 0; m < 5; ++m)
{
int n;
for(n = 0; n < 5; ++n) {
break;
}
System.out.println(n);
}
System.out.println(m);
Will output something to the effect of
0
0
0
0
0
5
I'm going to put a graphic
|>for(int i=0; i < 10; i++) {
| ....
------continue; // goto next iteration
....
}
for(int i=0; i < 10; i++) {
....
------break; // jump out of this loop
| ....
|}
|>
Simple Example:
Break leave the loop
int m = 0;
for(int n = 0; n < 5; ++n){
if(n == 2){
break;
}
m++;
}
System.out.printl("m:"+m); // m:2
Continue will go back to start loop.
int m = 0;
for(int n = 0; n < 5; ++n){
if(n == 2){
continue; // Go back to start and dont execute m++
}
m++;
}
System.out.printl("m:"+m); // m:4
Break causes the loop to be terminated. but continue causes the loop to be continued with the next iteration after skipping any statements in between
Break statement ends the whole loop in which it is written while continue statement ends the current iteration but does not end the whole loop.