Difference between revisions of "cpp/language/continue"
From cppreference.com
m (→Title: tt keyword.) |
|||
(29 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
− | {{title|continue statement}} | + | {{title|{{tt|continue}} statement}} |
− | {{cpp/language/ | + | {{cpp/language/statements/navbar}} |
− | Causes the remaining portion of the | + | 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. | ||
− | ==Syntax== | + | ===Syntax=== |
+ | {{sdsc begin}} | ||
+ | {{sdsc|{{spar optional|attr}} {{ttb|continue}} {{ttb|;}}}} | ||
+ | {{sdsc end}} | ||
− | {{ | + | ===Explanation=== |
− | {{ | + | The {{tt|continue}} statement causes a jump, as if by {{rlp|goto}} to the end of the loop body (it may only appear within the loop body of {{rlp|for}}, {{rlp|range-for}}, {{rlp|while}}, and {{rlp|do|do-while}} loops). |
− | {{ | + | |
− | + | More precisely, | |
− | + | For {{rlp|while}} loop, it acts as | |
+ | {{source|1= | ||
+ | while (/* ... */) | ||
+ | { | ||
+ | // ... | ||
+ | continue; // acts as goto contin; | ||
+ | // ... | ||
+ | contin:; | ||
+ | } | ||
+ | }} | ||
− | + | For {{rlp|do|do-while}} loop, it acts as: | |
+ | {{source|1= | ||
+ | do | ||
+ | { | ||
+ | // ... | ||
+ | continue; // acts as goto contin; | ||
+ | // ... | ||
+ | contin:; | ||
+ | } while (/* ... */); | ||
+ | }} | ||
− | = | + | For {{rlp|for}} and {{rlp|range-for}} loop, it acts as: |
+ | {{source|1= | ||
+ | for (/* ... */) | ||
+ | { | ||
+ | // ... | ||
+ | continue; // acts as goto contin; | ||
+ | // ... | ||
+ | contin:; | ||
+ | } | ||
+ | }} | ||
− | {{ltt|cpp/ | + | ===Keywords=== |
+ | {{ltt|cpp/keyword/continue}} | ||
− | ==Example== | + | ===Example=== |
− | {{example | + | {{example |
− | + | |code= | |
#include <iostream> | #include <iostream> | ||
− | int main() | + | int main() |
{ | { | ||
− | for (int i = 0; i < 10; | + | for (int i = 0; i < 10; ++i) |
− | if (i!=5) | + | { |
+ | if (i != 5) | ||
continue; | continue; | ||
− | + | std::cout << i << ' '; // this statement is skipped each time i != 5 | |
− | std::cout << i << | + | |
} | } | ||
− | + | std::cout << '\n'; | |
− | std::cout << | + | |
− | for (int j = 0; j | + | for (int j = 0; 2 != j; ++j) |
− | for (int k = 0; k < 5; | + | for (int k = 0; k < 5; ++k) // only this loop is affected by continue |
− | if (k == 3) continue; | + | { |
− | std::cout << j << k << " "; | + | if (k == 3) |
+ | continue; | ||
+ | // this statement is skipped each time k == 3: | ||
+ | std::cout << '(' << j << ',' << k << ") "; | ||
} | } | ||
− | + | std::cout << '\n'; | |
− | + | ||
− | + | ||
} | } | ||
− | + | |output= | |
5 | 5 | ||
− | + | (0,0) (0,1) (0,2) (0,4) (1,0) (1,1) (1,2) (1,4) | |
}} | }} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc see c|c/language/continue}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 07:50, 19 June 2024
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 |
[edit] Syntax
attr (optional) continue ;
|
|||||||||
[edit] 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:; }
[edit] Keywords
[edit] 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; 2 != j; ++j) for (int k = 0; k < 5; ++k) // only this loop is affected by continue { if (k == 3) continue; // this statement is skipped each time k == 3: std::cout << '(' << j << ',' << k << ") "; } std::cout << '\n'; }
Output:
5 (0,0) (0,1) (0,2) (0,4) (1,0) (1,1) (1,2) (1,4)
[edit] See also
C documentation for continue
|