Unit 1: C++ Foundations & Windows 11 SetupLesson #4 / 20

Lesson 4: Conditional Logic: if, else if, else & switch-case

NSNextSemLast Updated: 5 Aug, 2026

🔀 Decision Making in C++

Control flow statements allow your program to branch dynamically based on variable values or conditional evaluation.


💡 Syntax Structure: if-else vs switch-case

  • 🎯 if / else if / else: Best for evaluating complex range conditions or boolean expressions (gpa >= 3.5 && credits > 60).
  • switch (expression): Optimized jump table generated by the compiler for testing discrete integer or enum values (case 1:, case 2:).

  • 💻 Code Example: Grading & Calculator Engine

    C++ Source Code (main.cpp)
    #include <iostream>
    int main() {
    char option;
    std::cout << "Choose Operation [A: Addition, S: Subtraction, M: Multiplication]: ";
    std::cin >> option;
    double num1 = 50.0, num2 = 10.0;
    switch (toupper(option)) {
    case 'A':
    std::cout << "Result: " << (num1 + num2) << std::endl;
    break;
    case 'S':
    std::cout << "Result: " << (num1 - num2) << std::endl;
    break;
    case 'M':
    std::cout << "Result: " << (num1 * num2) << std::endl;
    break;
    default:
    std::cout << "Invalid Option Selected!" << std::endl;
    break;
    }
    return 0;
    }
    Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
    Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

    Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

    (c) Microsoft Corporation. All rights reserved.

    C:\Users\Student\CPP_Project>#include <iostream>

    C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp C:\Users\Student\CPP_Project> main.exe Hello World from C++ on Windows 11 Pro! [Process exited with code 0 in 0.001 seconds]

    > 💡 Developer Joke: Why did the C++ developer get lost in the city?

    > Because they couldn't find the default: case! 😂

    Interactive Knowledge Check

    Test your understanding of Lesson #4 concepts

    What happens if a switch case statement does not end with a 'break;' statement?