×

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

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

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

Input Iterators in C++

What are input iterators? Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented. All the iterators...

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

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

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

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.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

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

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.