Skip to main content
Course/Module 1/Topic 4 of 4Beginner

Conditionals (if/else)

Make decisions in C++ using if, else if, else, and switch statements.

25 minBy Priygop TeamLast updated: Feb 2026

C++ Conditionals

C++ conditionals work the same as C but with the added benefit of the bool type. The if statement evaluates a condition and runs code if true. else if checks additional conditions. else runs if all conditions are false. The switch statement is efficient for checking one variable against multiple constant values.

Conditional Syntax

  • if (condition) { } — Runs if true
  • else if (condition) { } — Another condition
  • else { } — Runs if all conditions false
  • switch (var) { case val: ... break; } — Multiple cases
  • Ternary: condition ? valueIfTrue : valueIfFalse
  • Conditions return bool: true or false

Conditionals Example

Example
#include <iostream>
using namespace std;

int main() {
    int age = 18;

    if (age >= 18) {
        cout << "You are an adult." << endl;
    } else {
        cout << "You are a minor." << endl;
    }

    // Grade checker
    int score = 75;
    if (score >= 90) {
        cout << "Grade: A" << endl;
    } else if (score >= 80) {
        cout << "Grade: B" << endl;
    } else if (score >= 70) {
        cout << "Grade: C" << endl;
    } else {
        cout << "Grade: F" << endl;
    }

    // Ternary operator
    string label = (age >= 18) ? "Adult" : "Minor";
    cout << "Label: " << label << endl;

    // Switch
    int day = 3;
    switch (day) {
        case 1: cout << "Monday" << endl; break;
        case 2: cout << "Tuesday" << endl; break;
        case 3: cout << "Wednesday" << endl; break;
        default: cout << "Other day" << endl;
    }

    return 0;
}

Try It Yourself: Grade Calculator

Try It Yourself: Grade CalculatorC++
C++ Editor
✓ ValidTab = 2 spaces
C++|30 lines|695 chars|✓ Valid syntax
UTF-8

Quick Quiz

Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep