- Within loops is used the Continue expression.
- Whenever there is a continuous statement inside a loop, the control jumps directly to the start of the loop for next iteration, ignoring the execution of statements inside the body of the loop for the current iteration.
- Sometimes a certain check condition within a loop should be skipped. Start in such cases; statements are used in C++ programming.
Syntax of continue
continue;
- In practice, continue; statement is almost always used inside a conditional statement.
Continue in C++. |
C++ continue example
C++ program to display integer from 1 to 10 except 6 and 9.
#include <iostream.h>
int main()
{
for (int i = 1; i <= 10; ++i)
{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}
return 0;
}
Expected Output:-
1 2 3 4 5 7 8 10
In above program, when i is 6 or 9, execution of statement cout << i << "\t"; is skipped inside the loop using continue; statement.
Another Example
#include <iostream.h>
int main(){
for (int num=0; num<=6; num++) {
if (num==3) {
continue;
}
cout<<num<<" ";
}
return 0;
}
Expected Output
0 1 2 4 5 6
Another Example
#include <iostream.h>
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
if( a == 15) {
// skip the iteration.
a = a + 1;
continue;
}
cout << "value of a: " << a << endl;
a = a + 1;
}
while( a < 20 );
return 0;
}
Expected Output:-
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Another Example:-
#include <iostream.h>
int main() {
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
cout << i << "\n";
i++;
}
return 0;
}
Expected Output:-
0
1
2
3
5
6
7
8
9