Difference between revisions of "cpp/language/continue"
From cppreference.com
m (r2.7.3) (Robot: Adding pt:cpp/language/continue) |
m (→Title: tt keyword.) |
||
(11 intermediate revisions by 7 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 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. | ||
===Syntax=== | ===Syntax=== | ||
− | + | {{sdsc begin}} | |
− | {{ | + | {{sdsc|{{spar optional|attr}} {{ttb|continue}} {{ttb|;}}}} |
− | {{ | + | {{sdsc end}} |
− | {{ | + | |
===Explanation=== | ===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 (/* ... */); | ||
+ | }} | ||
− | {{ltt|cpp/ | + | For {{rlp|for}} and {{rlp|range-for}} loop, it acts as: |
+ | {{source|1= | ||
+ | for (/* ... */) | ||
+ | { | ||
+ | // ... | ||
+ | continue; // acts as goto contin; | ||
+ | // ... | ||
+ | contin:; | ||
+ | } | ||
+ | }} | ||
+ | |||
+ | ===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) continue; | + | { |
− | std::cout << i << | + | if (i != 5) |
+ | continue; | ||
+ | std::cout << i << ' '; // this statement is skipped each time i != 5 | ||
} | } | ||
− | |||
std::cout << '\n'; | std::cout << '\n'; | ||
− | 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
|