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.”
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
temperatureis 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
marksis 90 or more, printA+. - If
marksis 80-89, printA. - If
marksis 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.
ifchecks if it’s red → outputs “Stop!”.else ifchecks if it’s yellow → outputs “Slow down!”.else ifchecks if it’s green → outputs “Go!”.elsehandles 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.
switchevaluateschoiceand executes the correspondingcaseblock.breakprevents the execution of the next case.defaulthandles invalid inputs.
When to Use switch?
- When comparing a single variable against multiple constant values.
- When
if-elsemakes 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:ifstatements for basic conditions.if-elsefor two outcomes.if-else-iffor multiple choices.nested iffor dependent conditions.switchfor multiple exact matches.- Ternary operator for concise conditions.