Control Flow
Break and Continue
continue skips the rest of the current iteration, while break exits the loop.
continue
`continue` jumps to the next loop iteration.
break
`break` leaves the loop immediately.
Break and Continue
break_continue.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int stop = 5;
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 = 4;
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 = 6;
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;
}
stop ← 5, total ← 0
3int main(void) {4 int stop→ 5 = 5; //@stop=4, 65 int total→ 0 = 0;total ← 1
pass 1 of 57for (int i1 = 1; i <= 6; i++) {8 if (i == 2) {9 continue;10 }11 if (i == stop) {12 break;13 }14 total→ 1 += i1;15}All 5 passes — pass 1 is the card above pass istoptotal1 1 — 0 → 1 2 2 — — 3 3 — 1 → 4 4 4 — 4 → 8 5 5 5 — if (i == 2)
7for (int i = 1; i <= 6; i++) {8 if (i2 == 2) {9 continue;10 }if (i == stop)
10}11if (i5 == stop5) {12 break;13}printf("total=%d ", total);
17 printf("total=%d\n", total8);18 return 0;19}outputtotal=8
stop ← 4, total ← 0
3int main(void) {4 int stop→ 4 = 4;5 int total→ 0 = 0;total ← 1
pass 1 of 47for (int i1 = 1; i <= 6; i++) {8 if (i == 2) {9 continue;10 }11 if (i == stop) {12 break;13 }14 total→ 1 += i1;15}All 4 passes — pass 1 is the card above pass istoptotal1 1 — 0 → 1 2 2 — — 3 3 — 1 → 4 4 4 4 — if (i == 2)
7for (int i = 1; i <= 6; i++) {8 if (i2 == 2) {9 continue;10 }if (i == stop)
10}11if (i4 == stop4) {12 break;13}printf("total=%d ", total);
17 printf("total=%d\n", total4);18 return 0;19}outputtotal=4
stop ← 6, total ← 0
3int main(void) {4 int stop→ 6 = 6;5 int total→ 0 = 0;total ← 1
pass 1 of 67for (int i1 = 1; i <= 6; i++) {8 if (i == 2) {9 continue;10 }11 if (i == stop) {12 break;13 }14 total→ 1 += i1;15}All 6 passes — pass 1 is the card above pass istoptotal1 1 — 0 → 1 2 2 — — 3 3 — 1 → 4 4 4 — 4 → 8 5 5 — 8 → 13 6 6 6 — if (i == 2)
7for (int i = 1; i <= 6; i++) {8 if (i2 == 2) {9 continue;10 }if (i == stop)
10}11if (i6 == stop6) {12 break;13}printf("total=%d ", total);
17 printf("total=%d\n", total13);18 return 0;19}outputtotal=13