×

C++ Continue in Do-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:                                      

Continue in Do-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.

What is Do-While Loop?

An iterative loop that checks the condition at the end.
The Do-While loop can be used whenever a test condition is specific, as the control enters the loop at a minimum once before the condition is evaluated. This loop checks the given condition after the execution of looping statements.

The loop actions or statements will be repeated infinitely as long as the test requirements are met.

Note: DO-WHILE LOOP means “first do it, then check”.

Syntax:

The Syntax of the DO-WHILE loop is:

Do
           { 
            statement(s);
           } 
         while(expression);

Continue in Do-While loop

The continue statement breaks one iteration (in the loop) if a specified condition occurs, and continues with the next iteration in the loop.

Syntax:

do ( ){         
   {                                           
     // ...            
  continue;
updation;
   }
 } while( ,) 

Examples of Continue in Do-While loop

Algorithm to print the list of numbers:

Step 1: Start the program.

Step 2: Read n and i.

Step 3: Initialise i=1.

Step 4: Print the value of I,

If (i==3)

Continue;

Else

go to step 5.

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

Step 6: Stop.

Program:

#include<iostream>
using namespace std;
int main()
{
int i, n;
cout << "Enter a number";
cin >> n;
cout << "the list of numbers are";


do(i=1)
{
   i++;
   if(i == 5)
    {
      continue;
   }
 cout << i;
  }while(i<=n)
return 0;
}

The output of the program:

Enter a number: 10
The numbers are:
1
2
3
4
6
7
8
9
10

Explanation:

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

We have used the do 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 variable ‘n’. We initialized i to 0(i=0). And, we used the continue statement inside the loop to skip number five. If the number is not 5, the statement outside the ‘if condition’ is executed, and the number is printed one by one.

After that, we use a condition as text expression that verifies the value of "i" is less than or equal to n (i<=n).

Algorithm to print Odd numbers using continue statement:

Step 1: Start the program.

Step 2: Read n and i.

Step 3: Initialise i=1.

Step 4: Iterate until i<=10 if the condition is false then go to step 6.

Step 5: Print the value of I.

If (i%2==0)

Continue;

Else

go to step 4.

Step 6: exit

Program:

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

Output:

The output of the program
Enter a number: 10
Odd numbers:
1
3
5
7
9

Explanation:

We have used the do 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 variable ‘n’. We initialized i to 1(i=1).

After then, we used the continue statement inside the loop to skip the even numbers with the logic (i%2==0). if the condition is true according to the logic then the number is skipped. And, if the number is not even, the statement outside if condition is executed, and the odd numbers are printed one by one.

After that, we use a condition as text expression that verifies the value of "i" is less than or equal to n (i<=n).

Continue with the Nested loop:

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

Program:

#include <iostream>
using namespace std;
int main()
 {
    int n;
    int sum=0;
do(i=1)
  {
      I++;
     do(i=1)
       { 
             j++;
            if (j ==3) 
             {
                continue;
            }
            cout << "i = " << i << ", j = " << j << endl;
        } while(j<=5)
 }while(i<=5)
return 0;
}

Output:

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

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.

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.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

5 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

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.

How to run program in turbo c++

What is Turbo C++? Turbo c++ is an integrated development environment (IDE) and a compiler to run C++ code. Turbo c++ helps to link the header files with the main code....

2 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

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

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.