Paramclasses Academy - Blog

Tutorials Details

What is Decision-Making Statements in C++

What is Decision-Making Statements in C++

October 15, 2026

4.1 Module overview

Decision-making statements allow a C++ program to choose which instructions to execute based on a condition.

For example, a program can check whether a student has passed an examination, whether a number is positive or negative, or which menu option a user selected.

In this module, we will learn how to control program flow using if, if-else, nested conditions, else-if ladders and switch statements.

Learning objectives

By the end of this module, students will be able to:

  • Understand conditions and Boolean expressions.

  • Use the if statement to execute code conditionally.

  • Apply if-else to choose between alternatives.

  • Use nested if statements and else-if ladders.

  • Implement menu-based programs using switch.

  • Use logical operators in decision-making.

  • Understand break, default and common conditional errors.

  • Build practical programs using decision-making statements.

Prerequisites: Modules 1–3, including variables, data types, operators and expressions.

Estimated learning time: 3–4 hours, including practical exercises.

4.2 What is decision-making in C++?

Decision-making is the process of selecting a path of execution according to whether a condition is satisfied.

For example:

  • If marks are at least 40, display “Pass.”

  • Otherwise, display “Fail.”

The condition evaluates to a Boolean result: true or false.

Start program

Check condition

Is marks greater than or equal to 40?

True

Display “Pass”

False

Display “Fail”

End program

4.3 The if statement

The if statement executes a block of code only when its condition evaluates to true.

Syntax

if (condition) {
    // Code executes when condition is true
}

Example: Check voting age

#include <iostream>

int main() {
    int age = 20;

    if (age >= 18) {
        std::cout << "You are eligible to vote.\n";
    }

    return 0;
}

Output

You are eligible to vote.

Explanation

  1. The variable age stores 20.

  2. The expression age >= 18 evaluates to true.

  3. The code inside the if block executes.

  4. The message is displayed.

If age were 16, the condition would be false, and the message would not be displayed.

Important: The comparison operator >= means “greater than or equal to.” It is different from the assignment operator =.

4.4 The if-else statement

The if-else statement provides two alternative paths. One executes when the condition is true; the other executes when it is false.

Syntax

if (condition) {
    // True branch
} else {
    // False branch
}

Example: Pass or fail

#include <iostream>

int main() {
    int marks = 35;

    if (marks >= 40) {
        std::cout << "Result: Pass\n";
    } else {
        std::cout << "Result: Fail\n";
    }

    return 0;
}

Output

Result: Fail

Because 35 >= 40 is false, the else block executes.

Example with user input

#include <iostream>

int main() {
    int number;

    std::cout << "Enter a number: ";
    if (!(std::cin >> number)) {
        std::cout << "Invalid input\n";
        return 1;
    }

    if (number % 2 == 0) {
        std::cout << "Even number\n";
    } else {
        std::cout << "Odd number\n";
    }

    return 0;
}

Example run

Enter a number: 7
Odd number

The remainder operator % determines whether the number is divisible by two. A remainder of zero means the integer is even.

4.5 The else-if ladder

An else-if ladder is used when a program needs to test several conditions in sequence.

The conditions are evaluated from top to bottom. The first condition that evaluates to true has its block executed, and the remaining branches are skipped.

Syntax

if (condition1) {
    // First condition is true
} else if (condition2) {
    // Second condition is true
} else if (condition3) {
    // Third condition is true
} else {
    // No condition is true
}

Example: Grade calculator

For this example, assume that marks are between 0 and 100 and the grading thresholds are:

Marks

Grade

90–100

A

80–89

B

70–79

C

60–69

D

0–59

F

#include <iostream>

int main() {
    int marks;

    std::cout << "Enter marks (0-100): ";

    if (!(std::cin >> marks)) {
        std::cout << "Invalid input\n";
        return 1;
    }

    if (marks < 0 || marks > 100) {
        std::cout << "Marks must be between 0 and 100.\n";
    } else if (marks >= 90) {
        std::cout << "Grade: A\n";
    } else if (marks >= 80) {
        std::cout << "Grade: B\n";
    } else if (marks >= 70) {
        std::cout << "Grade: C\n";
    } else if (marks >= 60) {
        std::cout << "Grade: D\n";
    } else {
        std::cout << "Grade: F\n";
    }

    return 0;
}

Example run

Enter marks (0-100): 85
Grade: B

How the program works

  1. The program reads the student's marks.

  2. It checks whether the marks are within the valid range.

  3. If the marks are valid, it checks the grade thresholds in descending order.

  4. The first matching grade is displayed.

  5. If none of the higher thresholds match, the grade is F.

Best practice: Check invalid input and out-of-range values before checking normal grade conditions.

4.6 Nested if statements

A nested if statement is an if statement placed inside another if or else block.

