Cpp Continue Statement
## C++ continue Statement
In C++, the `continue` statement is a loop control statement that works somewhat like the `break` statement. However, instead of forcing the loop to terminate entirely, `continue` skips the remaining code inside the current iteration and forces the loop to begin its next iteration.
### How it Behaves Across Different Loops:
* **`for` Loop:** Encountering `continue` causes the program to immediately jump to the update/increment expression (e.g., `i++`), and then evaluate the conditional test.
* **`while` and `do...while` Loops:** Encountering `continue` causes the program control to jump directly to the conditional test to determine if the loop should run again.
---
## Syntax
The syntax for the `continue` statement in C++ is straightforward:
```cpp
continue;
```
### How `continue` Works in Loop Structures
```
+---------------------------------------+
| |
v |
---> (True) --->
| |
| (False) (continue statement hit)
| |
v v
```
---
## Flowchart
```
+---------------+
| Start Loop |
+-------+-------+
|
v
/-----------------\
/ Is Condition \ No
< Satisfied? > ------>
\ /
\-----------------/
| Yes
v
|
v
/-----------------\
/ Is continue \ Yes
< encountered? > --------+
\ / |
| No |
v |
|
|
| |
+<------------------+
|
v
```
---
## Code Examples
### Example 1: Using `continue` in a `do...while` Loop
In this example, we initialize a local variable `a` with a value of 10. We use a `do...while` loop to print the value of `a`. When `a` equals 15, we increment its value and trigger the `continue` statement to skip the print statement for that specific iteration.
```cpp
#include
using namespace std;
int main ()
{
// Local variable declaration
int a = 10;
// do...while loop execution
do
{
if( a == 15)
{
// Skip the rest of this iteration
a = a + 1;
continue;
}
cout << "Value of a: " << a << endl;
a = a + 1;
} while( a < 20 );
return 0;
}
```
#### Output
When the above code is compiled and executed, it produces the following output (notice that `15` is skipped):
```text
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
```
---
### Example 2: Using `continue` in a `for` Loop
The `continue` statement is highly effective in `for` loops for filtering out unwanted iterations. In this example, we print only the even numbers between 1 and 10.
```cpp
#include
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
// If i is odd, skip the print statement and go to the next iteration
if (i % 2 != 0) {
continue;
}
cout << i << " is an even number." << endl;
}
return 0;
}
```
#### Output
```text
2 is an even number.
4 is an even number.
6 is an even number.
8 is an even number.
10 is an even number.
```
---
## Key Considerations and Best Practices
1. **Avoid Infinite Loops in `while` / `do...while`:**
When using `continue` inside a `while` or `do...while` loop, ensure that the loop control variable is updated *before* the `continue` statement is executed (as shown in Example 1). Otherwise, you may create an infinite loop because the update step is skipped.
2. **Readability vs. Nested Conditions:**
The `continue` statement is excellent for implementing the "guard clause" pattern. It helps keep your code clean by avoiding deeply nested `if-else` blocks inside loops.
3. **Scope Limitation:**
The `continue` statement only affects the innermost loop in which it is placed. If you have nested loops, a `continue` statement inside the inner loop will not skip iterations of the outer loop.
YouTip