0Pricing
C++ Academy · Lesson

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

  1. Branching: if-else, ternary, switch-case
  2. Loop Patterns: for, while, do-while
  3. Range-Based for Loop with Containers
  4. Early Returns break continue and goto
← Back to C++ Academy