Control Flow
Break and Continue
continue skips the rest of the current iteration, while break exits the loop.
Break and Continue
break_continue.c
#include <stdio.h>
int main(void) {
int stop = ;
int total = 0;
for (int i = 1; i <= 6; i++) {
if (i == 2) {
continue;
}
if (i == stop) {
break;
}
total += i;
}
printf("total=%d\n", total);
return 0;
}
#include <stdio.h>
int main(void) {
int stop = ;
int total = 0;
for (int i = 1; i <= 6; i++) {
if (i == 2) {
continue;
}
if (i == stop) {
break;
}
total += i;
}
printf("total=%d\n", total);
return 0;
}
#include <stdio.h>
int main(void) {
int stop = ;
int total = 0;
for (int i = 1; i <= 6; i++) {
if (i == 2) {
continue;
}
if (i == stop) {
break;
}
total += i;
}
printf("total=%d\n", total);
return 0;
}
continue
`continue` jumps to the next loop iteration.
break
`break` leaves the loop immediately.