Skip to content Skip to sidebar Skip to footer

C++ Continue Statement – Learn C++ Programming

The programming isn’t less than a magical spell; you say it, and the spell is executed. Isn’t it something like magic? But in magic also need to have control over the spell. Otherwise, it wouldn’t take long to get the situation out. In this blog, we will control the magic of the programming loops using C++ continue statements.

As in programming, we also need to keep an eye on the program flow; that’s an essential part of the programming world and the primary reason we’ve to learn loops, condition statements, break statements, continue statements, and switch-case statements as well.

Break and Continue are two critical statements we must learn to enhance the program control skills. This blog is about the continue statement, how it works with diagrammatical explanation, syntax, and working of continue statement with loops.

Continue Statement

Shall we continue the journey? Geeks are asking you to shift the new gear in programming by learning C++ continue statements.
Shall we continue the journey? Geeks are asking you to shift the new gear in programming by learning C++ continue statements.

The continue statement is another loop control statement, which works like a break statement, interrupting the current loop iteration and taking over the program control. The Continue statement is used to skip an iteration in loops.

It would be more sensible to skip a part of the loop statements, which should be avoided to make more sense, opening various other opportunities and scope in the programming.

You will be able to get more clarity after the end of this blog and after you get practical exposure.

Diagram

The C++ Continue Diagram
The C++ Continue Diagram

Explanation

This diagram could seem a little bit complex, but it’s literally a piece of cake:

  • Beginning with the program flow entering the loop, it faces whether it has to go inside or skip it.
  • When it enters the while loop and executes the loop body completely, there comes a situation powered by our beloved if else condition.
  • If the situation enters inside the if statement, then the current iteration will be skipped right away, and it will again continue the iteration of the loop until it gets satisfied.
  • Otherwise, the current iteration will also be executed as it was supposed to be, and the loop will be continued as it is supposed to until it satisfies the situation.

Syntax

Continue Statement with Loops

The continue statement is majorly used with loops to skip the current iteration in the program flow wherever needed, according to the logic of the program.

It is mainly used with conditional statements to control the program flow and give the correct direction to increase the program’s efficiency and accuracy, reducing the scope of exceptions and errors.

Simple Loops

Simple Loop/Normal Loop/Standard Loop means simple for, while, do-while loop without sub-types and complications like nested, infinite, etc. Continue statements can be implemented in any loop. Still, here we’re considering a simple illustration of implementing continue statements in simple loops and how the continue statement works in this situation.

Example:

#include <iostream>
using namespace std;

int main(){
    
    for (int i=1;i<=15;i++){
        if(i==9){
            continue;
        }
        else{
            cout<<i<<endl;
        }
    }
}

Output:

The Output in Simple Loops
The Output in Simple Loops

Explanation:

Simple Loops with continued explanations for continuation in life:

  • Getting inside the for loop in the first place, where the iterator runs from 1 until 15 (including 15), and the iterator is incrementing on the default rate, i.e., 1.
  • Inside the for loop, the decision-making statement welcomes us, stating that if iterator “i” becomes 9, it will skip the current iteration of the loop due to the continue statement. Otherwise, the numbers will be printed.
  • It’s an elementary program printing numbers from 1 to 15 in ascending/counting order. Still, the catch is that the numbers will be printed until 15 without 9, which will be skipped due to the continue statement. Other numbers will be printed as per the program flow to complete execution.
  • You can edit 9 and change it to any number you want the loop to skip. Always try playing around with the program, as it is the best source of learning, instead of reading this blog and copying and pasting the codes on the compiler.

Nested Loops

Nested loops are used to increase the code’s efficiency and open a gateway and numerous possibilities for new concepts. By including the ingredient named continue statement in the nested loops dish, the situation and the possibility gets to the next level, making the final dish much tastier than expected. And from the chef’s perspective, it gets much easier to implement the recipe because of the continue statement itself.

You must be able to interpret that by a chef, I mean programmer, and the continue statement makes it much easier to control the program flow, even in nested loops. Let’s understand it with another geeky example.

Example:

#include <iostream>
using namespace std;

int main(){
    
    for (int i=1;i<=5;i++){
        cout<<"Table of "<<i<<endl<<endl;
        for (int j=1;j<=10;j++){
            if (j==8){
                continue;
            }
            cout<<i*j<<"\t";
        }
        cout<<endl;
        cout<<endl;
    }
}

Output:

The Output in Nested loops
The Output in Nested loops

Explanation:

