×

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

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

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

4 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

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

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

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.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

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