Introduction

Conditional statements allow a program to make decisions based on certain conditions. In real life, we make decisions every day:

  • “If it’s raining, take an umbrella.”
  • “If my phone battery is low, charge it.”

Similarly, in C++, conditional statements control the flow of execution based on conditions.

Types of Conditional Statements

  1. if statement
  2. if-else statement
  3. if-else-if ladder
  4. nested if statements
  5. switch statement
  6. ternary operator

1. The if Statement

The if statement executes a block of code only if a specified condition is true.

Example:

#include <iostream>
using namespace std;

int main() {
    int temperature;
    cout << "Enter the temperature: ";
    cin >> temperature;

    if (temperature > 30) {
        cout << "It's a hot day. Stay hydrated!" << endl;
    }
    return 0;
}

Explanation:

  • If temperature is greater than 30, the message is displayed.
  • Otherwise, nothing happens.

Real-Life Analogy:

Imagine checking the weather before going outside:

  • Condition: If it’s sunny (temperature > 30), wear sunglasses.
  • Code Execution: Only applies when the condition is true.

2. The if-else Statement

An if-else statement executes one block of code if the condition is true, and another block if it is false.

Example:

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;

    if (age >= 18) {
        cout << "You are eligible to vote." << endl;
    } else {
        cout << "You are not eligible to vote yet." << endl;
    }
    return 0;
}

Real-Life Analogy:

  • Condition: If you’re 18 or older, you can vote.
  • Else: You must wait until you’re eligible.

3. The if-else-if Ladder

Used when there are multiple conditions to check.

Example:

#include <iostream>
using namespace std;

int main() {
    int marks;
    cout << "Enter your marks: ";
    cin >> marks;

    if (marks >= 90) {
        cout << "Grade: A+" << endl;
    } else if (marks >= 80) {
        cout << "Grade: A" << endl;
    } else if (marks >= 70) {
        cout << "Grade: B" << endl;
    } else {
        cout << "Grade: C" << endl;
    }
    return 0;
}

Explanation:

  • If marks is 90 or more, print A+.
  • If marks is 80-89, print A.
  • If marks is 70-79, print B.
  • Otherwise, print C.

Real-Life Analogy:

  • Schools assign grades based on scores.
  • Each range maps to a different grade.

Example: Traffic Light System 🚦

#include <iostream>
using namespace std;

int main() {
    string signal;
    cout << "Enter the traffic light color (red/yellow/green): ";
    cin >> signal;

    if (signal == "red") {
        cout << "Stop!" << endl;
    } else if (signal == "yellow") {
        cout << "Slow down!" << endl;
    } else if (signal == "green") {
        cout << "Go!" << endl;
    } else {
        cout << "Invalid input. Please enter red, yellow, or green." << endl;
    }
    return 0;
}

Explanation:

  • The program asks for a traffic light color.
  • if checks if it’s red → outputs “Stop!”.
  • else if checks if it’s yellow → outputs “Slow down!”.
  • else if checks if it’s green → outputs “Go!”.
  • else handles incorrect inputs.

4. Nested if Statements

One if inside another. Used when conditions depend on previous ones.

Example:

#include <iostream>
using namespace std;

int main() {
    int year;
    cout << "Enter a year: ";
    cin >> year;

    if (year % 4 == 0) {
        if (year % 100 != 0 || year % 400 == 0) {
            cout << year << " is a leap year." << endl;
        } else {
            cout << year << " is not a leap year." << endl;
        }
    } else {
        cout << year << " is not a leap year." << endl;
    }
    return 0;
}

Real-Life Analogy:

  • A store offers discounts only if:
    • You’re a member (checked first).
    • If premium member, you get extra discount.

5. The switch Statement

switch is used when multiple values need to be compared to a single variable.

Example:

#include <iostream>
using namespace std;

int main() {
    int day;
    cout << "Enter a day number (1-7): ";
    cin >> day;

    switch (day) {
        case 1: cout << "Sunday" << endl; break;
        case 2: cout << "Monday" << endl; break;
        case 3: cout << "Tuesday" << endl; break;
        case 4: cout << "Wednesday" << endl; break;
        case 5: cout << "Thursday" << endl; break;
        case 6: cout << "Friday" << endl; break;
        case 7: cout << "Saturday" << endl; break;
        default: cout << "Invalid day!" << endl;
    }
    return 0;
}

Example: Restaurant Menu 🍽️

#include <iostream>
using namespace std;

int main() {
    int choice;
    cout << "Choose an item:\n1. Pizza\n2. Burger\n3. Pasta\n";
    cin >> choice;

    switch (choice) {
        case 1:
            cout << "You chose Pizza." << endl;
            break;
        case 2:
            cout << "You chose Burger." << endl;
            break;
        case 3:
            cout << "You chose Pasta." << endl;
            break;
        default:
            cout << "Invalid choice!" << endl;
    }
    return 0;
}

Explanation:

  • The program presents a menu with options.
  • switch evaluates choice and executes the corresponding case block.
  • break prevents the execution of the next case.
  • default handles invalid inputs.

When to Use switch?

  • When comparing a single variable against multiple constant values.
  • When if-else makes the code lengthy.

6. The Ternary Operator

The ternary operator (? :) is a shorthand for if-else.

Example:

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    string result = (number % 2 == 0) ? "Even" : "Odd";
    cout << "The number is " << result << endl;

    return 0;
}

Real-Life Analogy:

  • Instead of writing:
    if (age >= 18) {
        cout << "Adult";
    } else {
        cout << "Minor";
    }
    
  • You can write: cout << (age >= 18 ? "Adult" : "Minor");

When to Use Which?

ScenarioUse
Checking a single conditionif
Two possibilities (true/false)if-else
Multiple conditionsif-else-if
Nested conditionsnested if
Checking one value against many casesswitch
Simple if-else in one lineternary operator

Conclusion

Conditional statements allow programs to make decisions. We covered:

  • if statements for basic conditions.
  • if-else for two outcomes.
  • if-else-if for multiple choices.
  • nested if for dependent conditions.
  • switch for multiple exact matches.
  • Ternary operator for concise conditions.

Mastering these is crucial for writing interactive and intelligent C++ programs!

Up Next: Loops in C++