15

Which statement will be executed after "continue" or "break" ?

for(int i = 0; i < count; ++i)
 {
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             continue;
     }
     //statement3                                                                                                                                                                                                                           
 }

for(int i = 0; i < count; ++i)
 {   
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {   
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             break;
     }
     //statement3                                                                                                                                                                                                                           
 }
eugene
  • 39,839
  • 68
  • 255
  • 489

6 Answers6

24

continue: ++j and then if j < count then statement2 otherwise statement3

break: statement3

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • For the first case: not exactly. After ++j, the loop test (j < count) will be carried out and if true, then statement2. – subhacom Jun 16 '11 at 07:00
22

Continue jumps straight to the top of the innermost loop, where the per-iteration code and continuance check will be carried out (sections 3 and 2 of the for loop).

Break jumps straight to immediately after the innermost loop without changing anything.

It may be easier to think of the former jumping to the closing brace of the innermost loop while the latter jumps just beyond it.

jpm
  • 3,165
  • 17
  • 24
  • Sorry, but -1 for not actually answering the question, in that "continue jumps... incrementing the counter", but this doesn't describe the next step - retesting the condition, and that that determines whether statement 2 or 3 executes. It might seem too obvious to spell out, but then when there's a question about break and continue these things can't be taken for granted. :-/. – Tony Delroy Jun 16 '11 at 07:14
  • @paxdiablo Thanks for the edit. It added quite a bit of clarity. – jpm Jun 16 '11 at 10:25
11

continue ends the current iteration, virtually it is the same as:

for(int i = 0; i < count; ++i)
 {
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             goto end_of_loop;
end_of_loop:
     }
     //statement3                                                                                                                                                                                                                           
 }

break exits the loop:

for(int i = 0; i < count; ++i)
 {   
     // statement1                                                                                                                                                                                                                          
     for(int j = 0; j < count; ++j)
     {   
         //statement2                                                                                                                                                                                                                       
         if(someTest)
             goto after_loop;
     }
after_loop:
     //statement3                                                                                                                                                                                                                           
 }
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
  • 4
    +1 A potentially easy-to-follow way to explain it, but would have been even clearer if there was some code in the loop after the test (I know - not your fault, just the way the question was). – Tony Delroy Jun 16 '11 at 07:23
2

Continue: It depends. The continue statement will execute the 'increment' part of the for-loop, then the 'test' part, and then decide whether to execute the next iteration or leave the loop. So it could be statement 2 or 3.

Break: statement 3.

Btw, is this homework?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Patrick
  • 23,217
  • 12
  • 67
  • 130
2

statement2 will execute after the continue, given that the loop was not in the last iteration.

statement3 will execute after the break.

'continue' (as the name suggests) continues the loop, while skipping the rest of the statements in the current iteration.

'break' breaks and exits from the loop.

sparkymat
  • 9,938
  • 3
  • 30
  • 48
-1
  1. For continue, innerloop is executed with new i,j values of i,j+1

  2. For break, innerloop is executed with new i,j values of i+1,0

ofcourse if boundary conditions are satisfied

Adithya Surampudi
  • 4,354
  • 1
  • 17
  • 17