Ch 6 · Conditional Statements · C
Topic 6 of 13 in C — Complete Tutorial (Beginner) — 2 lessons.
if / else if / else
A conditional statement lets your program take different actions depending on a condition. You write if, then a condition in round brackets, then a block in curly braces that runs only when the condition is true (non-zero). Add else if for more options and a final else as the catch-all. C checks the conditions top to bottom and runs the first true one, skipping the rest.
switch and the Ternary Operator
When comparing one variable against many specific values, a switch is cleaner than a long else if chain. Each case needs a break to stop "falling through" to the next; default is the catch-all. For a quick two-way choice that produces a value, the ternary operator condition ? a : b is a compact one-line if-else.