Nested conditions are useful when one decision depends on another decision.

Syntax

if (condition1) {
    if (condition2) {
        // Both conditions are true
    }
}

Example: Login validation

#include <iostream>

int main() {
    bool usernameCorrect = true;
    bool passwordCorrect = false;

    if (usernameCorrect) {
        if (passwordCorrect) {
            std::cout << "Login successful\n";
        } else {
            std::cout << "Incorrect password\n";
        }
    } else {
        std::cout << "Unknown username\n";
    }

    return 0;
}

Output

Incorrect password

The username condition is true, so the inner if statement executes. The password condition is false, so the inner else block displays the error message.

This example illustrates control flow only; real authentication systems must verify credentials securely rather than relying on Boolean variables.

Nested if vs logical operators

Sometimes, nested conditions can be simplified with logical operators.

Nested version:

if (age >= 18) {
    if (hasId) {
        std::cout << "Access allowed\n";
    }
}

Combined version:

if (age >= 18 && hasId) {
    std::cout << "Access allowed\n";
}

Both versions require the age condition and the hasId condition to be true.

Use nesting when separate branches or additional steps are needed. Use && for a straightforward combined condition.

4.7 The switch statement

The switch statement selects a branch based on the value of an expression. It is particularly useful for menu-based programs and fixed sets of discrete choices.

Syntax

switch (expression) {
    case value1:
        // Statements
        break;

    case value2:
        // Statements
        break;

    default:
        // No matching case
}

The expression must have an appropriate integral or enumeration type, subject to the language's rules. Ordinary switch statements do not directly switch on strings.

Example: Day of the week

#include <iostream>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            std::cout << "Monday\n";
            break;

        case 2:
            std::cout << "Tuesday\n";
            break;

        case 3:
            std::cout << "Wednesday\n";
            break;

        case 4:
            std::cout << "Thursday\n";
            break;

        case 5:
            std::cout << "Friday\n";
            break;

        default:
            std::cout << "Invalid day number\n";
    }

    return 0;
}

Output

Wednesday

Understanding break

The break statement exits the switch statement. Without it, execution can continue into the following case labels, a behaviour called fallthrough.

For example:

switch (choice) {
    case 1:
        std::cout << "One\n";
        break;

    case 2:
        std::cout << "Two\n";
        break;

    default:
        std::cout << "Other\n";
}

Each branch ends with break, so only the selected branch executes.

Understanding default

The default branch runs when no case label matches. It is optional, but useful for handling unexpected menu choices.

4.8 Difference between if-else and switch

| Feature | if-else | switch | |---|---|---| | Conditions | Supports complex Boolean expressions | Selects among matching case values | | Ranges | Supports ranges such as marks >= 90 | Does not directly support range cases | | Multiple logical conditions | Supports &&, || and other expressions | Not intended for general Boolean-condition chains | | Strings | Can compare strings using suitable expressions | Cannot directly switch on std::string | | Best use | Ranges, eligibility checks and complex logic | Menus and discrete integral or enum choices |

Rule of thumb: Use if-else for ranges and complex conditions. Use switch for a fixed set of discrete choices.

4.9 Combining conditions with logical operators

Decision-making becomes more powerful when conditions are combined using &&, || and !.

| Operator | Meaning | Example | |---|---|---| | && | Both conditions must be true | age >= 18 && hasId | | || | At least one condition must be true | isAdmin || isOwner | | ! | Reverses a Boolean condition | !isBlocked |

Example: Scholarship eligibility

Suppose a scholarship requires marks of at least 85 and attendance of at least 90%.

#include <iostream>

int main() {
    int marks = 90;
    int attendance = 95;

    if (marks >= 85 && attendance >= 90) {
        std::cout << "Eligible for scholarship\n";
    } else {
        std::cout << "Not eligible for scholarship\n";
    }

    return 0;
}

Output

Eligible for scholarship

Both conditions are true, so the student meets the example criteria.

4.10 Common mistakes in decision-making

Mistake

Explanation

Correction

Using = instead of ==

Assignment is not equality comparison.

Use == when checking equality.

Incorrect condition order

A broader condition can prevent a later branch from being reached.

Check specific or higher-priority cases first.

Missing braces

Statements may execute outside the intended branch.

Use braces consistently for multi-line blocks.

Forgetting break in switch

Execution can fall through into subsequent cases.

Add break unless fallthrough is intentional.

Ignoring invalid input

The program may process unusable data.

Check whether input operations succeed.

Overusing nested conditions

Deep nesting makes code harder to maintain.

Simplify with logical operators where appropriate.

Example: Incorrect condition ordering

Incorrect:

if (marks >= 40) {
    std::cout << "Pass\n";
} else if (marks >= 90) {
    std::cout << "Excellent\n";
}

