continue statement
Terminates the current iteration.
Syntax
attr (optional) continue ; |
| attr | - | (since C++11) any number of attributes |
Explanation
continue statements must be enclosed by any of the following statements:
| (since C++26) |
For iteration statements
When executing a continue statement, if the innermost such enclosing statement is an iteration statement, then control passes to the end of the iteration element's statement (i.e. the loop body).
while (/* ... */)
{
/* executed statements */
continue;
/* skipped statements */
}
do
{
/* executed statements */
continue;
/* skipped statements */
} while (/* ... */)
for (/* ... */)
{
/* executed statements */
continue;
/* skipped statements */
}
For expansion statementsWhen executing a continue statement, if the innermost such enclosing statement is an expansion statement, then control passes to the end of the expansion element's current expansion item's compound-statement. template for (/* ... */)
{
if (/* current item is the Nth */)
continue;
/* statements only skipped for the Nth item */
}
| (since C++26) |
Keywords
Example
#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)
See also
C documentation for continue |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/language/continue