Curriculum / C++ / Lesson 06

Conditions (if / else) in C++

C++ conditional statements work exactly like C's, with the bonus that conditions now naturally evaluate to a real bool instead of just an integer convention..

6 of 10
beginner 📖 5 min read 🎯 1 question

Conditions

C++ conditional statements work exactly like C's, with the bonus that conditions now naturally evaluate to a real bool instead of just an integer convention.

💡 Key insight Because C++ has real booleans, comparison results read more clearly in code and in the debugger.

Syntax

syntax
if (condition) {
    // code
} else if (condition2) {
    // code
} else {
    // code
}

Example

Only the first matching branch runs; the rest are skipped.

cpp
#include <iostream>
using namespace std;

int main() {
    int marks = 72;
    if (marks >= 90) {
        cout << "Grade: A" << endl;
    } else if (marks >= 60) {
        cout << "Grade: B" << endl;
    } else {
        cout << "Grade: C" << endl;
    }
    return 0;
}
outputGrade: B

Try it yourself

Copy this example into the C++ compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What must you add at the end of each switch case to avoid falling into the next one?

Back to curriculum