It has the following syntax:
//the set of loop statements
continue;
// the lines of code which need to be skipped
The continue statement is frequently used in conjunction with conditional statements, like if and if-else, to define the circumstance in which the current loop iteration should be bypassed.
Flowchart Diagram of Continue Statement
The continue statement is employed within loops such as for, while, and do-while to bypass the remaining code within the loop body for the ongoing iteration and proceed to the subsequent iteration.
As illustrated in the flow diagram, we can follow these steps to incorporate the continue keyword in C:
Start by adding the continue statement to the body of various types of loops like do-while, while, and for loops.
Step 2: The condition is assessed to determine if the next iteration should continue after the loop finishes.
If the statement is found to be true, the program will proceed to run the body of code until the condition is no longer true.
If the loop condition evaluates to false, the program proceeds to execute the remaining section of the loop before advancing to any subsequent instructions.
Step 5: In this step, it prints the output.
Working of the Continue Statement in C
In C programming, the continue statements are employed in conjunction with various types of loops like for loops, while loops, do-while loops, and nested loops.
1) Continue Statement with for Loop in C
When a continue statement is employed within a for loop in the C programming language, it skips the remaining statements in the current iteration and proceeds to the update expression before commencing the next iteration.
Syntax:
It has the following syntax:
for (initi; condition; update) { //using for loop
// Code block before continue statement
if (condition_to_skip) {
continue; // Skip to next iteration
}
// Code block after continue statement
}
Continue Statement with For Loop in C Example
Let's consider an example to demonstrate the continue statement with a for loop in the C programming language.
Example
#include <stdio.h>
int main() { //main function
printf("For loop (skip 3): ");
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue; // Skip when i is 3
printf("%d ", i);
}
return 0;
}
Output:
For loop (skip 3): 1 2 4 5
Explanation:
In this instance, a for loop is employed to display numbers ranging from 1 to 5. However, the code omits printing when the specified condition is satisfied (i = 3). Subsequently, the continue statement triggers the loop to immediately proceed to the subsequent iteration, thereby bypassing the printf statement for that particular value.
2) Continue Statement with a while Loop in C
In the C programming language, the continue statement within a while loop bypasses the remaining code within the current iteration and proceeds to evaluate the condition for the subsequent iteration of the while loop.
Syntax:
It has the following syntax:
While (condition) //while loop
{
//Block of the code
If (condition to continue) {
Continue; //Continue Statement
}
//Block of the code
}
Continue Statement with while loop in C
Let's consider an example to demonstrate the implementation of the continue statement within a while loop in the C programming language.
Example
#include <stdio.h>
int main() { //main function
int i = 1;
printf("While loop (skip even numbers): ");
while (i <= 5) {
if (i % 2 == 0) {
i++;
continue; // Skip even numbers
}
printf("%d ", i);
i++;
}
return 0;
}
Output:
While loop (skip even numbers): 1 3 5
Explanation:
In this instance, a while loop is employed to display numbers ranging from 1 to 5, while excluding the even numbers. If the value of i is even, the continue statement is triggered, leading to the subsequent iteration without printing that specific number.
3) Continue Statement with do-while Loop in C
In C programming, the continue statement in conjunction with a do-while loop is employed to bypass the remaining code within the current iteration and proceed to evaluate the condition for determining the execution of the next iteration. The do-while loop ensures that its body is executed at least once before progressing to the subsequent iteration.
Syntax:
It has the following syntax:
do{
//Block of the code
If (condition to continue) {
Continue; //Continue Statement
}
//Block of the code
}
while(condition);
The ```
//the set of loop statements
continue;
// the lines of code which need to be skipped
include <stdio.h>
int main {
int i = 1;
do {
if (i == 3) {
i++;
continue;
}
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}
In this example, the program will output: 1 2 4 5. When the value of `i` is 3, the `continue` statement is encountered, causing the loop to skip the `printf` statement and move to the next iteration.
Let's consider a scenario to demonstrate the continue statement within a do-while loop in the C programming language.
### Example
include <stdio.h>
int main { //main function
int i = 1;
printf("Do-while loop (skip 4): ");
do {
if (i == 4) {
i++;
continue; // Skip when i is 4
}
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}
Output:
Do-while loop (skip 4): 1 2 3 5
Explanation:
In this instance, a do-while loop is applied to display numbers ranging from 1 to 5, excluding the number 4. Once the condition is met (i=4), the continue statement is triggered, bypassing the printf for that particular cycle and proceeding directly to the loop's condition evaluation before progressing to the subsequent iteration.
### 4) Continue Statement with Nested Loop in C
In C programming, when the continue statement is employed within a nested loop, it serves to bypass the remaining statements within the ongoing iteration of the loop and proceed to the subsequent iteration of the same loop. Notably, it does not impact iterations of the outer loop.
Syntax:
It has the following syntax:
for (init; condition, update) //for loop{
for (init; condition, update) //another for loop
{
//Code Block
If (condition to continue) {
Continue; //Continue Statement
}
//Code Block
}
}
Continue Statement using Nested Loop in C Example
Let's consider a scenario to demonstrate the Continue Statement within a Nested Loop in the C programming language.
### Example
include <stdio.h>
int main { //main function
int x, y;
printf("Nested loop:\n");
for (x = 1; x <= 3; x++) {
for (y = 1; y <= 3; y++) {
if (x == 2 && y == 2) {
continue; // Skip this inner loop iteration
}
printf("x = %d, y = %d\n", x, y);
}
}
return 0;
}
Output:
Nested loop:
x = 1, y = 1
x = 1, y = 2
x = 1, y = 3
x = 2, y = 1
x = 2, y = 3
x = 3, y = 1
x = 3, y = 2
x = 3, y = 3
Explanation:
In this instance, we employ a pair of embedded for loops to traverse through x and y. If (x = 2) and (y = 2), the continue statement bypasses the rest of the inner loop's content for that specific iteration, proceeding to the subsequent y value. Meanwhile, the outer loop persists without any interruption.
## Usage of the Continue Statement in C
There are several uses of the continue statement in C. Some of the main uses are as follows:
- Remove the Current Iteration: The continue statement is used to stop executing the remaining code and proceed to the next iteration of the specified loop.
- Control the Execution of a Loop: It can stop an iteration in a loop from executing, even within a nested loop.
- Save Resources: Better performance can be provided through the use of structured control by preventing certain code from being executed within specific iterations.
- Most flocking patterns: It is commonly used to eliminate unusual data sequences when iterating.
- Suitable for Every Type of Loop: Its interoperability with for, while, and do-while loops add to its versatility.
- Enhancing Program Logic: It helps to maintain appropriate and intended loop behavior.
## Advantages of the Continue Statement in C
There are several advantages of the continue statement in C. Some of them are as follows:
- It allows us to skip certain iterations of a loop so we do not break the loop entirely and makes the loop logic cleaner because we can eliminate nesting handfuls of if-else conditions.
- There will be faster execution of some iterations of the loop since the useless code won't be executed.
- It is usable in for, while, and do-while loops.
- It makes is easy to throw bad value exceptions and remove bad values from inside loops.
## Disadvantages of the Continue Statement in C
There are several disadvantages of the continue statement in C. Some of them are as follows:
- Much use of the continue statement makes the code hard to read.
- If not well updated, the loop variables will keep looping forever.
- If iteration is skipped without checking what needs to be checked, unexpected behavior can take place.
- Overuse leads to unclear flow
- Sometimes, with better structuring of loops, the same effect can be achieved.
## Conclusion
In summary, the continue statement in C is compatible with for, while, and do-while loops. This statement allows for the skipping of particular iterations depending on specified conditions. In contrast to the break statement, which ends the loop entirely, the continue statement solely bypasses the current iteration and proceeds to the next one. Employing the continue statement is beneficial for selectively filtering out or bypassing specific values during the iteration process, thereby enhancing the control and efficiency of loops.