Difference between revisions of "cpp/language/continue"
From cppreference.com
(details) |
m (Typo) |
||
Line 1: | Line 1: | ||
{{title|continue statement}} | {{title|continue statement}} | ||
{{cpp/language/statements/navbar}} | {{cpp/language/statements/navbar}} | ||
− | Causes the remaining portion of the enclosing {{rlp|for}}, {{rlp|range-for}}, {{rlp|while}} or {{rlp|do | do-while}} loop body skipped. | + | Causes the remaining portion of the enclosing {{rlp|for}}, {{rlp|range-for}}, {{rlp|while}} or {{rlp|do | do-while}} loop body to be skipped. |
Used when it is otherwise awkward to ignore the remaining portion of the loop using conditional statements. | Used when it is otherwise awkward to ignore the remaining portion of the loop using conditional statements. |
Revision as of 15:56, 25 February 2016
Causes the remaining portion of the enclosing for, range-for, while or do-while loop body to be skipped.
Used when it is otherwise awkward to ignore the remaining portion of the loop using conditional statements.
Contents |
Syntax
attr(optional) continue ;
|
|||||||||
Explanation
The continue
statement causes a jump, as if by goto to the end of the loop body (it may only appear within the loop body of for, range-for, while, and do-while loops).
More precisely,
For while loop, it acts as
while (/* ... */) { // ... continue; // acts as goto contin; // ... contin:; }
For do-while loop, it acts as:
do { // ... continue; // acts as goto contin; // ... contin:; } while (/* ... */);
For for and range-for loop, it acts as:
for (/* ... */) { // ... continue; // acts as goto contin; // ... contin:; }
Keywords
Example
Run this code
#include <iostream> int main() { for (int i = 0; i < 10; i++) { if (i != 5) continue; std::cout << i << " "; //this statement is skipped each time i!=5 } std::cout << '\n'; for (int j = 0; j < 2; j++) { for (int k = 0; k < 5; k++) { //only this loop is affected by continue if (k == 3) continue; std::cout << j << k << " "; //this statement is skipped each time k==3 } } }
Output:
5 00 01 02 04 10 11 12 14
See also
C documentation for continue
|