Conditional Branching (if / else & switch) in C++
📌 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!
🚀 Practice in ArkyEdu Labs & Related Guides
🎮 Launch Interactive Lab →
Structura Alternativă: Instrucțiunea if / else & Operatori Logici
📖 Full Lesson & Curriculum →
Clasa a VII-a · Decizii if / else în C++
🏫 Grade 6 Hub →
All modules & guides for this grade
🏫 Grade 7 Hub →
All modules & guides for this grade
🏫 Grade 8 Hub →
All modules & guides for this grade
📚 Top 10 C++ Errors Fixed →
Troubleshoot the 10 most common C++ mistakes in Grades 7–8 (Code::Blocks): = vs ==, stray semicolons after if/while, infinite loops, 5/2 integer division, and n = 0.
❓ 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)Assignment (=) vs. Equality Check (==) in C++ →
A single equals sign (=) is the assignment operator that stores a value into a variable (x = 5). Double equals (==) is the comparison operator that tests whether two values are equal (x == 5).
while vs. do-while Loops in C++ →
Both while and do-while repeat code as long as a condition holds true. A while loop checks the condition first (may run 0 times), whereas a do-while loop checks at the end (always runs at least once).
Logical Spreadsheet Functions: IF & COUNTIF →
The IF function tests a condition and returns one result when true and another when false. COUNTIF counts how many cells in a range match a given criterion (such as scores >= 8).
Algorithm & Core Algorithm Properties →
An algorithm is a finite, unambiguous, step-by-step set of instructions that transforms input data into the desired output within a finite number of steps.