Thuta Learning
BasicProgrammingbeginner

If...Else

Relax. We'll talk through this in plain words — no textbook voice.

if...else lets you run different code paths depending on a condition. It's essential for decisions like login success/failure, pass/fail grading, or checking whether something is in stock.

cpp
#include <iostream>
using namespace std;

int main() {
    int mark = 75;

    if (mark >= 80) {
        cout << "Excellent";
    } else if (mark >= 40) {
        cout << "Pass";
    } else {
        cout << "Fail";
    }
    return 0;
}

The program checks conditions from top to bottom. mark >= 80 is false, so it moves to the next condition, and since mark >= 40 is true, it prints Pass.

You should see
Pass

Info

Condition order matters. Checking the widest range first and narrowing down as you go is usually the clearest approach.

Easy traps

  • When writing multiple else if branches, check whether your conditions overlap. For example, if you put mark >= 40 above mark >= 80, the Excellent branch can never be reached.
If...Else | Thuta Learning