In C++ programming, the continue statement serves as a control statement that is used to bypass the current iteration within a loop and direct the control back to the beginning of the iteration. This action compels the program control to proceed with the next iteration of the loop.
The continue statement is employed within the program loop. Essentially, when encountered, the code block within the loop following the continue statement will be bypassed, and the loop will proceed to the next iteration.
Syntax
It has the following syntax:
//loop statements
continue; //continue statement
//Code lines, which are to be skipped
Flowchart Diagram of Continue Statement
As illustrated in the flow diagram, adhere to the steps below to incorporate the continue keyword in C++:
Begin by navigating to the loop body of the program.
In the subsequent step, it evaluates the loop condition to determine whether to proceed with the next iteration.
If the condition evaluates to true, the loop will proceed to run the body repeatedly until the condition becomes false.
If the condition evaluates to false, the loop proceeds to execute the rest of the body.
Step 5: Finally, it prints the output.
Working of Continue Statement
In C++ programming, the continue statement is compatible with various types of loops, such as the for loop, while loop, do-while loop, and nested loop structures.
1. Continue Statement with for Loop in C++
In C++, when a continue statement is used within a for loop, it skips the current iteration, and the program flow transitions to the subsequent iteration within the loop body.
Syntax:
It has the following syntax:
for (init; condition, update) //for loop
{
//Code Block
If (condition to continue) {
Continue; //Continue Statement
}
//Code Block
}
Example:
Consider an example to demonstrate the usage of the continue statement within a for loop in the C++ programming language.
#include <iostream>
using namespace std;
int main()
{
for(int i=1; i<=8; i++) //for loop body
{
// Condition to continue statement
if(i==6){
continue; //Continue Statement
}
cout<<i<<"\n"; //it print the value of i
}
}
Output:
1
2
3
4
7
8
Explanation:
In this instance, we've opted for the "for" loop to display the "i" value in each cycle. In this scenario, we employ the (i==6) condition to execute the continue statement within the loop. Upon reaching the value of 6, the continue statement bypasses the ongoing iteration and proceeds to the subsequent one.
After that, the variable i is updated to 7, and the condition is evaluated once more. Ultimately, when the loop continues to output the value of i until it reaches 8, the condition turns to false. Subsequently, the loops conclude and display the final value of i in the compiler.
2. Continue Statement in a While Loop in C++
When a continue statement is employed within a while loop in C++ programming, it skips the ongoing iteration, allowing the control flow to proceed to the subsequent iteration of the while loop.
Syntax:
It has the following syntax:
While (condition) //while loop
{
//Block of the code
If (condition to continue) {
Continue; //Continue Statement
}
//Block of the code
}
Example:
Let's consider an illustration to demonstrate the continue statement using a while loop to identify the even numbers in C++.
#include<iostream>
using namespace std;
int main() {
int a = 2, x = 8;
while (a <= x)
{
cout << "Current value of a = " << a << endl; //print the value of a
if (a % 2 == 1) {
// skipping the odd number.
a++;
continue; //continue statement
}
cout << "a = " << a << " is an even number" << endl;
a++;
}
return 0;
}
Output:
Current value of a = 2
a = 2 is an even number
Current value of a = 3
Current value of a = 4
a = 4 is an even number
Current value of a = 5
Current value of a = 6
a = 6 is an even number
Current value of a = 7
Current value of a = 8
a = 8 is an even number
Explanation:
In this instance, a while loop is employed to compute the sum of numbers ranging from 2 to 8. Whenever the variable a is an odd number, the continue statement skips to the subsequent iteration of the loop.
3. Continue Statement in a Do-While Loop in C++
In C++ programming, if a continue statement is encountered within a do-while loop, the program's flow moves immediately to the next iteration, bypassing the remaining code within the loop body.
Syntax:
It has the following syntax:
do{
//Block of the code
If (condition to continue) {
Continue; //Continue Statement
}
//Block of the code
}
while(condition);
Example:
Let's consider an example to demonstrate the continue statement within a do-while loop in C++.
#include <iostream>
using namespace std;
int main() {
int x;
long long fact = 1;
do {
cout << "Enter a positive number to find factorial: ";
cin >> x;
// If the number is negative, try again
if (x < 0) {
cout << "Factorial is not defined for negative numbers! Please enter a positive integer.\n";
continue; // Skip all the remaining loop body
}
int a = 1;
do {
fact *= a;
a++;
} while (a <= x);
cout << "Factorial of " << x << " is: " << fact << endl;
} while (x < 0); // it repeats the above function if the user enters a negative number
return 0;
}
Output:
Enter a positive number to find factorial: 6
Factorial of 6 is: 720
Explanation:
In this instance, the software initially prompts the users to input a number that is greater than zero. Subsequently, if a negative number is entered, the continue statement is triggered, leading to the exclusion of all subsequent iterations in the loop. Conversely, upon entering a valid or positive number, a do-while loop is initiated to compute the factorial of the input number. Finally, the program displays the factorial of the specified number.
4. Continue Statement with the Nested Loop
When the continue statement is employed within nested loops in C++ programming, it shifts to the ongoing iteration of the loop and proceeds to the subsequent iteration.
Syntax:
It has the following syntax:
for (init; condition, update) //for loop{
for (init; condition, update) //another for loop
{
//Code Block
If (condition to continue) {
Continue; //Continue Statement
}
//Code Block
}
}
Example:
Let's consider an example to demonstrate the use of the continue statement within a nested loop in the C++ programming language.
#include <iostream>
using namespace std;
int main()
{
//for loop to print the value of i
for(int i=1;i<=3;i++){
//another for loop to print the value of j
for(int j=1;j<=3;j++){
if(i==2&&j==2){
continue;
}
cout<<i<<" "<<j<<"\n";
}
}
}
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Use Cases of Continue Statement in C++
Several use cases of the continue statement in C++ are as follows:
- Performance Optimization: This continue statement helps to optimize the overall performance by avoiding unnecessary calculations of processing.
- Filtering Data: It is very useful for filtering data by skipping invalid data in a dataset.
- Advantageous for Complex Loop Structure: It is very useful for several cases, such as a complicated loop structure that needs selective execution of iterations.
Frequently Asked Questions
1. What are the main differences between the Continue statement and the break statement in C++?
In C++, both the continue and break statements are employed within loops. While these loops may appear similar at first glance, there are several key differences between them. Some of the main distinctions include:
The continue statement allows skipping the current iteration in a loop without ending the loop.
It is specifically designed to be used within loop structures.
Break Statement:
In the C++ programming language, the break statement is used to end the execution of a loop prematurely.
It is also commonly employed with both the switch statement and various types of loops to control program flow.
2. Can the continue statements be utilized in if-else statements in C++?
No, the continue keyword is restricted to usage within specific loops like for, while, and do-while loops. Implementing the continue keyword within an if-else block will result in a compilation error.
3. Can the continue statement be utilized to skip multiple iterations in C++?
No, the continue statement is specifically designed to bypass the current iteration within a loop. To incorporate multiple iterations within a loop in C++, we can make use of the conditional if statement.
4. How does the continue statement work in C++?
When the continue statement is reached within a loop, the program flow transitions to the following iteration of the loop, bypassing any further code within the current iteration.