Use of the continue statement goes beyond simple iteration skipping; it's a key technique for improving code readability by reducing nesting and requires understanding its precise behavior in different loop types.
Uses of the continue Statement in C ⏩
While continue has a simple definition—skip to the next iteration—its application is primarily about structuring code for clarity and avoiding common pitfalls in complex loops.
1. Improving Readability by Reducing Nesting (Guard Clauses)
This is the most powerful and common use of continue. Instead of nesting your main loop logic inside an if statement, you can use continue at the beginning of the loop to handle invalid or uninteresting cases. This pattern is sometimes called using guard clauses.
- The Problem (Deeply Nested Logic): The main logic is buried, making it hard to read.
C
// Processing a list of network packets
for (int i = 0; i < packet_count; i++) {
if (packets[i].is_valid) {
if (packets[i].port == 80) {
// ...
// Ten lines of main processing logic here,
// deeply indented and hard to follow.
// ...
}
}
}
- The Solution (Flattened Logic with continue):
The code is flattened and much easier to read. The checks at the top "guard" the main logic.
C
for (int i = 0; i < packet_count; i++) {
// Guard clause 1: Skip invalid packets
if (!packets[i].is_valid) {
continue;
}
// Guard clause 2: Skip non-HTTP packets
if (packets[i].port != 80) {
continue;
}
// Main processing logic is now at the top level, not nested.
// It's clear this code only runs for valid, port 80 packets.
// ...
}
2. continue Behavior in for vs. while Loops
A subtle but critical detail is that continue behaves differently depending on the type of loop it's in. Misunderstanding this can lead to infinite loops.
- In a for loop: continue jumps to the modification expression (e.g., i++). This is generally safe.
- In a while or do-while loop: continue jumps to the conditional test.
The while Loop Pitfall
If the variable update occurs after the continue statement in a while loop, you can accidentally create an infinite loop.
Example: An Infinite while Loop
C
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
if (i == 3) {
// When i is 3, we jump back to the 'while (i < 5)' check.
// The 'i++' at the bottom is skipped.
// Since 'i' is still 3, the condition is still true, and we
// continue forever. This is an infinite loop.
continue;
}
printf("%d ", i);
i++;
}
return 0;
}
The Fix: In a while loop, ensure the variable update happens before any potential continue statement.
C
// Corrected while loop
while (i < 5) {
i++; // Update before the check
if (i == 3) {
continue;
}
printf("%d ", i);
}
3. Enhancing Clarity and Expressing Intent
An programmer uses control flow to make the code's purpose clear. continue can be more expressive than a complex if-else structure.
It explicitly signals to the reader: "The work for this specific item is done; skip it and move on to the next one."
This is most effective when you are filtering a list.
Example: Processing User Records
C
#include <stdio.h>
#include <stdbool.h>
typedef struct {
int id;
bool is_active;
bool needs_email_update;
} User;
int main() {
User users[3] = {{1, true, false}, {2, false, true}, {3, true, true}};
for (int i = 0; i < 3; i++) {
// Using continue to express "skip non-active users"
if (!users[i].is_active) {
continue;
}
// Using continue to express "skip users who don't need an update"
if (!users[i].needs_email_update) {
continue;
}
// The intent is clear: this code only runs for active users
// who need an email update.
printf("Sending email update to user ID %d...\n", users[i].id);
}
return 0;
}