Decision Making in C++

In real life, decisions are made based on conditions — for example, "if it is raining, take an umbrella." C++ programs work the same way. Decision-making statements allow the program to choose a course of action based on whether a condition is true or false.

The if Statement

The simplest decision-making statement. The block of code inside executes only when the condition is true.

if (condition) {
    // code runs if condition is true
}
#include <iostream>
using namespace std;

int main() {
    int temperature = 35;

    if (temperature > 30) {
        cout << "It is hot outside." << endl;
    }
    return 0;
}

Output:

It is hot outside.

The if-else Statement

Adds an alternative path when the condition is false.

if (condition) {
    // runs when condition is true
} else {
    // runs when condition is false
}
int marks = 45;

if (marks >= 50) {
    cout << "Passed!" << endl;
} else {
    cout << "Failed. Try again." << endl;
}

Output:

Failed. Try again.

The if-else if-else Chain

Used when there are more than two possible paths. Only the first matching condition runs.

int score = 75;

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

Output:

Grade: B

Nested if Statements

An if inside another if is called nested. Use with care to avoid deeply nested code.

int age = 20;
bool hasTicket = true;

if (age >= 18) {
    if (hasTicket) {
        cout << "Welcome to the event!" << endl;
    } else {
        cout << "No ticket. Entry denied." << endl;
    }
} else {
    cout << "Under age. Entry denied." << endl;
}

Output:

Welcome to the event!

The switch Statement

When a variable needs to be compared against multiple specific values, switch is cleaner than a long chain of if-else if. It matches the expression against each case.

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code when no case matches
}
#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 4: cout << "Thursday"  << endl; break;
        case 5: cout << "Friday"    << endl; break;
        default: cout << "Weekend"  << endl;
    }
    return 0;
}

Output:

Wednesday

The Importance of break in Switch

Without break, execution falls through to the next case — this is called fall-through. This can be a bug or intentional behavior depending on the design.

int x = 2;
switch (x) {
    case 1: cout << "One
";
    case 2: cout << "Two
";   // no break
    case 3: cout << "Three
"; // also runs!
}

Output (due to fall-through):

Two
Three

Switch with Characters

char grade = 'B';

switch (grade) {
    case 'A': cout << "Excellent" << endl; break;
    case 'B': cout << "Good"      << endl; break;
    case 'C': cout << "Average"   << endl; break;
    default:  cout << "Invalid grade" << endl;
}

Output:

Good

Comparison: if-else vs switch

Featureif-elseswitch
Condition typeAny boolean expressionOnly integer or character equality
Range checksSupported (>, <=)Not supported
ReadabilityGood for complex conditionsCleaner for multiple fixed values
PerformanceMay be slower with many branchesOften faster with many cases

Key Takeaways

  • if runs code when a condition is true.
  • if-else provides an alternative path when the condition is false.
  • if-else if-else chains handle multiple conditions in order.
  • switch is ideal for comparing a single variable against multiple fixed values.
  • Always include break in switch cases to prevent fall-through (unless it is intentional).

Leave a Comment

Your email address will not be published. Required fields are marked *