How to use continue statement in C / C++
In this tutorial, we will learn about the syntax and how to use continue
statement in C / C++. The continue
statement is used in loops such as: for
, while
, do… while
.
Before learning about the continue
statement, you need to learn the usage of:
- How to use for loop in C / C++.
- How to use while loop in C / C++.
- How to use do...while loop in C / C++.
- How to use if - else statement in C / C++.
Syntax and its working of continue statement in C / C++
In the C and C++ programming languages, the continue
statement is used to skip the current iteration of the loop and the control of the program goes to the next iteration.
Syntax of continue
statement in C / C++:
continue;
When the continue
statement is encountered, the loop immediately jumps to the next iteration. Regardless of whether there is another command behind it or not.
Continue statement in C / C++ with for loop
In the for
loop, we can use continue
statement to skip the current iteration. See the example below:
Example: Display numbers from 1 to 5, ignore number 3.
C++ programming:
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { // break condition if (i == 3) { continue; } cout << i << endl; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { for (int i = 1; i <= 5; i++) { // continue condition if (i == 3) { continue; } printf("%d\n",i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
In this program, we use for loop to display the numbers from 1 to 5. However inside the loop body, we have a continue
statement on condition i = 3
. So when the program loops to i = 3
, will skip and go to i = 4
.
Continue statement in C / C++ with while loop
The continue
statement is also used in the while
loop to skip the current iteration and move on to the next iteration. We have the following example:
Example: Continue
statement in C / C++ with while
loop.
- Program to find the sum of positive numbers.
- If the user enters a negative numbers, break ends the loop.
- The negative number entered is not added to sum.
- If number above 50, skip iteration.
C++ programming:
#include <iostream> using namespace std; int main() { int sum = 0; int number = 0; while (number >= 0) { // add all positive numbers sum += number; // take input from the user cout << "Enter a number: "; cin >> number; // continue condition if (number > 50) { cout << "The number is greater than 50 and won't be calculated." << endl; number = 0; // the value of number is made 0 again continue; } } // display the sum cout << "The sum is " << sum << endl; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int sum = 0; int number = 0; while (number >= 0) { // add all positive numbers sum += number; // take input from the user printf("Enter a number: "); scanf("%d", &number); // continue condition if (number > 50) { printf("The number is greater than 50 and won't be calculated.\n"); number = 0; // the value of number is made 0 again continue; } } // display the sum printf("The sum is %d\n",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
The above program will calculate the sum of the numbers entered by the user. However, there are some conditions as follows:
- Input numbers are less than 50.
- Input number is positive.
Continue statement in C / C++ with nested loop
In the case of two nested loops, the continue
statement skips only the iteration of the loop closest to it. Outer loop are not affected.
Example: Continue
statement in C / C++ with nested loop.
C++ programming:
#include <iostream> using namespace std; int main() { int number; int sum = 0; // nested for loops // first loop for (int i = 1; i <= 3; i++) { // second loop for (int j = 1; j <= 3; j++) { if (j == 2) { continue; } cout << "i = " << i << ", j = " << j << endl; } } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int number; int sum = 0; // nested for loops // first loop for (int i = 1; i <= 3; i++) { // second loop for (int j = 1; j <= 3; j++) { if (j == 2) { continue; } printf("i = %d, j = %d\n",i,j); } } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output: