3

According to ISO C++:

The continue statement shall occur only in an iteration-statement and causes control to pass to the loop-continuation portion of the smallest enclosing iteration-statement, that is, to the end of the loop. More precisely, in each of the statements

while (foo) {           do {                    for(;;){
  {                     {                       {
      // ...              //....                 //...
  }                     }                       }
  contin:;             contin:;                contin:;
  }                    } while (foo);           }

a continue not contained in an enclosed iteration statement is equivalent to goto contin

Based on the last part of the quote, I thought that the following would be allowed:

#include <iostream>
using namespace std;
int main() {
    continue;
    cout << "Will be jumped" << endl;
contin:
}

I thought this will work as a goto statement, jumping to contin. What did I miss?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • 2
    `continue` **only** works in loops – NathanOliver Sep 23 '21 at 12:06
  • 3
    It seems you misunderstand the examples. What is shown in the examples (as well as the examples in [this `continue` online reference](https://en.cppreference.com/w/cpp/language/continue)) is *equivalence* not actual behavior. Using `continue` ***in a loop*** is *equivalent* to using `goto some_label;` where `some_label` is placed at the end of the loop. – Some programmer dude Sep 23 '21 at 12:07
  • 2
    When the standard uses italics for identifiers like *contin*, those are exposition-only identifiers and cannot be referred to from C++ code. – Daniel Sep 23 '21 at 12:11

1 Answers1

4

This is slight phrasing issue. What the quote means is that in

for (;;) {
  {
    // ...
  }
contin: ;
}

The ... can be anything, including another iteration statement.

for (;;) {
  {
    while(foo()) {
        // ...
        continue;
    }
  }
contin: ;
}

The continue; that is not nested inside another looping construct, is going to be equivalent to goto contin;. But if it is contained, it will of course continue the internal loop, not the outer one.

Bear in mind that contin: ; is used for exposition purposes. It doesn't mean there's a literal C++-level label you can do things with.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458