Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 07 Beginner

Conditional Statements in C++

Updated: May 17, 2026
5 min read

# CHAPTER 7

Conditional Statements

1. Introduction

Programs need to make decisions. Should a user be granted access? Is the player's health zero? Conditional statements allow a C++ program to choose different paths of execution based on specific conditions, creating intelligent software.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the if statement to run code conditionally.
  • Use if-else for two-way branching.
  • Chain conditions using else if ladders.
  • Use switch-case for multi-way branching.
  • Write concise conditions using the Ternary Operator.

3. The if Statement

Executes a block of code ONLY if the condition evaluates to true.
cpp
12345
int age = 20;

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

4. The if-else Statement

Provides an alternative block of code to run if the condition is false.
cpp
1234567
int temperature = 35;

if (temperature > 30) {
    cout << "It&#039;s hot outside!" << endl;
} else {
    cout << "The weather is pleasant." << endl;
}

5. The else if Ladder

Used to check multiple conditions in sequence. As soon as one condition is true, its block executes, and the rest are skipped.
cpp
123456789101112
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;
}
// Output: Grade: C

6. Nested if Statements

Placing an if statement inside another if statement.
cpp
123456789101112
int age = 25;
bool has_license = true;

if (age >= 18) {
    if (has_license) {
        cout << "You can drive." << endl;
    } else {
        cout << "You need a license first." << endl;
    }
} else {
    cout << "You are too young to drive." << endl;
}

7. The switch-case Statement

The switch statement is a cleaner alternative to a long else if ladder when comparing a single variable against exact values.
cpp
123456789101112131415
int day = 3;

switch (day) {
    case 1:
        cout << "Monday" << endl;
        break; // Exits the switch block
    case 2:
        cout << "Tuesday" << endl;
        break;
    case 3:
        cout << "Wednesday" << endl;
        break;
    default: // Runs if no cases match
        cout << "Weekend!" << endl;
}

Important: Without the break keyword, execution "falls through" and runs all subsequent cases!

8. Mini Project: Grade Evaluation System

cpp
1234567891011121314151617181920212223242526
#include <iostream>
using namespace std;

int main() {
    int marks;

    cout << "=== GRADE CALCULATOR ===" << endl;
    cout << "Enter your marks (0-100): ";
    cin >> marks;

    if (marks < 0 || marks > 100) {
        cout << "Error: Invalid marks entered." << endl;
    } else if (marks >= 90) {
        cout << "Grade: A+ (Outstanding)" << endl;
    } else if (marks >= 80) {
        cout << "Grade: A (Excellent)" << endl;
    } else if (marks >= 70) {
        cout << "Grade: B (Good)" << endl;
    } else if (marks >= 60) {
        cout << "Grade: C (Pass)" << endl;
    } else {
        cout << "Grade: F (Fail)" << endl;
    }

    return 0;
}

9. Memory-Level Explanation

At the assembly level, an if statement compiles into a "Compare" (CMP) instruction followed by a "Jump" (JMP) instruction. If the comparison fails, the CPU alters the Instruction Pointer (IP) register to skip over the code block entirely.

10. Common Mistakes

  • Using = instead of ==:
if (x = 5) assigns 5 to x, which evaluates to true. This is a massive logic bug. It must be if (x == 5).
  • Missing break in switch: This causes fall-through, executing unintended code.
  • Strings in switch: switch only works with integers, characters, or enums. You CANNOT switch on a std::string in C++.

11. Exercises

  1. 1. Write a program to check if a given year is a leap year.
  1. 2. Build a simple calculator using switch-case that asks the user for an operator (+, -, *, /) and two numbers.
  1. 3. Use the ternary operator to find the maximum of two numbers.

12. MCQ Quiz with Answers

Question 1

What happens if an if condition evaluates to false?

Question 2

Which keyword handles the "catch-all" scenario in a switch statement?

Question 3

What is the output of if (5 = 5) { cout << "Yes"; }?

Q4. Can you use a std::string variable in a switch expression? a) Yes b) No Answer: b) No (Only integral types like int, char, enum)
Question 5

What prevents "fall-through" in a switch case?

Question 6

What does (x > y) ? x : y; do?

Q7. Is else mandatory after an if? a) Yes b) No Answer: b) No
Question 8

Which operator is used to compare two values for equality?

Q9. Can if statements be nested inside other if statements? a) Yes b) No Answer: a) Yes
Question 10

In C++, what integer value usually represents 'true'?

13. Interview Questions

  • Q: Explain the difference between if-else if ladders and switch statements. Which is faster? (Hint: Jump tables).
  • Q: What is the dangling else problem?
  • Q: Why is it considered dangerous to use if (a = b)?

14. Summary

Conditional statements give your program decision-making power. if, else if, and else provide flexible logic evaluation. switch-case offers a cleaner syntax for matching exact integers or characters. The ternary operator is a concise shortcut for simple binary decisions.

15. Next Chapter Recommendation

In Chapter 8: Loops in C++, we will learn how to repeat actions automatically using for, while, and do-while loops.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·