C++ PROGRAMMING · 8 MIN STUDY

Making choices with if and switch

A conditional executes one path when its condition is true and another when it is false.

Learning objectives

  • Choose between independent if statements and an if/else if/else chain.
  • Use switch for a set of discrete integral or enumeration values.
  • Trace a decision and predict which statements execute.

Prerequisites: variables, input/output, comparison operators, and Boolean values. Review Comparison and logical operators first.

How a condition controls a branch

An if statement evaluates its condition. If the result is true, its controlled statement runs; otherwise it is skipped. In an if/else if/else chain, conditions are checked in order and at most one branch is selected. Use braces to make each branch boundary clear.

Complete C++17 program — if / else if / else
#include <iostream>

int main() {
    int marks{72};

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

Trace the decision

CheckResult for 72Next action
marks >= 90falseCheck the next condition
marks >= 40truePrint Pass; skip later branches

Conditions are contextual Boolean expressions: in C++, a scalar value can be contextually converted to bool (zero is false; nonzero is true). Prefer an explicit comparison when it communicates the rule more clearly.

When switch is a better fit

Use switch when one integral or enumeration expression is compared against several constant case labels. It is not a general replacement for ranges such as “marks at least 40”. A break exits the switch; without it, execution can fall through into the next case. A default label handles values not matched by another case.

Complete C++17 program — switch
#include <iostream>

int main() {
    int choice{2};

    switch (choice) {
        case 1:
            std::cout << "Start\n";
            break;
        case 2:
            std::cout << "Settings\n";
            break;
        case 3:
            std::cout << "Quit\n";
            break;
        default:
            std::cout << "Unknown option\n";
            break;
    }
}
Expected output Settings

Common mistakes

  • Use == for equality; = assigns a value.
  • Order range checks from the most specific/highest threshold when later branches depend on earlier ones.
  • Remember that a missing break can cause switch fall-through.
  • Do not expect switch to accept arbitrary run-time ranges or strings as case labels in C++17.

Practice

  1. For marks = 39, which branch runs in the first program?
  2. For choice = 7, what does the switch program print?
  3. Why is an if/else if chain more suitable than switch for classifying a numeric range?
  4. What happens if the break after case 2 is removed?

Worked answers

Show reasoned answers
  1. The final else prints Try again.
  2. The default branch prints Unknown option.
  3. Range conditions such as marks >= 40 are Boolean expressions, not constant case labels.
  4. Execution falls through to the next case body unless another control transfer exits the switch.

Summary

Use if for Boolean conditions and ranges; use switch for discrete integral or enumeration choices. Trace conditions in order and make branch boundaries explicit with braces and deliberate break statements.

References