Zirak
Zirak

Reputation: 39848

Continue statements

Wondering what a continue statement does in a do...while(false) loop, I mocked up a simple test-case (pseudo-code):

count = 0;
do {
    output(count);
    count++;
    if (count < 10)
        continue;
}while (false);

output('out of loop');

The output was, to my surprise:

0
out of loop

A bit confused, I changed the loop from a do...while to a for:

for (count = 0; count == 0; count++) {
    output(count);
    if (count < 10)
        continue;
}
output('out of loop');

While functionally not the same, the purpose is practically the same: Make a condition only satisfied the first iteration, and in next ones continue (until a certain value is reached, purely for stopping possible infinite-loops.) They might not run the same amount of times, but functionality here isn't the important bit.

The output was the same as before:

0
out of loop

Now, put into terms of a simple while loop:

count = 0;
while (count == 0) {
    output(count);
    count++;
    if (count < 10)
        continue;
}
output('out of loop');

Once again, same output.

This is a bit confusing, as I've always thought of the continue statement as "jump to the next iteration". So, here I ask: What does a continue statement do in each of these loops? Does it just jump to the condition?

((For what it's worth, I tested the above in JavaScript, but I believe it's language-agnostic...js had to get at least that right))

Upvotes: 5

Views: 260

Answers (4)

user2100815
user2100815

Reputation:

It's best to think of continue as jumping to the end of the enclosing loop. This may haelp:

#include <iostream>
using namespace std;

int main() {

    int n = 0;

    do {
        cout << n << endl;
        n += 1;
        if ( n == 3 ) {
            continue;
        }
        cout << "n was not 3" << endl;
    } while( n != 3 );

}

which prints:

0
n was not 3
1
n was not 3
2

and terminates, because the continue jumps to the while() at the end of the loop. similar stiff happens for for() and while() loops.

Upvotes: 1

Brad
Brad

Reputation: 5488

continue skips to the next iteration when it is used in a loop. break exits the current block. Typically, break is used to exit a loop but it could be used to exit any block.

for (int i = 0; i < 1000; i++) {
  if (some_condition) {
    continue; // would skip to the next iteration
  }

  if (some_other_condition) {
    break; // Exits the loop (block)
  }

  // other work
}

Upvotes: 0

JosephH
JosephH

Reputation: 8825

Your definition of continue statement as "jump to the next iteration" is correct. This will force the program to start next iteration by first re-evaluating the conditional expression.

The problem with your snippets is that they all exit after one iteration because your conditional expressions are set to either false or count ==0. This will always return false after one iteration.

Moreover, putting continue statement at the end of the loop is meaningless. It will re-evaluate the conditional expression in either case.

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89232

In a for loop, continue runs the 3rd expression of the for statement (usually used as some kind of iteration), then the condition (2nd expression), and then the loop if the condition is true. It does not run the rest of the current iteration of the loop.

In a while (or do-while) loop, it just runs the condition and then the loop if the condition holds. It also does not run the rest of the current iteration of the loop.

Upvotes: 5

Related Questions