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, a condition in round brackets, then a block in curly braces that runs only when the condition is true. Add else if for more options and else as the catch-all. C++ checks conditions top to bottom and runs the first true one, skipping the rest.
switch and the Ternary
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 producing a value, the ternary operator condition ? a : b is a compact one-line if-else.