×

Continue in C++ While loop

Continue statement:

Inside the loop, the continue statement is used to control the loop.

C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start of the loop and skips the current statement when it is reached. Continue statement is used in all the iteration control loops.

SYNTAX:

The syntax of the continue statement is: 
         continue;
  • Continue is a predefined keyword.
  • The continue statement can’t be used outside of the iteration loop. It only appears in iteration statements.

Flowchart:

The following image shows the flow chart of continue statements in loops:

Continue in C++ While loop

Explanation:

The statements within the loop are executed based on the loop condition. If the specified condition is true, the loop iteration will be continued to execute and the loop is terminated when the specified condition is false. The continue statement skips the current statement and passes control to the next loop iteration when the continue keyword is used inside a loop.

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 condition no longer met.

The syntax for the while loop:

The syntax for a while loop is given by:

while(condition)
                     { 
statement(x);
                     }

Usage of Continue Statement in While Loop:

When the continue is used in the While loop the control immediately jumps to the Boolean expression.

  while () {         
 {                                           
     // ...            
 }                                           
 continue;
 } 


Example 1: Program to print the list of numbers

Algorithm:

Step 1: Start the program

Step 2: Read n, i

Step 3: Initialise i=1

Step 4: iterate until I<=10 if the condition is false then go to step 4

Step 5: Print the value of I,

If (i==3)

Continue;

Else

go to step 4

Step 6: Stop.

#include<iostream>
using namespace std;
int main()
{
int i, n;
cout << "Enter a number";
cin >> n;
cout << "the numbers are";
i=1;
while(i <= n)
 {
   if(i == 3)
    {
     continue;
   }
 cout << i;
 i++;
 }
return 0;
}

The output of the program:

Enter a number: 8
The numbers are:
1
2
4
5
6
7
8

Explanation:

The program is designed to print the list of numbers which are entered by the user.

We have used the While loop method in the above code to demonstrate the implementation of the continue statement.

We've set up two variables: "n" for entering the value and "i" for the iteration. The program asks the user for a number, which is then stored in n. We initialize i to 0(i=0) in the while loop and add a condition that verifies that "i" is less than or equal to n (i<=n).

We used the continue statement inside the loop to skip number three. If the number is not 3, the statement outside the ‘if condition’ is executed, and the number is printed one by one.

Example 2: Program to print the following numbers:

#include <iostream>
using namespace std;
int main()
{
int j=10;
while (j >=0)
{
if (j==6)
{
     j--;
    continue;
}
 cout<<" the Value of j: "<<j<<endl;
     
    j--;
  
  }
  
return 0;
}


The output of the program:

The value of j: 10
    
The value of j: 9
    
The value of j: 8
   
The value of j: 7 
    
The value of j: 5
   
The value of j: 4
   
The value of j: 3
   
The value of j: 2
  
The value of j: 1

Explanation:

The While loop method was used in the above program to demonstrate the implementation of the continue statement.

The program is designed to print a list of numbers. We have used a variable "j" for the iteration. We initialized j to 10 (j=10) in the while loop and specified a condition that verifies that "j" is more than or equal to n ( j >= n). We used the continue statement inside the loop to skip the number six. If the number is not 6, the statement outside the if condition is executed, and the number is printed one by one.

Continue with the Nested loop:

When the continue keyword is used with nested loops, the current iteration of the inner loop is skipped.

Program:

// using continue statement inside
// nested while loop
#include <iostream>
using namespace std;
int main() 
{
   
int number, i, j;
   
int sum = 0;
 
// nested while loops
 // first loop
   
i=1;
   
while(i<=3) 
   
{
             // second loop
       
j=1;
 
while(j<=3)
       
{
            j++;
            if(j==2)
            {
               continue;
            }
     
  cout << "i = " << i << ", j = " << j << endl;
           }
       
i++;
 
}
 return 0;
}

Output of the program:

i = 1, j = 2
i = 1, j = 4
i = 1, j = 5
i = 2, j = 2
i = 2, j = 4
i = 2, j = 5
i = 3, j = 2
i = 3, j = 4
i = 3, j = 5
i = 4, j = 2
i = 4, j = 4
i = 4, j = 5

Explanation:

When the continue statement is used in the above program, it skips the current iteration of the inner loop. And the program's control shifts to the update expression of inner loop. As a result, the value of j=3 never appears in the output.


Related Topics

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++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

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

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

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

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.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

Print Table Using For-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...

3 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

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.

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.