tags:

views:

3151

answers:

15

Can anyone tell me the difference between break and continue statements?Thanks.

+28  A: 

Break leaves a loop, continue jumps to the next iteration.

Peter
You're fast. Enjoy your silver badge.
Michael Myers
I hope I'l get my first, if he accepts the answer... Seems a lot of people ask questions at SO, but don't accept an answer if there is one...
Peter
Yeahaaa, got it, thx *g*
Peter
Note that Java also contains labeled continue/break statements which have different semantics :-)
Jay
This is just the basics of break and continue. For a better explanation look at Jay's Post
Peter
lol - this trivial question and answer generated 400+ points on SO
mson
+4  A: 

break completely exits the loop. Continue skips the statements after the continue statement and keeps looping.

jsight
+2  A: 

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.

Warrior
+1  A: 

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.

Graeme Perrow
+1  A: 

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.

Electrons_Ahoy
+1  A: 

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.

Gumbo
+21  A: 

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.

Jay
+1  A: 

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:;
}
Michael Buen
In case some was wondering how to do it in C# ??? :)
OscarRyz
Doesn't C# have break; and continue; statements? I can't believe it.
OscarRyz
Yeah C# have, I just explain the semantics of break and continue :-)
Michael Buen
A: 

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
OscarRyz
A: 

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.

A: 

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
Kevin Loney
You've got variable scoping problems in your examples.
Darron
I'm aware of that I was being lazy.
Kevin Loney
A: 

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
  |     ....    
  |}   
  |>
Johannes Schaub - litb
A: 

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
Markus Lausberg
A: 

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

Tamanna. JAGANNATH UNIVERSITY
A: 

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.

Pinki