Aral Code

Control Flow Lesson 7 of 17

C++ if, else, switch and the Ternary Operator

Make decisions in C++ with if, else if, else, switch-case and the ternary operator, including if with an initializer and switch on characters.

Decisions in C++ work just like in C and Java: if/else for any condition, switch for exact values, and the ternary ?: for quick either-or values.

if, else if, else

#include <iostream>
#include <string>
using namespace std;

string descriptor(int g) {
    if (g >= 90) return "Outstanding";
    else if (g >= 85) return "Very Satisfactory";
    else if (g >= 80) return "Satisfactory";
    else if (g >= 75) return "Fairly Satisfactory";
    else return "Did Not Meet Expectations";
}

int main() {
    int grades[] = {93, 86, 74};
    for (int g : grades) {
        cout << g << " - " << descriptor(g) << endl;
    }
    return 0;
}

Output

93 - Outstanding
86 - Very Satisfactory
74 - Did Not Meet Expectations

The checks go from the highest range down, and only the first match runs. (We put the logic in a function and used a range-based for loop; both get full lessons soon.)

Combining conditions

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    bool registered = true;
    double total = 1200;
    bool member = false;

    if (age >= 18 && registered) {
        cout << "You can vote." << endl;
    }
    if (total >= 1000 || member) {
        cout << "Free delivery!" << endl;
    } else {
        cout << "Delivery fee: P50" << endl;
    }
    return 0;
}

Output

You can vote.
Free delivery!

if with an initializer (C++17)

You can declare a variable inside the if itself, keeping it local to the decision:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string section = "Grade 10 - Rizal";
    if (auto pos = section.find("Rizal"); pos != string::npos) {
        cout << "Found at index " << pos << endl;
    } else {
        cout << "Not found" << endl;
    }
    return 0;
}

Output

Found at index 11

switch

#include <iostream>
using namespace std;

int main() {
    char op = '*';
    double a = 6, b = 7;
    switch (op) {
        case '+': cout << a + b << endl; break;
        case '-': cout << a - b << endl; break;
        case '*': cout << a * b << endl; break;
        case '/':
            if (b != 0) cout << a / b << endl;
            else cout << "Cannot divide by zero" << endl;
            break;
        default: cout << "Unknown operator" << endl;
    }
    return 0;
}

Output

42

switch works on integers, characters and enums, but not on strings or decimals. Each case needs a break, or execution falls through into the next case.

Grouped cases

#include <iostream>
using namespace std;

int main() {
    int month = 7;
    switch (month) {
        case 12: case 1: case 2:
            cout << "Amihan season" << endl; break;
        case 3: case 4: case 5:
            cout << "Tag-init (summer)" << endl; break;
        default:
            cout << "Habagat (rainy season)" << endl;
    }
    return 0;
}

Output

Habagat (rainy season)

The ternary operator

#include <iostream>
using namespace std;

int main() {
    int items = 3;
    cout << items << (items == 1 ? " item" : " items") << endl;
    int n = 14;
    cout << n << " is " << (n % 2 == 0 ? "even" : "odd") << endl;
    return 0;
}

Output

3 items
14 is even

Enums with switch

An enum class gives names to a set of options, which reads better than magic numbers:

#include <iostream>
using namespace std;

enum class Light { Red, Yellow, Green };

int main() {
    Light now = Light::Yellow;
    switch (now) {
        case Light::Red:    cout << "Stop" << endl; break;
        case Light::Yellow: cout << "Slow down" << endl; break;
        case Light::Green:  cout << "Go" << endl; break;
    }
    return 0;
}

Output

Slow down

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Can a C++ switch work directly on a std::string?
2. What does (5 > 3 ? "yes" : "no") give?
3. What's special about if (auto x = f(); x > 0)?
4. What does enum class Light { Red, Yellow, Green }; create?

By Sir Reyel · Last updated 2026-12-04