Conditions and loops
if / else, switch, for, while and do … while.
Runs in Zerethon IDE · C (GCC 14)
Official C documentation ↗
main.c
#include <stdio.h>
int main(void) {
for (int n = 1; n <= 4; n++) {
if (n % 2 == 0) {
printf("%d even\n", n);
} else {
printf("%d odd\n", n);
}
}
char op = '*';
switch (op) {
case '+':
puts("plus");
break;
case '*':
puts("times");
break;
default:
puts("unknown");
}
int i = 3;
while (i > 0) {
i--;
if (i == 1) continue;
printf("i = %d\n", i);
}
return 0;
}
Output
1 odd 2 even 3 odd 4 even times i = 2 i = 0
Watch out: Without break, a case falls through into the next one. And if (x = 5) assigns instead of comparing — use == (gcc warns with -Wall).