The continue statement in C is used within loops to skip the current iteration and proceed to the next one.
When encountered, continue stops the current loop cycle and goes back to the loop’s condition check, effectively bypassing the remaining code in the loop body for that iteration.
1. Syntax of continue Statement
continue;
The continue statement is typically used inside loops like for, while, and do-while to skip specific iterations based on certain conditions.
2. Basic Example of continue in a for Loop
Let’s start with a simple example where we use continue to skip printing the number 3 in a sequence from 1 to 5.
#include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip the iteration when i equals 3 } printf("%d\n", i); } return 0; }
Explanation:
- The if (i == 3) statement checks if i is 3.
- When i is 3, continue is executed, skipping the printf statement for that iteration.
Output:
1 2 4 5
3. Using continue in a while Loop
Here’s an example using continue in a while loop to skip a specific number.
#include <stdio.h> int main() { int i = 1; while (i <= 5) { if (i == 3) { i++; // Increment i before continue to avoid an infinite loop continue; } printf("%d\n", i); i++; } return 0; }
Explanation:
- continue skips the print statement for i = 3.
- It’s essential to increment i before continue to prevent an infinite loop.
Output:
1 2 4 5
4. continue with for Loop to Skip Even Numbers
This example uses continue to skip even numbers in a for loop.
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } printf("%d\n", i); } return 0; }
Explanation:
- i % 2 == 0 checks if i is even.
- If true, continue skips to the next iteration, effectively printing only odd numbers.
Output:
1 3 5 7 9
5. continue with while Loop to Skip Odd Numbers
Here’s an example that skips odd numbers in a while loop.
#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i % 2 != 0) { i++; continue; // Skip odd numbers } printf("%d\n", i); i++; } return 0; }
Explanation:
- i % 2 != 0 checks if i is odd.
- If true, continue skips the printf statement, printing only even numbers.
Output:
2 4 6 8 10
6. continue in a Nested Loop
You can use continue in nested loops to skip specific iterations in the inner loop.
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { continue; // Skip the iteration when j equals 2 } printf("i = %d, j = %d\n", i, j); } } return 0; }
Explanation:
- The inner loop skips the iteration when j is 2.
- This example demonstrates how continue affects only the inner loop, as it applies only to the loop it’s directly inside.
Output:
i = 1, j = 1 i = 1, j = 3 i = 2, j = 1 i = 2, j = 3 i = 3, j = 1 i = 3, j = 3
7. Using continue to Skip Specific Characters in a String
You can use continue to skip specific characters in a loop that iterates over a string.
#include <stdio.h> int main() { char str[] = "Hello World"; int i = 0; while (str[i] != '\0') { if (str[i] == 'o') { i++; continue; // Skip character 'o' } printf("%c", str[i]); i++; } printf("\n"); return 0; }
Explanation:
- The loop checks each character in str.
- continue skips printing the character if it’s an ‘o'.
Output:
Hell Wrld
8. Using continue to Filter Elements in an Array
You can use continue to filter specific values when iterating over an array.
#include <stdio.h> int main() { int arr[] = {1, 2, -3, 4, -5, 6}; int size = sizeof(arr) / sizeof(arr[0]); printf("Positive numbers in the array:\n"); for (int i = 0; i < size; i++) { if (arr[i] < 0) { continue; // Skip negative numbers } printf("%d\n", arr[i]); } return 0; }
Explanation:
- The loop iterates over each element of the array.
- If the element is negative, continue skips printing that value.
Output:
Positive numbers in the array: 1 2 4 6
9. Using continue to Skip Multiples of a Number
You can use continue to skip specific multiples in a loop, such as multiples of 3.
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i % 3 == 0) { continue; // Skip multiples of 3 } printf("%d\n", i); } return 0; }
Explanation:
- i % 3 == 0 checks if i is a multiple of 3.
- If true, continue skips printing that number.
Output:
1 2 4 5 7 8 10
10. continue in a do-while Loop
The continue statement can also be used in do-while loops to skip certain iterations.
#include <stdio.h> int main() { int i = 1; do { if (i == 5) { i++; continue; // Skip when i equals 5 } printf("%d\n", i); i++; } while (i <= 10); return 0; }
Explanation:
- The do-while loop skips printing when i is 5.
- Since i is incremented after continue, the loop does not print 5.
Output:
1 2 3 4 6 7 8 9 10
Summary Table of continue Statement Examples
Use Case | Description | Example Code |
---|---|---|
Skipping a single number | Skips printing one specific number | if (i == 3) continue; |
Skipping even numbers | Skips printing even numbers | if (i % 2 == 0) continue; |
Skipping odd numbers | Skips printing odd numbers | if (i % 2 != 0) continue; |
Nested loop with continue | Skips specific values in an inner loop | for (j = 1; j <= 3; j++) if (j == 2) continue; |
Skipping characters in a string | Skips specific characters in a string | if (str[i] == ‘o') continue; |
Filtering array elements | Skips printing negative numbers | if (arr[i] < 0) continue; |
Skipping multiples of a number | Skips multiples of a specific number | if (i % 3 == 0) continue; |
do-while loop | Skips an iteration in a do-while loop | if (i == 5) continue; in do-while |
Complete Example: Multiple Uses of continue Statement
This example demonstrates using continue to skip negative values in an array and odd values
in a loop.
#include <stdio.h> int main() { int arr[] = {10, -3, 15, -7, 8}; int size = sizeof(arr) / sizeof(arr[0]); printf("Positive numbers in the array:\n"); for (int i = 0; i < size; i++) { if (arr[i] < 0) { continue; // Skip negative numbers } printf("%d\n", arr[i]); } printf("Even numbers from 1 to 10:\n"); for (int j = 1; j <= 10; j++) { if (j % 2 != 0) { continue; // Skip odd numbers } printf("%d\n", j); } return 0; }
Explanation:
- The first for loop skips negative values in arr using continue.
- The second for loop skips odd numbers from 1 to 10, printing only even numbers.
Output:
Positive numbers in the array: 10 15 8 Even numbers from 1 to 10: 2 4 6 8 10
The continue statement is useful for selectively skipping iterations in loops, allowing you to control program flow more efficiently.