Conditionals

A conditional lets a program take one path or another depending on some test. C++ gives you three ways to express that: if/else for open-ended conditions, switch for checking one value against a fixed set of possibilities, and the ternary operator for a quick inline choice between two values.

if, else if, else

Conditions in an if chain are checked top to bottom, and the first one that's true wins — the rest are skipped entirely, even if they'd also be true:

</> grading.cpp
#include <iostream>

int main() {
    int score = 82;

    if (score >= 90) {
        std::cout << "Grade: A" << std::endl;
    } else if (score >= 80) {
        std::cout << "Grade: B" << std::endl;
    } else if (score >= 70) {
        std::cout << "Grade: C" << std::endl;
    } else {
        std::cout << "Grade: F" << std::endl;
    }
    return 0;
}
Terminal output
Grade: B

82 fails the >= 90 test but passes >= 80, so the program prints Grade: B and never even evaluates the remaining branches. Ordering these checks from highest to lowest threshold is what makes a single comparison per branch sufficient — writing score >= 80 first would wrongly catch a 95 as well, since it never gets the chance to check the more specific >= 90 case first.

switch

When you're comparing one variable against several exact values, switch is usually clearer than a long if/else if chain. Each matching case runs until it hits a break:

</> weekday.cpp
#include <iostream>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            std::cout << "Monday" << std::endl;
            break;
        case 2:
            std::cout << "Tuesday" << std::endl;
            break;
        case 3:
            std::cout << "Wednesday" << std::endl;
            break;
        default:
            std::cout << "Some other day" << std::endl;
    }
    return 0;
}
Terminal output
Wednesday

The break at the end of each case matters more than it looks. Without it, execution "falls through" into the next case regardless of whether that case's value matches — which is occasionally exactly what you want. Stacking cases with no code between them shares one body across several values:

</> weekend-check.cpp
#include <iostream>

int main() {
    int day = 6;

    switch (day) {
        case 6:
        case 7:
            std::cout << "Weekend" << std::endl;
            break;
        default:
            std::cout << "Weekday" << std::endl;
    }
    return 0;
}
Terminal output
Weekend

case 6 has no break, so if day is 6, execution falls straight through into case 7's body and prints Weekend. That's intentional fallthrough, used deliberately here — the dangerous version is forgetting a break where you meant each case to be independent, which silently runs the next case's code too.

The ternary operator

For a simple choice between two values, condition ? valueIfTrue : valueIfFalse is more compact than a full if/else, and it's an expression, so it can sit directly inside another statement:

</> ternary.cpp
#include <iostream>
#include <string>

int main() {
    int temperature = 15;
    std::string advice = (temperature < 18) ? "Wear a jacket" : "T-shirt weather";
    std::cout << advice << std::endl;
    return 0;
}
Terminal output
Wear a jacket

The ternary operator earns its keep for short, obvious choices like this one. Reach for a full if/else instead as soon as either branch needs more than a single expression, or as soon as nesting a second ternary inside the first one would make the line hard to read at a glance.

Note: switch in C++ only works on integer types, characters, and enumerations — you cannot switch on a std::string or a double the way some other languages allow. For those, an if/else if chain is the right tool.