🚀
ArkyEdu GLOSAR TIC
RO Practice Lab
Algorithms, C++ & Scratch Grades 6–8 RO Term: Structura Alternativă (if / else și switch) în C++

Conditional Branching (if / else & switch) in C++

💬 Share on WhatsApp
📌 Quick Definition (Answer-First)

Conditional branching lets a program choose between different execution paths depending on whether a logical condition is true or false (if / else), or by matching discrete constant values (switch).

How It Works & Why It Matters

Wrap multi-line branches inside curly braces { } so every intended statement belongs to the branch.

The switch statement cleanly replaces long else-if chains for menus or exact integer/char matches—remembering to end each case with break;.

💻 if / else & switch Syntax
if (score >= 5) {
  cout << "Passed";
} else {
  cout << "Try again";
}

switch (choice) {
  case 1: cout << "Play"; break;
  default: cout << "Invalid";
}

🏫 Classroom Example

Determining whether an input year is a leap year uses nested if/else with && and ||.

⚖️ if/else vs. switch

Use if/else for ranges and boolean expressions; use switch for fixed discrete integer or char choices.

⚠️ Common Mistake in Exams & Homework

Adding a stray semicolon after if (condition); — that terminates the if statement with an empty body!

❓ Quick Questions & Answers

What happens if you omit break inside a switch case?
Execution falls straight through into the next case below it until a break or closing brace is reached.

Related Glossary Terms

← Back to Full Glossary (A–Z)