Branching: if-else, ternary, switch-case
Choose between if-else chains, ternary expressions, and switch statements for clear branching.
The if Statement
The basic conditional. The body executes when the condition is true. Curly braces are optional for a single statement but always recommended.
int score = 85;
if (score >= 60) {
std::cout << "Pass\n";
}if-else Chains
Test alternatives with else if and a final else. The first true branch runs.
if (score >= 90) std::cout << "A";
else if (score >= 80) std::cout << "B";
else if (score >= 70) std::cout << "C";
else std::cout << "F";All lessons in this course
- Branching: if-else, ternary, switch-case
- Loop Patterns: for, while, do-while
- Range-Based for Loop with Containers
- Early Returns break continue and goto