The marks >= 90 branch can never execute because every value greater than or equal to 90 already satisfies marks >= 40.

Correct:

if (marks >= 90) {
    std::cout << "Excellent\n";
} else if (marks >= 40) {
    std::cout << "Pass\n";
} else {
    std::cout << "Fail\n";
}

The corrected order checks the higher threshold first.

4.11 Practical project: ATM menu system

This project combines switch, if-else, arithmetic and user input to create a simple ATM menu.

Source code

#include <iostream>
#include <iomanip>

int main() {
    double balance = 5000.00;
    int choice;
    double amount;

    std::cout << "===== ATM MENU =====\n";
    std::cout << "1. Check Balance\n";
    std::cout << "2. Deposit\n";
    std::cout << "3. Withdraw\n";
    std::cout << "4. Exit\n";
    std::cout << "Enter your choice: ";

    if (!(std::cin >> choice)) {
        std::cout << "Invalid input\n";
        return 1;
    }

    switch (choice) {
        case 1:
            std::cout << "Balance: Rs. "
                      << std::fixed << std::setprecision(2)
                      << balance << '\n';
            break;

        case 2:
            std::cout << "Enter deposit amount: ";
            if (!(std::cin >> amount)) {
                std::cout << "Invalid amount\n";
                return 1;
            }

            if (amount > 0) {
                balance += amount;
                std::cout << "Deposit successful\n";
                std::cout << "New balance: Rs. "
                          << balance << '\n';
            } else {
                std::cout << "Amount must be positive\n";
            }
            break;

        case 3:
            std::cout << "Enter withdrawal amount: ";
            if (!(std::cin >> amount)) {
                std::cout << "Invalid amount\n";
                return 1;
            }

            if (amount <= 0) {
                std::cout << "Amount must be positive\n";
            } else if (amount > balance) {
                std::cout << "Insufficient balance\n";
            } else {
                balance -= amount;
                std::cout << "Withdrawal successful\n";
                std::cout << "New balance: Rs. "
                          << balance << '\n';
            }
            break;

        case 4:
            std::cout << "Thank you for using our ATM\n";
            break;

        default:
            std::cout << "Invalid menu choice\n";
    }

    return 0;
}

Example run

===== ATM MENU =====
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your choice: 3
Enter withdrawal amount: 1000
Withdrawal successful
New balance: Rs. 4000.00

Project explanation

  • switch selects the requested ATM operation.

  • if-else validates deposit and withdrawal amounts.

  • balance += amount adds a deposit.

  • balance -= amount subtracts a withdrawal.

  • default handles an invalid menu choice.

  • std::fixed and std::setprecision(2) format the balance to two decimal places.

This is a teaching example, not a production banking system. The program processes one operation per run; a complete ATM simulation would typically use loops, persistent account data and stronger validation.

4.12 Hands-on exercises

Complete the following tasks before checking your knowledge.

Exercise 1 — Pass or fail

Accept marks from the user. Validate that the marks are between 0 and 100, then display Pass for marks of 40 or more and Fail otherwise.

Exercise 2 — Largest of three numbers

Accept three numbers and use conditional statements to display the largest number. Also consider the possibility of equal values.

Exercise 3 — Leap year

Write a program to determine whether a year is a leap year. A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100.

Exercise 4 — Menu-driven calculator

Use switch to perform addition, subtraction, multiplication or division based on the user's selected operator. Handle division by zero.

Exercise 5 — Login decision

Accept a username and password, then implement a basic demonstration of login validation using conditional statements. Do not use real credentials or store passwords in plain text in a real application.

4.13 Module summary

In this module, you learned:

  • How decision-making controls program execution.

  • The syntax and behaviour of if and if-else.

  • How to implement multiple conditions using an else-if ladder.

  • How nested if statements work.

  • How to select branches with switch, case, break and default.

  • How to combine conditions using logical operators.

  • How to prevent common errors in conditional logic.

  • How to build a practical menu-driven program.

Module completion criteria: Students should be able to select an appropriate decision-making statement, write valid conditional expressions, validate user input and implement a small decision-driven C++ application.

4.14 Knowledge-check quiz

Module 4 quiz

Choose one answer for each question, then check your score.

 

1. Which statement executes a block only when its condition is true?

switch only

if

return

break

2. What is the purpose of break in a typical switch case?

Restart the program

Declare a variable

Exit the switch statement

Compare two values

3. Which operator means logical AND?

&

||

!

&&

4. What happens when no case matches and a switch has a default branch?

The default branch executes

The first case always executes

The program automatically restarts

All cases are skipped without executing default

5. Which condition correctly checks that marks are from 0 through 100, inclusive?

marks < 0 && marks > 100

marks >= 0 && marks <= 100

marks > 0 || marks < 100

marks == 0 && marks == 100