Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/continue"

From cppreference.com
< cpp‎ | language
 
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/sidebar}}
+
{{cpp/language/statements/navbar}}
Causes the remaining portion of the nearest {{rlp|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}}
  
{{sdcl list begin}}
+
===Explanation===
{{sdcl list item | {{ttb|continue}}}}
+
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).
{{sdcl list end}}
+
  
==Explanation==
+
More precisely,
  
This statement works as a shortcut to the end of the nearest loop body.  
+
For {{rlp|while}} loop, it acts as
 +
{{source|1=
 +
while (/* ... */)
 +
{
 +
  // ...
 +
  continue; // acts as goto contin;
 +
  // ...
 +
  contin:;
 +
}
 +
}}
  
In case of {{rlp|while}} or {{rlp|do | do-while}} loops, the next statement executed is the condition check ({{sparam|cond_expression}}). In case of {{rlp|for}} loop, the next statements executed are the iteration expression and condition check ({{sparam|iteration_expression}}, {{sparam|cond_expression}}). After that the loop continues as normal.
+
For {{rlp|do|do-while}} loop, it acts as:
 +
{{source|1=
 +
do
 +
{
 +
    // ...
 +
    continue; // acts as goto contin;
 +
    // ...
 +
    contin:;
 +
} while (/* ... */);
 +
}}
  
==Keywords==
+
For {{rlp|for}} and {{rlp|range-for}} loop, it acts as:
 +
{{source|1=
 +
for (/* ... */)
 +
{
 +
    // ...
 +
    continue; // acts as goto contin;
 +
    // ...
 +
    contin:;
 +
}
 +
}}
  
{{ltt|cpp/keywords/continue}}
+
===Keywords===
 +
{{ltt|cpp/keyword/continue}}
  
==Example==
+
===Example===
{{example cpp
+
{{example
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
  
int main()  
+
int main()
 
{
 
{
     for (int i = 0; i < 10; i++) {
+
     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 << " ";       //this statement is skipped each time i!=5
+
 
     }
 
     }
   
+
     std::cout << '\n';
     std::cout << std::endl;
+
  
     for (int j = 0; j < 2; j++) {
+
     for (int j = 0; 2 != j; ++j)
         for (int k = 0; k < 5; k++) {        //only this loop is affected by continue
+
         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
+
             if (k == 3)
 +
                continue;
 +
            // this statement is skipped each time k == 3:
 +
             std::cout << '(' << j << ',' << k << ") ";
 
         }
 
         }
     }
+
     std::cout << '\n';
 
+
    return 0;
+
 
}
 
}
| output=
+
|output=
 
5
 
5
00 01 02 04 10 11 12 14
+
(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

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
continue - break
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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

continue

[edit] 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)

[edit] See also

C documentation for continue