What is the continue Statement?
The continue statement in C is a jump statement that skips the current iteration of a loop and jumps to the next iteration. It works inside for, while, and do-while loops.
How It Works
| Loop Type | Where Continue Jumps |
|---|---|
for | Increment/update expression |
while | Loop condition |
do-while | Loop condition |
Basic Syntax
continue;
Practical Examples
Example 1: Skip Even Numbers
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i); // Prints odd numbers only
}
return 0;
}
// Output: 1 3 5 7 9
Example 2: Skip Specific Value in while Loop
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
i++;
if (i == 5) {
continue; // Skip printing 5
}
printf("%d ", i);
}
return 0;
}
// Output: 1 2 3 4 6 7 8 9 10
Example 3: Skip Negative Numbers in Array
#include <stdio.h>
int main() {
int numbers[] = {5, -2, 8, -1, 3, -7, 4};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Positive numbers: ");
for (int i = 0; i < size; i++) {
if (numbers[i] < 0) {
continue; // Skip negative numbers
}
printf("%d ", numbers[i]);
}
return 0;
}
// Output: Positive numbers: 5 8 3 4
Example 4: User Input Validation
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter 5 positive numbers:\n");
for (int i = 1; i <= 5; i++) {
printf("Number %d: ", i);
scanf("%d", &num);
if (num <= 0) {
printf("Skipping non-positive number.\n");
continue; // Skip adding invalid numbers
}
sum += num;
}
printf("Sum of positive numbers: %d\n", sum);
return 0;
}
Important Differences: break vs continue
| Feature | break | continue |
|---|---|---|
| Effect | Exits loop completely | Skips only current iteration |
| Loop resumes | No | Yes (next iteration) |
| Use case | Stop entirely | Skip one iteration |
Comparison Example
#include <stdio.h>
int main() {
printf("Using continue:\n");
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i); // 1 2 4 5
}
printf("\n\nUsing break:\n");
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
printf("%d ", i); // 1 2
}
return 0;
}
Common Mistakes to Avoid
| Mistake | Why It's Wrong |
|---|---|
Using continue outside a loop | Compilation error |
Unreachable code after continue | Code never executes |
| Forgetting to update loop variable | Infinite loop (in while loop) |
Bad Example (Infinite Loop)
int i = 0;
while (i < 10) {
if (i == 5) {
continue; // i never increments → infinite loop
}
i++;
}
Fixed Version
int i = 0;
while (i < 10) {
i++; // Increment BEFORE continue
if (i == 5) {
continue;
}
printf("%d ", i);
}
Quick Summary
continueskips the rest of current loop iteration- Program flow jumps to next iteration
- Useful for filtering or skipping specific values
- Does not exit the loop (unlike
break) - Works in
for,while, anddo-whileloops
Complete Guide to Core & Advanced C Programming Concepts (Functions, Strings, Arrays, Loops, I/O, Control Flow)
https://macronepal.com/bash/building-blocks-of-c-a-complete-guide-to-functions/
Explains how functions in C work as reusable blocks of code, including declaration, definition, parameters, return values, and modular programming structure.
https://macronepal.com/bash/the-heart-of-text-processing-a-complete-guide-to-strings-in-c-2/
Explains how strings are handled in C using character arrays, string manipulation techniques, and common library functions for text processing.
https://macronepal.com/bash/the-cornerstone-of-data-organization-a-complete-guide-to-arrays-in-c/
Explains arrays in C as structured memory storage for multiple values, including indexing, initialization, and efficient data organization.
https://macronepal.com/bash/guaranteed-execution-a-complete-guide-to-the-do-while-loop-in-c/
Explains the do-while loop in C, where the loop body executes at least once before checking the condition.
https://macronepal.com/bash/mastering-iteration-a-complete-guide-to-the-for-loop-in-c/
Explains the for loop in C, including initialization, condition checking, and increment/decrement for controlled iteration.
https://macronepal.com/bash/mastering-iteration-a-complete-guide-to-while-loops-in-c/
Explains the while loop in C, focusing on condition-based repetition and proper loop control mechanisms.
https://macronepal.com/bash/beyond-if-else-a-complete-guide-to-switch-case-in-c/
Explains switch-case statements in C, enabling multi-branch decision-making based on variable values.
https://macronepal.com/bash/mastering-conditional-logic-a-complete-guide-to-if-else-statements-in-c/
Explains if-else statements in C for decision-making and controlling program flow based on conditions.
https://macronepal.com/bash/mastering-the-fundamentals-a-complete-guide-to-arithmetic-operations-in-c/
Explains arithmetic operations in C such as addition, subtraction, multiplication, division, and operator precedence.
https://macronepal.com/bash/foundation-of-c-programming-a-complete-guide-to-basic-input-output/
Explains basic input and output in C using scanf and printf for interacting with users and displaying results.