C — Continue statements
continue skips the remainder of the current loop body. In a C for loop, control moves to the update expression before the next condition test.
A simple example
Skip just the number 2
- Start with Visit 1 through 3
- Do this Skip 2
- Result 1 → 3
#include <stdio.h>
int main(void) {
for (int n = 1; n <= 3; n++) {
if (n == 2) {
continue;
}
printf("%d\n", n);
}
}
1 3
Exclude invalid readings while keeping zero
Skip negative readings, which represent invalid measurements here. Count every valid reading, including zero, and add it to the total. The update advances i even for a negative reading. Testing for a negative value specifically keeps zero in the valid count.
A counter records visitors in each time interval. A negative reading signals a fault, while zero means the counter worked and nobody entered. Keeping 0, 4 and 6 gives three valid intervals and ten visitors.
Run the example
Use a C17 compiler. Run these commands from the directory containing the example. Use a Unix-style shell.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson
#include <stdio.h>
int main(void) {
int readings[] = {-2, 0, 4, -1, 6};
int count = 0, sum = 0;
for (int i = 0; i < 5; ++i) {
if (readings[i] < 0) continue;
++count;
sum += readings[i];
}
printf("%d readings, sum %d\n", count, sum);
return 0;
}
3 readings, sum 10
The valid values are 0, 4, and 6. Skipping the negative entries changes the count without losing the measured zero.
Try it yourself
Append −3 and then 0. What should the count and sum become?
Check your answer
The count rises to 4 and the sum stays 10. Only the appended zero is a valid measurement.