C Programming: Memory, Pointers and the Stack ยท Control Flow
Skip a bad value with continue and stop at a sentinel with break.
#include <stdio.h>
int main(void) {
int nums[6] = {3, -1, 4, 0, 5, 9};
int total = 0;
for (int i = 0; i < 6; i++) {
if (nums[i] < 0) {
continue;
}
if (nums[i] == 0) {
break;
}
total += nums[i];
}
printf("total = %d\n", total);
return 0;
}nums is created on the stack with the value [3, -1, 4, 0, 5, 9].total is created on the stack with the value 0.i is created on the stack with the value 0.nums[i] < 0 to decide which way the code goes.nums[i] == 0 to decide which way the code goes.i changes from 0 to 1.nums[i] < 0 to decide which way the code goes.i changes from 1 to 2.nums[i] < 0 to decide which way the code goes.nums[i] == 0 to decide which way the code goes.total changes from 3 to 7.i changes from 2 to 3.nums[i] < 0 to decide which way the code goes.nums[i] == 0 to decide which way the code goes.total = 7.0.