C Programming • C17 baseline, standards-specific features labeled
Make Decisions with if and switch
Choose branches using explicit conditions and avoid accidentally treating assignment as comparison.
An if statement runs a branch when its condition is nonzero. Use braces even for short branches so later edits do not change which statements are controlled. A switch is useful for a fixed set of integral or enumeration values.
Example
#include <stdio.h>
int main(void) {
int temperature = 31;
if (temperature >= 30) {
puts("Warm");
} else {
puts("Cool");
}
return 0;
}
What to expect
Warm
Key idea: Write conditions so that the intended comparison is obvious and test both sides of each branch.