Back to: C Tutorials For Beginners and Professionals
Continue Statement in C Language with Examples
In this article, I will discuss the Continue Statement in C Language with Examples. Please read our previous articles discussing Break Statements in C Language with Examples. At the end of this article, you will understand what a Continue Statement in C is and when and how to use a Continue Statement in C language with examples.
Continue Statement in C Language:
The continue statement in C language is a control flow statement used within loops (such as for, while, and do-while loops). When a continue statement is encountered in a loop, the current iteration of the loop is skipped, and control jumps to the beginning of the loop for the next iteration, skipping the remaining code in the loop body for the current iteration. It is especially useful when you want to skip over certain steps in a loop under specific conditions but still continue looping.
Syntax: continue;
Flow Chart of Continue Statement in C Language
Note: The continue statement is very similar to the break statement, except that instead of terminating the loop, it will skip the current iteration and continue with the next iteration. That means the continue statement skips the rest of the body of the loop and immediately checks the loop’s condition. if the loop’s condition remains true, the loop’s execution continues.
In Loops (for, while, do-while)
- Initialization: In a loop, initialization occurs first (e.g., setting up a counter variable in a for loop).
- Condition Check: The loop’s condition is evaluated. If the condition is false, the loop terminates. If it’s true, the loop’s body starts executing.
- Execution of Loop Body: The loop executes its body. When it reaches the continue statement, the flow changes.
- Encounter continue: On encountering a continue statement, the control immediately moves to the next iteration of the loop.
- In a for loop, this means jumping to the update expression and then to the condition check.
- In a while or do-while loop, it directly jumps to the condition check.
- Skipping Remaining Code: Any code following the continue statement in the current iteration is skipped.
- Next Iteration: The loop proceeds to the next iteration (if the condition remains true).
Example: Continue in a For Loop
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; // Skip the rest of the loop body for even numbers } printf("%d ", i); // This will print only odd numbers } return 0; }
In this example, the continue statement is used to skip the remaining part of the loop for even numbers. Therefore, only odd numbers between 0 and 9 are printed.
Example: Continue in a While Loop
#include <stdio.h> int main() { int i = 0; while (i < 10) { i++; if (i % 3 == 0) { continue; // Skip the rest of the loop for numbers divisible by 3 } printf("%d ", i); // This will print numbers not divisible by 3 } return 0; }
In this case, the continue statement is used to skip the print statement for numbers divisible by 3.
Example: Continue in a do-while loop:
In this example, the loop will not print the number 5.
#include <stdio.h> int main() { int i = 0; do { if (i == 5) { i++; // Increment i before continue continue; // Skips the rest of the loop body when i is 5 } printf("%d ", i); i++; } while (i < 10); return 0; }
Example: Continue with a Condition
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { continue; // Skip this iteration when i is 5 } printf("%d ", i); } return 0; }
In this example, the number 5 is skipped in the loop. The loop prints numbers from 1 to 10, excluding 5.
In the next article, I will discuss Return Statements in C Language with Examples. In this article, I try to explain the Continue Statement in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.