×

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 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 for 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 For Loop?

A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently.

The syntax that can be used for the for loop in the C++ programming language is given below:

for (initialization statement; termination condition; increment or decrement statement (used for modifying value for further evaluation))
{
    /* main body of the “for” loop */
}

Continue in For Loop

The continue keyword in the for loop moves the control to the update statement of the loop in which it is used.

  for ( , , ) {         
   {                                           
     // ...            
  continue;
   }
 } 

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";
for (i=1; i<=n; i++)
{
   if(i == 5)
    {
      continue;
   }
 cout << i;
  }
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 for 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) and used a condition as text expression that verifies the value of "i" is less than or equal to n (i<=n). In the loop, we used the continue statement in the if condition 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.

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";
 for (i=1; i<=n; i++)
{
   if(i%2==0)
    {
      continue;
   }
 cout<<i;
    }
return 0;
}

Output:

Enter a number: 10
Odd numbers:
1
3
5
7
9

Explanation

We have used the for 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) and used a condition as text expression that verifies the value of "i" is less than or equal to n (i<=n).

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.

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 for loop
#include <iostream>
using namespace std;
int main()
 {
    int n;
    int sum=0;
// nested for loops
            // first loop
  for (int i = 1; i <= 5; i++) 
{
        // second loop
     for (int j = 1; j <= 5; j++)
 {
            if (j ==3) 
             {
                continue;
            }
            cout << "i = " << i << ", j = " << j << endl;
  }
 }
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

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

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

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

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

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

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

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

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.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.