Example 1:
The “iostream” header file is included in this code. This header file is utilized in C++ as the input or output functions are declared in this header file. After this, we place the “std” namespace and then invoke the “main()” function. We initialize the “a” variable as the integer data type and then place the “for” loop. In this loop, we place “a = 0” and the condition that we add here is “a <= 9”. Then, we increment it in the value of “a”. In the following, we utilize the “if” condition in which we insert “a == 5” and place the “continue” statement after this. The “continue” statements help skip the value of “5” from this loop. Then, we add the “cout” and print the remaining numbers. It renders all the numbers except the “5” since we added the “continue” statement.
Code 1:
using namespace std;
int main() {
int a;
for ( a = 0; a <= 9; a++) {
if (a == 5) {
continue;
}
cout << "The number is " << a << endl;
}
return 0;
}
Output:
Here, all the numbers are rendered, but the number “5” is skipped from this output and not rendered here because we added a “continue” statement to skip the number “5” from this loop.
Example 2:
We utilize the “continue” statement inside the “while” loop. The code includes the “iostream” header file because the input and output methods are declared in this header file. The “std” namespace is then added, and the “main()” function is then called here. After this, we place “int” and initialize an integer variable with the “num” name. After this, we use the “while()” loop and place the “num <= 12” condition.
Below this, we place “if” which contains another condition which is “num == 7”. Then, we place “num++” which will increment by one in the value of “num”. After this, we utilize the “continue” statement. This “continue” statement skips the number “7” from the “while” loop. The “cout” is added and the remaining numbers are printed. Since we inserted the “continue” statement, it renders all the numbers other than “7”. After this, we place “num++” to increment the value of “num”.
Code 2:
using namespace std;
int main() {
int num = 0;
while (num <= 12) {
if (num == 7) {
num++;
continue;
}
cout << "We display number using while loop " << num << endl;
num++;
}
return 0;
}
Output:
Since all the numbers are printed in this instance, the number “7” is omitted from the output and not rendered since a “continue” statement is added to the loop to skip the number “7”.
Example 3:
We add this “continue” statement inside the nested “for” loop. After inserting the header file and the “std” namespace, we invoke the “main()” method. The “n1” and “n2” variables are declared here as the integer type. Now, we use the “for” loop which contains the “n1 <= 4” condition. Below this, we place the “for” loop again which is the “nested for” loop. The second “for” loop contains the condition that says “n2 <= 4”.
Now, we utilize “if” in which we add the condition and use the “&&” operator. The “continue” is then added here. It skips only those values where “n1” is “3” and “n2” is “3”. When both “n1” and “n2” are ”3”, it skips that iteration of the loop here. Then, we use the “cout” below this to render the remaining values.
Code 3:
using namespace std;
int main()
{
int n1, n2;
for( n1=1; n1 <=4; n1++){
for( n2 =1;n2 <=4; n2++){
if(n1==3 && n2==3){
continue;
}
cout<< "n1 = " << n1 <<" n2 = "<< n2 <<endl;
}
}
}
Output:
Since a “continue” line is added to the nested loop to skip the iteration where both variable values are “3” and all other values are printed here.
Example 4:
The “int” variable named “num_1” is initialized with the value of “1”. Then, we use the “while” loop and add the “num_1 <= 9” condition. Below this, the “cout” is included and the “if” condition is placed there. The “if” condition checks the remainder of the number equal to zero when we divide it by “2”. This condition is added when we want to check the even number in our code. Below, we place “num_1++” to increment the value of “num_1”. Here, we utilize the “continue” to skip the even numbers from the loop and not render the given message for the even number that is present in the loop.
Code 4:
using namespace std;
int main() {
int num_1 = 1;
while (num_1 <= 9) {
cout << "The number is = " << num_1 << endl;
if (num_1 % 2 == 0) {
num_1++;
continue;
}
cout << "The number is " << num_1 << " which is an odd number" << endl;
num_1++;
}
return 0;
}
Output:
Here, we can see that the even numbers are omitted from the output and the message for the odd number is rendered here just because of the “continue” statement that we inserted in our code.
Example 5:
In our last code, we get the values from the user that are less than “45” and terminate the user input when we enter the negative value and calculate the sum of the given values. First, we initialize the “summation” and “value” as the integer data types.
Then, check whether the value is positive or not. Then, apply “summation + = value” if the value is positive and render the message to “Enter number for sum”. Below this, we utilize the “cin” to get the number and save it in the “value”. Now, we move ahead and see whether “value > 45”. If this condition is satisfied, we must execute the given line in which we have written a message to enter a value less than “45”. Then, assign “value = 0” and place the continue statement which skips that value and get a new value from the user. After this, we calculate the “summation” and render it. This summation is done when we enter a negative value.
Code 5:
using namespace std;
int main() {
int summation = 0;
int value = 0;
while (value >= 0) {
summation += value;
cout << "Please Enter a number for sum: ";
cin >> value;
if (value > 45) {
cout << "The number you have entered here is greater than 45 so, we won't calculate it'" << endl;
value = 0;
continue;
}
}
cout << "The sum of the entered num is " << summation << endl;
return 0;
}
Output:
This outcome renders how our code gets the user’s input, applies the summation, and displays a message when we enter a value greater than “45”. Here, we might notice that the values that are greater than “45” are skipped and not calculated in the summation process. This is because we applied the “continue” statement at that place in our code.
Conclusion
The “continue” concept in C++ programming is studied thoroughly in this guide. We explored how this “continue” statement aids in skipping the desired value from the output to render. We utilized this “continue” statement in our codes and explained each code as well as the outcomes of these codes. We used this “continue” statement inside the “for”, “while”, and “nested for” loop.