Beginner⏱️ 8 min📘 Topic 8 of 32

🔀 C++ if, else and switch — Conditional Statements with Examples

Learn C++ conditional statements: if, else if, else, nested conditions and switch. Includes the ternary operator, fall-through behavior and interview questions.

Conditionals let your program take different paths based on data. Two forms cover 99% of cases:

🟦 if / else if / else

if (score >= 90)      grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else                  grade = 'F';

🟩 switch — multi-value dispatch

switch (day) {
  case 1: std::cout << "Mon"; break;
  case 2: std::cout << "Tue"; break;
  case 3: std::cout << "Wed"; break;
  default: std::cout << "?";
}

⚠️ The fall-through gotcha

Without break, execution falls through to the next case. Sometimes intentional, usually a bug. In C++17+ you can mark it explicit with [[fallthrough]];.

❓ Ternary for short branches

std::string status = (age >= 18) ? "adult" : "minor";

💡 When to choose which

  • if / else — ranges, complex boolean conditions
  • switch — exact value matches (int, enum, char)
  • ternary — short inline value choice

💻 Code Examples

Grade calculator

int score = 85;
char grade;
if      (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else                  grade = 'F';
std::cout << grade;
Output:
B

switch with intentional fall-through

switch (month) {
  case 12: case 1: case 2:
    std::cout << "Winter"; break;
  case 3:  case 4: case 5:
    std::cout << "Spring"; break;
  case 6:  case 7: case 8:
    std::cout << "Summer"; break;
  default:
    std::cout << "Fall";
}
Output:
Winter / Spring / Summer / Fall depending on month.

⚠️ Common Mistakes

  • Forgetting `break` in switch — silent fall-through bugs.
  • Using switch with `double` or `std::string` — only integral types (int, char, enum, bool) allowed.
  • Writing `if (x = 5)` (assignment) instead of `if (x == 5)` (comparison).
  • Deeply nested if/else — extract guard clauses or use early returns.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.When should you use switch instead of if/else?Beginner
When you're checking ONE variable against many EXACT integral values. switch can be faster (compiler may build a jump table) and reads more cleanly than long if/else chains.
Q2.What happens if you forget `break` in a switch case?Beginner
Execution falls through to the next case. Sometimes useful (e.g., grouping cases), but usually a bug. Most compilers warn about implicit fall-through.
Q3.Can a switch statement work on strings in C++?Intermediate
No — switch in C++ only works on integral types (int, char, enum, bool). For strings, use if/else chains or an unordered_map of string → function.
Q4.What is `[[fallthrough]]`?Intermediate
A C++17 attribute marking an intentional fall-through case. It tells the compiler 'I meant to do this, don't warn me.' Improves readability and silences warnings.
Q5.Is the ternary operator a statement or expression?Advanced
An expression — it returns a value. Unlike if/else, you can use it in an initializer or as a function argument: `return (a > b) ? a : b;`.

🧠 Quick Summary

  • if/else for ranges and complex conditions.
  • switch for exact integral-value matches.
  • Always `break` unless you intentionally fall through.
  • Ternary `?:` is an expression — use for short value choices.
  • Watch for `=` vs `==` typos in conditions.