Continue Statement in C++
The continue statement is a loop control statement that is used to control the loop. A continue statement is used to proceed with the actions that are performed at that particular interval of time. That is, to continue the execution of the next iteration. It allows bypassing a portion of the loop's body under a certain condition without prematurely exiting the loop altogether. And the primary use case of the continue statement is to optimize as well as “streamline” loops by skipping unnecessary computations or actions for certain iterations, thus improving the overall performance and efficiency of the program respectively.
The continue statement in C++ is a powerful tool for loop control that allows programmers to skip the remainder of the current iteration and move on to the next iteration. We must take care while making use of this statement as excessive use of the continue statements can lead to “complex” and “less readable code”, making it harder to understand the control flow logic. A continue statement can optimize the code by avoiding unnecessary computations or actions. Sometimes continue statements can make the code complex and don't help in “code optimization”, so it is not suggested always to use a continue statement.
The continue statement is used in the case of many loops, like while, for and do while. This statement is written just after the loop logic statement. After the logic is written in a loop, if we wish to proceed with the further execution, we will make use of the continue statement.
Let us see the syntax with an example:
Syntax:
Loop body
{
continue;
}
Usage of Continue statement in for loop:
In for loop, continue statement is used to continue further with the execution, which means the next step of the loop that usually happens only when a loop is directly written, and we may not execute the steps further after the “loop logic”. It is only possible by using the Continue statement itself.
Syntax:
for(loop logic)
{
Statement1;
Statement2;
Continue;
}
Let us see an example illustrating it:
Code:
#include <iostream>
int main() {
for (int i = 1; i <= 10; ++i) {
if (i == 6) {
continue; // Skip iteration when i is 3
}
std::cout << "printing value is: " << i << std::endl;
}
return 0;
}
Output:
![Continue Statement in C++](https://static.tutorialandexample.com/cpp/continue-statement-in-cpp1.png)
The above is an example illustrating the usage of the continue statement in for loop. In the above example, we will be able to print all values except 6.
Usage of continue statement in while loop:
When continue statement is placed within a while loop's body, the continue statement enables the programmer to bypass the remaining code within the current iteration. This can be beneficial when specific conditions are met and we wish to skip certain processing steps for that iteration as well. In scenarios where a while loop is iterating through a collection of the data, and we encounter an element that doesn't meet a certain criterion, then the continue statement can be employed to skip further processing. Making use of the continue statement strategically, programmers can optimize their code by just reducing out the unnecessary computations and focusing processing efforts on elements.
Syntax:
While (loop logic)
{
Statement1;
Statement2;
Continue;
}
Let us see an example discussing the usage of the Continue statement in while loop:
Code:
#include <iostream>
int main() {
int num = 3;
while (num <= 10) {
if (num == 6) {
++num;
continue; // Skip processing for num = 3
}
std::cout << "Required value: " << num << std::endl;
++num;
}
return 0;
}
Output:
![Continue Statement in C++](https://static.tutorialandexample.com/cpp/continue-statement-in-cpp2.png)
The above code discusses how one can make use of the “continue statement” in while loop and under what situation the continue statement is used in C++ programming language.
Advantages of Continue Statement in C++ programming language
Therefore, using “continue statement” may also decrease the execution time as it skips the unnecessary steps.
The “continue statement” is incredibly useful when we have certain conditions under which we don't want certain processing steps to be get executed.
When certain conditions are met then further processing is not needed for a particular iteration, we can make use of the “continue statement” to avoid executing that iteration's remaining code, which means that it improves the code optimization in a few cases as well.
The “continue statement” promotes a consistent loop structure by allowing us to separate special cases or an exceptional conditions.
We can write cleaner and more concise code by making use of the “continue statements” instead of nesting conditional statements within our loop in order to achieve the same effect.