The continue statement in C++ is used to skip some statements inside the body of the loop and forces the next iteration of the loop. The continue statement skips the rest of the statements of loop body in the current iteration.
Most of the time, continue statement is used with conditional statement inside body of the loop. Use of continue statement, change the normal sequence of execution of statements inside loop.
- Inside while and do..while loop, continue statement will take control to the condition statement.
- Inside for loop, continue statement will take control to the update statement(increment/decrement of loop control variable) then condition will be checked.
Syntax of continue Statement
for (int i = 0; i < 5; ++i) { // Code before the continue statement if (someCondition) { // Skip rest of the loop and move to next iteration continue; } // Code after the continue statement }In this example, if someCondition is true, the continue statement will skip the remaining code in the current iteration and move on to the next iteration of the loop.
Flow Diagram of continue Statement
- We can use continue statement inside any loop(for, while and do-while). It skips the remaining statements of loop's body and starts next iteration.
- If we are using continue statement inside nested loop, then it will only skip statements of inner loop from where continue is executed.
C++ continue Statement Example Program
#include <iostream> using namespace std; int main(){ int N, counter, sum=0; cout << "Enter a positive number\n"; cin >> N; for(counter=1; counter <= N; counter++){ //Using continue statement to skip all odd numbers if(counter%2 == 1){ continue; } sum+= counter; } cout <<"Sum of all even numbers from 1 to " <<N<<" = "<< sum; return 0; }Output
Enter a positive number 6 Sum of all even numbers from 1 to 6 = 12In above program, we first take an integer N as input from user. We want to find the sum of all even numbers between 1 to N. If counter is odd number then continue statement will force next iteration without executing sum statement. Here, continue statement is used to skip all odd numbers while finding even numbers sum.
Labeled continue for Nested Loops
- Skipping Inner Loop Iterations : Consider a scenario with nested loops, and you want to skip iterations of the inner loop based on a certain condition. The labeled continue statement allows you to achieve this.
#include <iostream> int main() { for (int i = 0; i < 3; ++i) { std::cout << "Outer loop iteration " << i << ": "; for (int j = 0; j < 3; ++j) { if (j % 2 == 0) { // Skip iterations for even values of j in the inner loop continue; } std::cout << j << " "; } std::cout << "\n"; } return 0; }
In this example, the labeled continue statement skips iterations for even values of j in the inner loop, allowing you to control the flow of both loops independently. - Using Labeled continue with While Loop : Labeled continue statements are not limited to for loops; they can also be used with while loops.
#include <iostream> int main() { int i = 0; outerLoop: // Label for the outer loop while (i < 3) { std::cout << "Outer loop iteration " << i << ": "; int j = 0; while (j < 3) { if (j % 2 == 0) { // Skip iterations for even values of j in the inner loop ++j; continue outerLoop; // Labeled continue } std::cout << j << " "; ++j; } std::cout << "\n"; ++i; } return 0; }
Here, the labeled continue statement is used with a while loop for the inner loop, showcasing its flexibility.
Using continue with While and Do-While Loops
- Using continue with a While Loop : Consider a scenario where you want to find the first occurrence of a certain value in an array using a while loop. The continue statement can be employed to skip unnecessary iterations once the value is found.
#include <iostream> int main() { int array[] = {3, 7, 2, 8, 5, 9}; int target = 8; int i = 0; while (i < 6) { if (array[i] != target) { // Skip iterations until the target value is found ++i; continue; } std::cout << "Target value found at index " << i << "\n"; break; // Break out of the loop once the target is found } return 0; }
Here, the continue statement allows us to skip iterations until the target value is found, enhancing the efficiency of the search. - Using continue with a Do-While Loop : In a similar vein, the continue statement can be applied within a do-while loop to skip iterations based on specific conditions.
#include <iostream> int main() { int i = 0; do { if (i % 2 == 0) { // Skip iterations for even numbers ++i; continue; } std::cout << i << " "; ++i; } while (i < 10); return 0; }
In this example, the continue statement skips iterations for even numbers in a do-while loop.
Common Use Cases for continue statement
- Skipping Specific Iterations : One of the primary use cases for the continue statement is to skip certain iterations based on a specific condition. For instance, in a loop processing elements, you might want to skip elements that meet certain criteria.
#include <iostream> int main() { int numbers[] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; ++i) { if (numbers[i] % 2 == 0) { // Skip even numbers continue; } std::cout << numbers[i] << " "; } return 0; }
Here, the continue statement skips the iteration when encountering an even number. - Avoiding Nested Loop Execution : In scenarios involving nested loops, the continue statement can be instrumental in controlling the execution flow. Consider a case where you want to skip the inner loop for specific conditions:
#include <iostream> int main() { for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 3; ++j) { if (i * j == 4) { // Skip the inner loop when i * j equals 4 continue; } std::cout << i * j << " "; } } return 0; }
In this example, the continue statement ensures that the inner loop is skipped when i * j equals 4.
Conclusion
In the vast landscape of C++ programming, the continue statement stands as a versatile tool, offering a means to skip iterations and shape the flow of loops according to specific conditions. By understanding its syntax, exploring practical examples, and adhering to best practices, you can wield the continue statement with finesse, creating code that is not only efficient but also maintainable.