×

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and skip the current one.

Syntax:

It has the following syntax:

continue;

Working:

  • When the loop’s continue is reached, the rest of the code in the current iteration is skipped.
  • The loop subsequently executes the next iteration from there.
  • It is commonly used within while, for, and do-while loops.

Points to remember:

  1. The continue statement is not the same as break, which completely breaks the loop.
  2. When we need to skip particular iterations according to certain conditions, it is helpful.
  3. It works with all loop structures, including do-while, while, and for.

Key Features:

Several key features of continue statement in C++ are as follows:

  • Skips the Current Iteration: The loop skips the current iteration and proceeds to the next one without examining the remaining statements.
  • All loop types, including while, do-while, and for loops, are compatible with it.
  • Doesn’t Close the Loop: In contrast to break, continue only skips a few iterations instead of closing the loop.
  • Condition-based execution, typically used in if statements, enables us to purposefully omit particular iterations based on preset criteria.
  • Improves Code Readability: Highly nested conditional expressions can be avoided by avoiding pointless iterations.
  • Frequently Used in Filtering Operations: Frequently used in loops that need to skip specific values (e.g., reporting only odd or even numbers).

Algorithm:

  • Start
  • Initialize a loop that runs from i = 1 to i = 5.
  • Inside the loop, check if i == 3.

    • If true, execute continue, which skips the rest of the loop body for that iteration.
    • If false, print i.
  • The loop continues to the next iteration.
  • Once the loop completes all iterations, the program ends.

Pseudo code:

BEGIN

    FOR i FROM 1 TO 5 DO

        IF i EQUALS 3 THEN

            CONTINUE  // Skip this iteration

        ENDIF

        PRINT "Number: ", i

    ENDFOR

END

Example 1:

Let us take an example to illustrate the continue statement in C++.

#include <iostream>

using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {

            continue;  // Skips the rest of the loop body when i == 3

        }

        cout << "Number: " << i << endl;

    }

    return 0;

}

Output:

Number: 1

Number: 2

Number: 4

Number: 5

The result shows that the continue statement is run and the cout command is skipped for that iteration when i == 3.

Example 2:

Let us take another example to illustrate the continue statement for nested for loop in C++.

#include <iostream> 

using namespace std; 

int main() { 

    cout << "Output of Outer Loop" << endl; 

    for (int i = 1; i <= 12; i++) { 

        if (i == 5 || i == 10) { 

            continue; 

        }  

        cout << i << " "; 

    } 

    cout << endl << "Output of Nested Loop" << endl; 

    // Continue within inner loop 

    for (int x = 1; x <= 3; x++) { 

        for (int y = 0; y < 6; y++) { 

            if (y == 2) { 

                continue; 

            } 

            cout << x << y << " "; 

        } 

    } 

    return 0; 

}

Output:

Output of Outer Loop

1 2 3 4 6 7 8 9 11 12

Output of Nested Loop

10 11 13 14 15 20 21 23 24 25 30 31 33 34 35

Example 3:

Let us take another example to illustrate the continue statement using the While loop in C++.

#include <iostream>

using namespace std;

int main() {

    int i = 0;

    while (i < 5) {

        i++;  // Increment i at the start       

        // If i is 3, skip this iteration

        if (i == 3) {

            continue;

        }

        cout << "Number: " << i << endl;

    }

    return 0;

}

Output:

Number: 1

Number: 2

Number: 4

Number: 5

Explanation:

  • The loop keeps going till i < 5 and starts at i = 0.
  • The i variable is increased before the condition is checked.
  • When i == 3, continue is used instead of the cout command.
  • The loop proceeds to the next iteration without printing the third.

Example 4:

Let us take another example to illustrate continue statement using Do-While Loop in C++.

#include <iostream>

using namespace std;

int main() {

    int i = 0;

    do {

        i++;  // Increment i at the start      

        // If i is 3, skip this iteration

        if (i == 3) {

            continue;

        }

        cout << "Number: " << i << endl;

    } while (i < 5);

    return 0;

}

Output:

Number: 1

Number: 2

Number: 4

Number: 5

Explanation:

  • The do-while loop is used at least once.
  • The i variable is raised at the start of every repetition.
  • When I == 3, the continue statement is executed, leaving out cout << "Number: " << i << endl;.
  • In this cycle, i = 4 and i = 5 are used.

Conclusion:

In conclusion, programmers can skip some repetitions of a loop without completely ending it by using the continue statement due to C++’s strong control flow architecture. It is useful when one can afford to disregard certain requirements but wants to complete the loop. The continue statement is existent for the for, while, and do-while loops improves readability and efficiency by removing unnecessary nesting of conditional statements. Sometimes, the use of continue makes the explanation harder to follow, so it should be used judiciously. When understood correctly and used appropriately, programs are much clearer and more efficient, which gives us better control over how loops are executed.


Related Topics

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

How to create a stack in C++

A stack is a data structure, which is of linear type. A specific order has to be followed while inserting and deleting the elements from the stack. Generally, stack follows...

5 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages. C++ Function A function is a set of statements that executes a task....

5 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

C++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.