If you didn’t notice till now, the 8th Multiplication of every number is skipped. Let’s understand how:

  • Getting the first for loop, i.e., the outer loop, which has the iterator initializing at 1, running until 5 (including 5), and incrementing by 1 as usual.
  • Printing the heading “Table of iterator i,” and here, the innermost loop comes into play, the inner for loop. It initializes the iterator j from 1 to 10 (including 10) with a simple increment of j by 1.
  • These loops print the mathematical tables from 1 to 5 with the heading. The catch is the 8th iteration or the 8th multiplication in the table is skipped from every table.
  • When the table was getting printed by “i” and “j” multiplying together, in every table, there’s a situation when j will turn 8 to multiply. Instead, it’ll be forced into the if block to skip and continue the execution. Hence 8th iteration of every table is skipped.
  • Here also, you must try playing with the code to learn how the programming works and how your intervention affects the program’s logic, as it is yet the best source of learning, instead of just reading, copying, and pasting the codes on the compiler.

Program: Sum of the First n Natual Even Numbers

#include <iostream>
using namespace std;

int main(){
    
    //Part 1: Initialization & Accepting Data
    
    int num;
    cout<<"Enter Number: ";
    cin>>num;
    int sum = 0;
    
    //Part 2: The Main Operation
    
    cout<<"The Positive Numbers --> "<<endl;
    
    for(int i=1; i<=num ; i++){
        if (i%2 == 0){
            cout<<i<<endl;
            sum +=i ;
        }
        else{
            continue;
        }
    }
    
    //Part 3: Printing The Total
    
    cout<<"The Total Sum: "<<sum;
    
    return 0;
}

Output

The Output of the Program
The Output of the Program

Explanation

This program has been taken from for loop blog. If you would like a complete understanding, you can visit the for loop blog here.

Note: Please make sure to code and run the program manually on onlinegdb for better practical exposure.

Objective: The program will take user input and print the even numbers and their sum until the user in their input provides the last even number.

Let’s get the ultimate explanation for the strange lines of code provided above:

  • In the first part, we initialized a variable “num,” accepted a value from the user, and stored it in the “num” variable.
    //Part 1: Initialization & Accepting Data
    
    int num;
    cout<<"Enter Number: ";
    cin>>num;
    int sum = 0;
  • In the second part, we’ve put the for loop in which the iterator starts from 1, running until the number provided by the user, “num,” and incrementing by default,i.e., 1.
  • Inside the for loop, a conditional statement prints the number and adds the value to the sum only when the number is adequately divisible by 2 and leaves the remainder 0, which means an even number.
  • If the number is not divisible by 2, the number is passed into the else block with the continue statement. That means all odd numbers are passed in continue statements which skip the operation with the current number (current session), move on and continue the iteration with the next number.
  • Interpreting this, the if statement only considers the number and performs the printing and the addition operation if the number is an even number. Otherwise, any odd number will fall into the else block and be skipped from the operation; hence the program flow will get to the next number and perform the very same operation of the loop.
    //Part 2: The Main Operation
    
    cout<<"The Positive Numbers --> "<<endl;
    
    for(int i=1; i<=num ; i++){
        if (i%2 == 0){
            cout<<i<<endl;
            sum +=i ;
        }
        else{
            continue;
        }
  • In the third part of the program, the total sum of the even numbers is printed. They were also printed during the iteration process; the total sum is printed at the end.
    //Part 3: Printing The Total
    
    cout<<"The Total Sum: "<<sum;

Conclusion

The deflecting lines continue the journey of C++ while providing you the blog's conclusion.
The deflecting lines continue the journey of C++ while providing you the blog’s conclusion.

In this blog, we’ve thoroughly gained knowledge of the continue statements and gone through the concepts of the continue in normal and nested loops. We’ve also gone through some real-life examples, programs, and diagrams that should help you to solve the framed interview questions.

In the upcoming blogs, you’ll get exposure to continue statements and switch statements. This will complete the entire tutorial series course for C++ beginners to intermediate. After the C++ beginners to intermediate, the geeks plan to expand the peak with more extensive tutorials to enhance the learning base and provide more exclusive and practical learning.

This is also one of the most detailed blogs ever, and it took a lot of money & effort to upgrade the blog’s quality to this level. We’ve just begun. We’ve constantly improved the quality to enhance your experience, which shall require your support, and we expect it from you.

We shall start accepting donations soon as we’re spending heavily on the website’s maintenance and super high-quality content creation & research of the website. You can now support us through the like button on the bottom, sharing this blog, disabling ad blockers, commenting on your thoughts, and connecting with us on socials.

You can also support us by visiting this blog and buying the reviewed services (They’re self-tested and the best in the market), which will help us grow, develop, and upgrade the content quality to the next level. You may also subscribe to our newsletters, check out our latest blogs from other niches, and hit a like (heart) below the blog. Perfect Goodbye for now, and enjoy the ever-best programming blog series only at GeekonPeak!

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates