×

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

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 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.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

For Loop Examples in C++

For Loop A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax of for loop In C++, a...

6 minutes read.

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...

4 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Reverse a Number in C++

In this tutorial, you'll learn how to utilize a C++ program to reverse a number entered by a user at runtime. Because there are various ways to write a C++...

3 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.