×

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

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

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

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.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

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.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

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