C continue Statement
The continue statement skips the remaining code of the current round and jumps to the next round of the loop.
Skip even numbers
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 8; i++) {
if (i % 2 == 0) {
continue; // go to next i
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
Output of the above program is
1 3 5 7
Add only positive numbers
#include <stdio.h>
int main(void) {
int data[] = {5, -2, 8, -7, 10};
int sum = 0;
for (int i = 0; i < 5; i++) {
if (data[i] < 0) continue;
sum += data[i];
}
printf("Sum of positive = %d\n", sum);
return 0;
}
Output:
Output of the above program is
Sum of positive = 23
break vs continue
| break | continue |
|---|---|
| Stops the whole loop | Skips only the current round |
| Control goes after the loop | Control goes to the next round |
Note:
In a while loop be careful: if the counter is increased after continue, it never runs and the loop can become infinite. Increase it before the continue.
Next: C goto Statement.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |