Understanding the continue Statement in C: A Beginner’s Guide
The continue statement in C is used to skip the current iteration of a loop and move directly to the next iteration. Unlike the break statement, which terminates the loop entirely, the continue statement simply bypasses the remaining code in the current iteration while keeping the loop active.
In this blog, we will explore the continue statement with examples ranging from basic to complex, along with detailed explanations for absolute beginners.
1. What is the continue Statement?
The continue statement is primarily used when certain iterations of a loop need to be skipped based on a specific condition.
Syntax:
continue;- When the
continuestatement is executed, all code following it in the loop is ignored for that iteration. - The control goes directly to the next iteration.
2. Simple Example: Using continue in a for Loop
This program demonstrates how the continue statement works in a simple loop.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the iteration when i is 5
}
printf("%d ", i);
}
printf("\nLoop completed.\n");
return 0;
}Output:
1 2 3 4 6 7 8 9 10
Loop completed.Explanation:
- The
forloop starts ati = 1and increments up to10. - When
i == 5, thecontinuestatement skips the rest of the code for that iteration and moves to the next value ofi. - As a result,
5is not printed.
3. Practical Applications of continue
1. Skipping Even Numbers in a Loop
This program demonstrates skipping specific values using the continue statement.
#include <stdio.h>
int main() {
printf("Odd numbers between 1 and 10:\n");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
printf("\nDone!\n");
return 0;
}Output:
Odd numbers between 1 and 10:
1 3 5 7 9
Done!Explanation:
- The
ifcondition checks whetheriis even (i % 2 == 0). - If true, the
continuestatement skips printingi. - This results in only odd numbers being printed.
2. Filtering Specific Characters in a String
This program demonstrates skipping specific characters when processing a string.
#include <stdio.h>
int main() {
char str[] = "hello world";
printf("String without vowels: ");
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
continue; // Skip vowels
}
printf("%c", str[i]);
}
printf("\n");
return 0;
}Output:
String without vowels: hll wrldExplanation:
- The loop iterates through each character in the string.
- The
ifcondition checks if the character is a vowel. - If true, the
continuestatement skips printing the vowel.
3. Nested Loops with continue
When used in nested loops, the continue statement affects only the innermost loop.
Example: Skipping Values 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 == 2
}
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}Output:
i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3Explanation:
- The
continuestatement skips the iteration in the inner loop (jloop) whenj == 2. - The outer loop (
iloop) remains unaffected.
4. Skipping Specific Rows in a Multiplication Table
This program skips rows where the multiplier is 5.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the 5th row
}
for (int j = 1; j <= 10; j++) {
printf("%2d ", i * j);
}
printf("\n");
}
return 0;
}Output (Partial):
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
6 12 18 24 30 36 42 48 54 60
... (skipped row for 5)4. Infinite Loops with continue
Using continue in an infinite loop allows selective skipping without terminating the loop.
#include <stdio.h>
int main() {
int i = 0;
while (1) {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
if (i >= 10) {
break; // Exit the loop when i >= 10
}
}
printf("\nLoop exited.\n");
return 0;
}Output:
1 3 5 7 9
Loop exited.Explanation:
- The
continuestatement skips printing for even numbers. - The
breakstatement ensures the loop exits afterireaches10.
5. Key Points to Remember
- The
continuestatement skips the remaining code in the current iteration and moves to the next iteration of the loop. - It is especially useful for filtering data or skipping unwanted conditions.
- When used in nested loops, it only affects the innermost loop.
- Use
continuecautiously to avoid making code harder to read.
Summary
The continue statement in C is a powerful tool for controlling the flow of loops. By understanding its behavior and applications, you can write cleaner and more efficient programs.
In the next blog, we’ll explore infinite loops and their practical applications in C. Stay tuned!