Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ if Statement
Next article icon

continue Statement in C++

Last Updated : 04 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

C++ continue statement is a loop control statement that forces the program control to execute the next iteration of the loop. As a result, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Syntax:

continue;
Continue statement in C++

Flowchart of continue Statement in C++

Example: Consider the situation when you need to write a program that prints numbers from 1 to 10 but not 4. It is specified that you have to do this using loop and only one loop is allowed to use. Here comes the usage of the continue statement. What we can do here is we can run a loop from 1 to 10 and every time we have to compare the value of the iterator with 4. If it is equal to 4 we will use the continue statement to continue to the next iteration without printing anything otherwise we will print the value. 

Continue Statement with for Loop

If continue is used in a for loop, the control flow jumps to the test expression after skipping the current iteration.

Continue statement with for loop

Continue statement in for loop

Example:

C++




// C++ program to explain the use
// of continue statement with for loop
  
#include <iostream>
using namespace std;
  
int main()
{
    // loop from 1 to 10
    for (int i = 1; i <= 10; i++) {
  
        // If i is equals to 4,
        // continue to next iteration
        // without printing
        if (i == 4)
            continue;
  
        else
            // otherwise print the value of i
            cout << i << " ";
    }
  
    return 0;
}
 
 
Output
1 2 3 5 6 7 8 9 10 

The continue statement can be used with any other loop also like while or do while in a similar way as it is used with for loop above. 

Continue Statement with While Loop

With continue, the current iteration of the loop is skipped, and control flow resumes in the next iteration of while loop.

Continue Statement with While Loop

The continue statement in while loop

Example:

C++




// C++ program to explain the use
// of continue statement with while loop
  
#include <iostream>
using namespace std;
  
int main()
{
    int i = 0;
    // loop from 1 to 10
    while (i < 10) {
          i++;
        // If i is equals to 4,
        // continue to next iteration
        // without printing
        if (i == 4) {
            continue;
        }
  
        else {
            // otherwise print the value of i
            cout << i << " ";
        }
    }
  
    return 0;
}
 
 
Output
1 2 3 5 6 7 8 9 10 

Continue Statement with do-while Loop

In do-while loop, the condition is checked after executing the loop body. When a continue statement is encountered in the do-while loop, the control jumps directly to the condition checking for the next loop, and the remaining code in the loop body is skipped.

working of continue in do-while loop

The continue statement in do-while loop

Example:

C++




// C program to explain the use
// of continue statement with while loop
  
#include <iostream>
using namespace std;
  
int main()
{
  
    int i = 0;
    // loop from 1 to 10
    do {
  
        // If i is equals to 4,
        // continue to next iteration
        // without printing
        i++;
  
        if (i == 4) {
            continue;
        }
        else {
            // otherwise print the value of i
            cout << i << ' ';
        }
  
    } while (i < 10);
    return 0;
}
 
 
Output
1 2 3 5 6 7 8 9 10 

Here, 4 is not printed as a continue statement is encountered in the case of i = 4.

Note: In while and do-while loop, we updated the loop variable i before the continue statement or else we will keep encountering continue before updating the loop variable creating an infinite loop.

Continue Statement with Nested Loops

Using continue, you skip the current iteration of the inner loop when using nested loops. 

Example:

C++




// C++ program to explain the use
// of continue statement with nested loops
  
#include <iostream>
using namespace std;
  
int main()
{
  
    for (int i = 1; i <= 2; i++) {
        for (int j = 0; j <= 4; j++) {
            if (j == 2) {
                continue;
            }
            cout << j << " ";
        }
        cout << endl;
    }
  
    return 0;
}
 
 
Output
0 1 3 4   0 1 3 4 

Continue skips the current iteration of the inner loop when it executes in the above program. As a result, the program is controlled by the inner loop update expression. In this way, 2 is never displayed in the output.

Continue Statement vs Break Statement 

There are some differences between the continue and break statements which alter the normal flow of a program. 

Continue Break
Only the current loop iteration is stopped by a Continue statement. The break statement stops the loop in its entirety.
With Continue, subsequent iterations continue without interruption. The remaining iterations are also terminated by Break.
Continue statement can be used with for loop but it cannot be used with switch statements The break statement can be used with the switch statement
When you use the continue statement, and the control statements, the control remains inside the loop. When you use a break statement, the control exits from the loop.
The continue statement skips a particular loop iteration. The execution of the loop at a specific condition is stopped with a break statement.

Exercise Problem: Given a number n, print a triangular pattern. We are allowed to use only one loop.

Input: 7  Output:  *  * *   * * *  * * * *  * * * * *  * * * * * *  * * * * * * *

Solution: Print the pattern by using one loop | Set 2 (Using Continue Statement)



Next Article
C++ if Statement

H

Harsh Agarwal
Improve
Article Tags :
  • C++
  • CPP-Basics
Practice Tags :
  • CPP

Similar Reads

  • goto Statement in C++
    The goto statement in C++ is a control flow statement that provides for an unconditional jump to the same function's predefined label statement. In simple terms, the goto is a jump statement transfers the control flow of a program to a labeled statement within the scope of the same function, breakin
    5 min read
  • Compound Statements in C++
    Compound statements in C++ are blocks used to group multiple statements together into a single unit. These statements are enclosed in curly braces {} and can be used wherever a single statement is expected. The statements put inside curly braces are executed just like a single statement would have b
    3 min read
  • Jump statements in C++
    Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminate or continue the loop inside a program or to stop the execution of a function. In C++, there is four jump statement: Table of Content continue Statementbreak Statementreturn Statementgot
    4 min read
  • C++ if Statement
    The C++ if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not executed based on a certain condition. Let's take a look at an example: [GFGTABS] C++ #include <iostream> using namespace std; int
    3 min read
  • Naming Convention in C++
    Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for
    6 min read
  • Sleep Function in C++
    C++ provides the functionality of delay or inactive state of the program with the help of the operating system for a specific period of time. Other CPU operations will function adequately but the sleep() function in C++ will sleep the present executable for the specified time by the thread. The slee
    4 min read
  • std::function in C++
    The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases. Table of Content What is std::function in C++?Example of std::functionMember Functions of
    6 min read
  • copy_n() Function in C++ STL
    Copy_n() is the C++ function defined in <algorithm> library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size
    2 min read
  • Copy File To Vector in C++ STL
    Prerequisite:  Vectors in C++ STLFile Handling in C++ The C++ Standard Template Library (STL) provides several useful container classes that can be used to store and manipulate data. One of the most commonly used container classes is the vector. In this article, we will discuss how to copy the conte
    2 min read
  • String Functions in C++
    A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences