Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

C++ Conditional Statements — if, else, switch | Tutorials Logic

if / else if / else

Conditional statements let your program make decisions. The if block runs when the condition is true; else runs when it's false.

if / else if / else
#include <iostream>
using namespace std;

int main() {
    int score = 75;

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

    // Nested if
    int age = 20;
    bool hasID = true;
    if (age >= 18) {
        if (hasID) {
            cout << "Access granted" << endl;
        } else {
            cout << "ID required" << endl;
        }
    } else {
        cout << "Too young" << endl;
    }

    return 0;
}

switch Statement

switch is cleaner than a long if-else chain when testing a single variable against multiple constant values. Always include break to prevent fall-through, and default for unmatched cases.

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;  // � runs
        case 4: cout << "Thursday"  << endl; break;
        case 5: cout << "Friday"    << endl; break;
        case 6: cout << "Saturday"  << endl; break;
        case 7: cout << "Sunday"    << endl; break;
        default: cout << "Invalid day" << endl;
    }

    // Fall-through (intentional - no break)
    char grade = 'B';
    switch (grade) {
        case 'A':
        case 'B':
        case 'C':
            cout << "Passed" << endl;  // runs for A, B, or C
            break;
        case 'F':
            cout << "Failed" << endl;
            break;
        default:
            cout << "Unknown grade" << endl;
    }

    return 0;
}

Ternary Operator

Ternary and if constexpr (C++17)
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;

    // Ternary: condition ? value_if_true : value_if_false
    int max = (a > b) ? a : b;
    cout << "Max: " << max << endl;  // 20

    // Nested ternary (use sparingly - hurts readability)
    int x = 0;
    string sign = (x > 0) ? "positive" : (x < 0) ? "negative" : "zero";
    cout << x << " is " << sign << endl;  // 0 is zero

    // C++17: if with initializer
    if (int result = a + b; result > 25) {
        cout << "Sum " << result << " is greater than 25" << endl;
    }

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.