🔀 C++ Conditional Statements: if-else and switch

Soumik DastidarSoumik Dastidar
3 min read

❓ Why Use Conditions?

Conditions allow your program to choose different paths based on logical checks. Think of it like this:

“If the user is above 18, allow access. Otherwise, deny access.”

✅ if, else if, and else

🧠 Syntax:

if (condition) {
    // code block
} else if (another_condition) {
    // another block
} else {
    // fallback block
}

📌 Example:

int age;
cin >> age;

if (age >= 18) {
    cout << "Access granted.";
} else {
    cout << "Access denied.";
}

🧠 else if: Multiple Conditions

int marks;
cin >> marks;

if (marks >= 90) {
    cout << "Grade: A";
} else if (marks >= 80) {
    cout << "Grade: B";
} else if (marks >= 70) {
    cout << "Grade: C";
} else {
    cout << "Grade: F";
}

🪓 switch Statement

Use switch When you want to compare a single variable against multiple fixed values (like menu choices, grades, days of the week).

🧠 Syntax:

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // fallback code
}

📌 Example:

int day;
cin >> day;

switch (day) {
    case 1: cout << "Monday"; break;
    case 2: cout << "Tuesday"; break;
    case 3: cout << "Wednesday"; break;
    default: cout << "Invalid day";
}

⚠️ if vs. switch – When to Use What?

Featureif-elseswitch
Works withAll types (int, float, bool…)Only integers & chars
ConditionsComplex & logicalSimple equality checks only
FlexibilityHighLow

🚫 Common Mistakes

❌ Forgetting to use break in switch:

case 1: cout << "One";  // will fall through to next case

❌ Using switch with a string or float (not allowed in C++)
❌ Putting a semicolon after if condition:

if (x > 0);  // ❌ This ends the if statement early

💼 Task for You

🔧 Task:

  1. Take a user’s exam score as input.

  2. Use if-else to print their grade based on:

    • 90+: A

    • 80-89: B

    • 70-79: C

    • Else: F

  3. Then take a number between 1–7 as input and use switch to print the corresponding day name.

🖨️ Sample Output:

Enter your score: 85
Grade: B

Enter day number (1-7): 3
Day: Wednesday

🎯 What’s Next?

With conditional logic mastered, you're ready to repeat actions using loops! In the next part, we’ll cover for, while, and do-while Loops to add repetition to your programs.

📍Next up: Part 9 – Loops in C++

✨ Follow the Series

C++ Zero to Hero is your step-by-step journey to becoming confident in C++. From the very basics to algorithms and problem-solving — one topic at a time.

👣 Stay consistent. Practice every task. And soon, you’ll be writing real-world C++ applications like a pro.

0
Subscribe to my newsletter

Read articles from Soumik Dastidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Soumik Dastidar
Soumik Dastidar

I'm Soumik—a passionate problem solver, aspiring software developer, and lifelong learner. Currently building a C++ Complete Series to help beginners master programming from scratch. I love breaking down complex concepts, automating real-world tasks, and sharing what I learn. Let's grow together, one line of code at a time.