×

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

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

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.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

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.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.