The continue statement is used to skip the remaining statements
of the current iteration of a loop and move to the next iteration.
Unlike break, the continue statement does
not terminate the loop.
continue when you want to skip
a particular iteration but continue executing the loop.
The continue statement skips the remaining part of the
current loop iteration.
continue;
After continue executes, the loop proceeds according to its normal iteration rules.
continue;
The continue statement consists of the keyword continue
followed by a semicolon.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
std::cout << i << std::endl;
}
Output:
1
2
4
5
When i is 3, the current iteration is skipped.
Suppose we have this loop:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
std::cout << i;
}
The execution is:
i == 3, continue executes.for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
std::cout << i << " ";
}
Output:
1 2 3 4 6 7 8 9 10
Only 5 is skipped.
With a while loop, make sure the loop variable is updated before continue, otherwise the loop can become infinite.
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
std::cout << i << " ";
}
Output:
1 2 4 5
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
std::cout << i << " ";
} while (i < 5);
Output:
1 2 4 5
The value 3 is skipped, but the loop continues.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
std::cout << i << " ";
}
Output:
1 3 5 7 9
The continue statement skips all even numbers.
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue;
}
std::cout << i << " ";
}
Output:
2 4 6 8 10
Here, odd numbers are skipped.
for (int i = 1; i <= 20; i++) {
if (i % 5 == 0) {
continue;
}
std::cout << i << " ";
}
The numbers 5, 10, 15 and 20 are skipped.
The continue statement is often used inside an if statement.
for (int i = 1; i <= 10; i++) {
if (i < 5) {
continue;
}
std::cout << i << " ";
}
Only numbers from 5 to 10 are displayed.
int number;
for (int i = 1; i <= 5; i++) {
std::cout << "Enter number: ";
std::cin >> number;
if (number < 0) {
continue;
}
std::cout << "Number = "
<< number
<< std::endl;
}
Negative numbers are skipped.
int numbers[] = {10, -5, 20, -8, 30};
for (int i = 0; i < 5; i++) {
if (numbers[i] < 0) {
continue;
}
std::cout << numbers[i] << " ";
}
Output:
10 20 30
Negative values are ignored.
#include <iostream>
#include <string>
int main() {
std::string text = "HELLO";
for (int i = 0; i < text.length(); i++) {
if (text[i] == 'L') {
continue;
}
std::cout << text[i];
}
return 0;
}
Output:
HEO
Both occurrences of L are skipped.
int skipNumber = 7;
for (int i = 1; i <= 10; i++) {
if (i == skipNumber) {
continue;
}
std::cout << i << " ";
}
The number stored in skipNumber is skipped.
int number = 5;
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
std::cout << number
<< " x "
<< i
<< " = "
<< number * i
<< std::endl;
}
The fifth multiplication is skipped.
When continue is used inside nested loops, it affects the nearest enclosing loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
continue;
}
std::cout << j << " ";
}
std::cout << std::endl;
}
The inner loop skips 3 during every outer iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
std::cout << "Number: "
<< i
<< std::endl;
}
std::cout << "Loop finished";
The loop continues after skipping 3 and eventually finishes normally.
| continue | break |
|---|---|
| Skips the current iteration. | Terminates the loop. |
| Loop continues. | Loop stops. |
| Used when an iteration should be ignored. | Used when no more iterations are required. |
// continue
if (i == 5) {
continue;
}
// break
if (i == 5) {
break;
}
Sometimes the same result can be achieved using if-else. However, continue can make loop logic easier to read when unwanted cases need to be skipped.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
std::cout << i << " ";
}
The code focuses directly on the values that should be processed.
int marks[] = {75, -1, 82, 90, -5};
for (int i = 0; i < 5; i++) {
if (marks[i] < 0) {
continue;
}
std::cout << "Marks: "
<< marks[i]
<< std::endl;
}
Invalid negative values are skipped while valid marks are processed.
int numbers[] = {-10, 20, -30, 40, 50};
for (int i = 0; i < 5; i++) {
if (numbers[i] <= 0) {
continue;
}
std::cout << numbers[i] << " ";
}
Output:
20 40 50
Only positive numbers are processed.
char choice;
for (int i = 1; i <= 5; i++) {
std::cout << "Enter y or n: ";
std::cin >> choice;
if (choice == 'n') {
continue;
}
std::cout << "Processing..."
<< std::endl;
}
When the user enters n, processing for that iteration is
skipped.
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
std::cout << i << " ";
} while (i < 5);
For a do-while loop, after continue is executed, control moves toward the loop's condition check.
Output:
1 2 4 5
Be careful when using continue inside while and do-while loops. The loop variable must still be updated.
Incorrect example:
int i = 1;
while (i <= 5) {
if (i == 3) {
continue;
}
i++;
}
When i becomes 3, continue executes before
i++, so the loop can remain stuck at 3.
Better approach:
int i = 1;
while (i <= 5) {
i++;
if (i == 3) {
continue;
}
std::cout << i;
}
#include <iostream>
int main() {
int numbers[] = {10, -5, 20, 0, 30, -8};
for (int i = 0; i < 6; i++) {
if (numbers[i] <= 0) {
continue;
}
std::cout << "Valid number: "
<< numbers[i]
<< std::endl;
}
return 0;
}
The program ignores zero and negative values and processes only positive numbers.
#include <iostream>
int main() {
int marks[] = {85, -1, 72, 91, -5, 68};
for (int i = 0; i < 6; i++) {
if (marks[i] < 0) {
continue;
}
std::cout << "Student Marks: "
<< marks[i]
<< std::endl;
}
return 0;
}
Invalid marks are skipped, while valid marks are displayed.
| Concept | Meaning |
|---|---|
continue; |
Skips the remaining statements of the current iteration. |
| for Loop | Moves to the next iteration of the for loop. |
| while Loop | Moves toward the next condition check. |
| do-while Loop | Moves toward the condition check at the end of the loop. |
| Nested Loop | continue affects the nearest enclosing loop. |
| break | Terminates the loop completely. |
if (condition) {
continue;
}
Question: What does the continue statement do in a loop?