Conditional Statements in C++
Learn about conditional statements in C++, including if, if-else, if-else-if ladder, nested if, switch, and ternary operator.
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
- if statement
- if-else statement
- if-else-if ladder
- nested if statements
- switch statement
- ternary operator
1. The if
Statement
The if
statement executes a block of code only if a specified condition is true
.
Example:
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:
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:
Explanation:
- If
marks
is 90 or more, printA+
. - If
marks
is 80-89, printA
. - If
marks
is 70-79, printB
. - Otherwise, print
C
.
Real-Life Analogy:
- Schools assign grades based on scores.
- Each range maps to a different grade.
Example: Traffic Light System 🚦
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:
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:
Example: Restaurant Menu 🍽️
Explanation:
- The program presents a menu with options.
switch
evaluateschoice
and executes the correspondingcase
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:
Real-Life Analogy:
- Instead of writing:
- You can write:
cout << (age >= 18 ? "Adult" : "Minor");
When to Use Which?
Scenario | Use |
---|---|
Checking a single condition | if |
Two possibilities (true/false) | if-else |
Multiple conditions | if-else-if |
Nested conditions | nested if |
Checking one value against many cases | switch |
Simple if-else in one line | ternary 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!