C++ continue
statement
The continue
statement in C++ is used to skip the current iteration of a loop and continue with the next iteration. Unlike the break
statement, which terminates the loop entirely, the continue
statement only skips the remaining code in the current iteration and proceeds to the next iteration of the loop.
In this blog post, we'll cover:
continue
statement.for
, while
, do...while
).continue
statement.continue
StatementThe syntax of the continue
statement is straightforward:
continue;
continue
Statement in for
LoopsThe continue
statement in a for
loop skips the remaining statements inside the loop and jumps to the next iteration. This is useful when you want to skip over certain iterations based on a condition but keep the loop running.
for
Loop
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
cout << "Odd number: " << i << endl;
}
return 0;
}
Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
Explanation:
continue
statement is executed when the value of i
is even (i % 2 == 0
).cout
statement for even numbers and only prints odd numbers.continue
Statement in while
LoopsIn a while
loop, the continue
statement works similarly by skipping the rest of the code inside the loop's body and jumping back to the condition check for the next iteration.
while
Loop
#include <iostream>
using namespace std;
int main() {
int i = -5;
while (i <= 5) {
i++; // Increment i
if (i < 0) {
continue; // Skip negative numbers
}
cout << "Positive number: " << i << endl;
}
return 0;
}
Output:
Positive number: 0
Positive number: 1
Positive number: 2
Positive number: 3
Positive number: 4
Positive number: 5
Explanation:
continue
statement is executed when i
is less than 0, causing the loop to skip negative numbers and only print positive numbers.i++
ensures that i
is incremented with each iteration, but negative numbers are skipped when they occur.continue
Statement in do...while
LoopsIn a do...while
loop, the continue
statement is used to skip the current iteration and return to the loop's condition check. Since the do...while
loop always executes the block of code at least once, the continue
statement ensures the loop moves to the next iteration without executing the remaining statements.
do...while
Loop
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
if (i % 2 == 0) {
i++;
continue; // Skip even numbers
}
cout << "Odd number: " << i << endl;
i++;
} while (i <= 10);
return 0;
}
Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
Explanation:
do...while
structure.continue
statement skips the even numbers, printing only the odd ones.continue
Statementcontinue
to optimize the flow.You can use the continue
statement to skip invalid user input and continue asking the user for valid input.
#include <iostream>
using namespace std;
int main() {
int number;
// Ask the user for numbers until a valid one is entered
while (true) {
cout << "Enter a positive number: ";
cin >> number;
if (number <= 0) {
cout << "Invalid input! Please enter a positive number." << endl;
continue; // Skip invalid input
}
cout << "You entered: " << number << endl;
break; // Exit the loop when a valid number is entered
}
return 0;
}
Output:
Enter a positive number: -3
Invalid input! Please enter a positive number.
Enter a positive number: 5
You entered: 5
Explanation:
continue
statement is executed, skipping the cout
for invalid input and prompting the user again until a valid number is entered.continue
and break
StatementsFeature | continue |
break |
---|---|---|
Purpose | Skips the current iteration and moves to the next one. | Exits the loop or switch statement entirely. |
Loop Termination | Does not terminate the loop; only skips the iteration. | Terminates the loop or switch immediately. |
Use Case | Skipping unwanted or invalid iterations. | Breaking out of a loop or switch prematurely. |