</>
Skip to content
C++ lessons (14/42)

C++ — If Else

Basic if statement

#include <iostream>
using namespace std;

int main() {
    int temperature = 28;

    if (temperature > 25) {
        cout << "It's warm outside!" << endl;
    }

    return 0;
}

if-else

#include <iostream>
using namespace std;

int main() {
    int hour = 14;

    if (hour < 12) {
        cout << "Good morning!" << endl;
    } else {
        cout << "Good afternoon!" << endl;
    }

    return 0;
}

if-else if-else

#include <iostream>
using namespace std;

int main() {
    int score = 85;

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

    return 0;
}

if with initializer (C++17)

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

optional<int> findValue() {
    return 42;
}

int main() {
    // C++17: declare variable in if condition
    if (auto val = findValue(); val.has_value()) {
        cout << "Found: " << val.value() << endl;
    } else {
        cout << "Not found" << endl;
    }

    // Also useful for maps
    #include <map>
    map<string, int> scores = {{"Alice", 95}, {"Bob", 87}};
    if (auto it = scores.find("Alice"); it != scores.end()) {
        cout << "Alice: " << it->second << endl;
    }

    return 0;
}

switch statement

#include <iostream>
using namespace std;

int main() {
    int day = 3;

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

    return 0;
}

switch with enum

#include <iostream>
using namespace std;

enum class Color { RED, GREEN, BLUE };

int main() {
    Color c = Color::GREEN;

    switch (c) {
        case Color::RED:
            cout << "Red" << endl;
            break;
        case Color::GREEN:
            cout << "Green" << endl;
            break;
        case Color::BLUE:
            cout << "Blue" << endl;
            break;
    }

    return 0;
}

Ternary operator

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    string status = (age >= 18) ? "Adult" : "Minor";
    cout << status << endl;

    // Nested ternary (avoid nesting deeply)
    int score = 85;
    string grade = (score >= 90) ? "A" :
                   (score >= 80) ? "B" :
                   (score >= 70) ? "C" : "F";
    cout << "Grade: " << grade << endl;

    return 0;
}

Nested if

#include <iostream>
using namespace std;

int main() {
    int age = 25;
    bool hasTicket = true;

    if (age >= 18) {
        if (hasTicket) {
            cout << "Welcome to the event!" << endl;
        } else {
            cout << "You need a ticket." << endl;
        }
    } else {
        cout << "Sorry, you must be 18+." << endl;
    }

    return 0;
}

Combining conditions

#include <iostream>
using namespace std;

int main() {
    int age = 25;
    int income = 50000;

    if (age >= 18 && income >= 30000) {
        cout << "Qualifies for premium card." << endl;
    }

    if (age < 12 || age > 65) {
        cout << "Discounted ticket." << endl;
    }

    int isBanned = 0;
    if (!isBanned) {
        cout << "Access granted." << endl;
    }

    return 0;
}

Best practices

  • Use early returns to reduce nesting
  • Prefer enum class over integers in switch
  • Always provide a default case
  • Use [[fallthrough]] to document intentional fallthrough
  • Consider if constexpr for compile-time branching

Mini Practice

Write C++ code that:

  1. Uses if-else to classify a number as positive, negative, or zero
  2. Uses switch with an enum class
  3. Uses C++17 if-with-initializer
  4. Combines conditions with && and ||

Up Next

In the next lesson, you'll learn about Loops — for, while, and range-based loops.

Related Topics

Frequently Asked Questions about If Else

What is If Else in C++?

If Else is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn If Else?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn If Else.

Why is If Else important in C++?

If Else is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.