A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cplusplus/cpp_continue_statement.htm below:

C++ continue Statement

C++ continue Statement The continue Statement

The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.

For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

Syntax

The syntax of a continue statement in C++ is −

continue;
Flow Diagram

Here is the following flow diagram showcasing a continuation statement in C++:

Example

Here is the following example of a continue statement in C++:

#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int a = 10;

   // do loop execution
   do {
      if( a == 15) {
         // skip the iteration.
         a = a + 1;
         continue;
      }
      cout << "value of a: " << a << endl;
      a = a + 1;
   } 
   while( a < 20 );
 
   return 0;
}
Output

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Explanation
  1. Firstly, the do-while loop will ensure that the block of code inside the do section executes at least once and then it will check the condition a < 20, and the loop will continue til it satisfies the condition.
  2. In the if statement, if a == 15, then a = a + 1 will increment the value by 1, and the continue statement will skip the 15; therefore, 15 will not be printed.
The continue Vs break Statements

The continue statement skips the remaining part of the current iteration and moves to the next iteration of the loop. This is basically used when a user wants to skip specific iterations based on a condition. It does not terminate the loop, it just transfers the execution to the next iteration. Whereas the break statement terminates the loop completely based on any specific condition given and stops the further execution of the loop.

Use Cases of Continue Statement

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4