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
ifstatement to run code conditionally.
-
Use
if-elsefor two-way branching.
-
Chain conditions using
else ifladders.
-
Use
switch-casefor 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
4. The if-else Statement
Provides an alternative block of code to run if the condition is false.
cpp
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
6. Nested if Statements
Placing an if statement inside another if statement.
cpp
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
Important: Without the break keyword, execution "falls through" and runs all subsequent cases!
8. Mini Project: Grade Evaluation System
cpp
9. Memory-Level Explanation
At the assembly level, anif 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
breakin switch: This causes fall-through, executing unintended code.
-
Strings in switch:
switchonly works with integers, characters, or enums. You CANNOT switch on astd::stringin C++.
11. Exercises
- 1. Write a program to check if a given year is a leap year.
-
2.
Build a simple calculator using
switch-casethat asks the user for an operator (+,-,*,/) and two numbers.
- 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"; }?
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?
else mandatory after an if?
a) Yes b) No
Answer: b) No
Question 8
Which operator is used to compare two values for equality?
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 ifladders andswitchstatements. 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 usingfor, while, and do-while loops.