C chooses which statements run with if, switch, and loops (next topic). There is no goto prohibition — the keyword exists — but Wave A treats it as a last resort (one use: breaking out of a nested error path in C with no exceptions).
Mental model: a condition is an integer expression. Zero is false; anything else is true. Pointers: NULL is false. That is not Java. A mistyped if (p = q) compiles.
if (score >= 90) {
grade = 'A';
} else if (score >= 75) {
grade = 'B';
} else {
grade = 'C';
}
if (ready); { start(); } — start always runs.if (ready) for flags. Prefer if (n != 0) for counts. Prefer if (p != NULL) for pointers (or if (p) if the project style is consistent)./* Incorrect */
if (ok);
puts("ok");
/* Correct */
if (ok) {
puts("ok");
}
Selector is an integer type (including char and enums). Not a string. Not a float.
switch (cmd) {
case 'a':
do_a();
break;
case 'b':
case 'c':
do_bc();
break;
default:
do_other();
break;
}
break, execution falls through. Comment intentional fall-through.case labels must be integer constant expressions.default is optional. Put it last.case need a block: case 1: { int x = 0; … } (C23 eases some of this; do not assume C23).cond ? a : b is an expression. Both branch types convert to a common type. Only the chosen branch is evaluated.
const char *msg = n == 1 ? "item" : "items";
Do not nest ternaries. Do not use a ternary for two side-effecting procedures.
if (x = 0) always false and assigns. Some compilers warn; -Wall helps.switch on a char that might be signed — high-bit bytes become negative case labels you never wrote.== in a condition.break, return, or a commented fall-through.~ Good luck with your revision! ~