What is Conditional Statement?
Conditional statements allow us to execute specific code blocks based on certain conditions. They are essential in programming for decision-making.
Real-Life Example of Conditional Statements
Imagine you're deciding what to wear based on the weather. If it's raining, you wear a raincoat; otherwise, you wear normal clothes. Now, let's add more conditions:
- If it's **cold**, you wear a **jacket**.
- If it's **hot**, you wear a **T-shirt and shorts**.
- If it's **snowing**, you wear a **heavy coat, gloves, and boots**.
- If the **weather is unpredictable**, you carry an **umbrella** just in case.
This is how conditional statements work in programming. Based on different conditions, different actions (or outputs) are chosen. Just like you adjust your outfit according to the weather, a program executes different code based on given conditions.
If Statement
The if statement executes a block of code only if a specified condition is true.
let temperature = 30;
if (temperature > 25) {
console.log("It's hot outside.");
}
In this example, if the temperature is greater than 25, it will log "It's hot outside." Otherwise, nothing happens.
Else Statement
The else statement executes a block of code when the if condition is false.
let temperature = 15;
if (temperature > 25) {
console.log("It's hot outside.");
} else {
console.log("It's cool outside.");
}
Here, since the temperature is **15**, which is not greater than 25, the **else block** executes, logging "It's cool outside."
Else-If Statement
The else if statement allows us to check multiple conditions one after another.
let temperature = 10;
if (temperature > 25) {
console.log("It's hot outside.");
} else if (temperature > 15) {
console.log("It's warm outside.");
} else {
console.log("It's cold outside.");
}
In this case:
- If
temperature > 25, it logs **"It's hot outside."** - If
temperature > 15, it logs **"It's warm outside."** - Otherwise, it logs **"It's cold outside."**
Nested Else-If Statement
A nested **else-if** allows checking for more complex conditions inside another **if-else** block.
let weather = "rainy";
let temperature = 20;
if (temperature > 25) {
if (weather === "sunny") {
console.log("Wear light clothes.");
} else {
console.log("Carry an umbrella.");
}
} else {
if (weather === "snowy") {
console.log("Wear a heavy coat.");
} else {
console.log("Wear a jacket.");
}
}
This example checks both **temperature** and **weather** to decide the most suitable clothing.
Switch Statement
The **switch** statement is useful when checking multiple values of a single variable.
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the workweek.");
break;
case "Friday":
console.log("Weekend is near!");
break;
case "Sunday":
console.log("It's a relaxing day.");
break;
default:
console.log("It's a regular day.");
}
Each case checks for a specific value, and the **break** prevents fall-through to the next case. The **default** case handles unmatched values.
Difference Between If-Else and Switch (Comparison Table)
| Aspect | If-Else | Switch |
|---|---|---|
| Used for | Evaluating conditions and expressions (e.g., comparisons, logical checks) | Matching specific values of a single variable |
| Readability | Less readable with many nested conditions | More readable for multiple exact match cases |
| Performance | Can be slower when evaluating many expressions | Can be faster due to optimized lookup (for known values) |
| Use Case | Best for complex logic and range-based checks | Best for multiple discrete value comparisons |
| Condition Type | Supports all kinds of expressions (e.g., `x > 10`, `a && b`) | Only supports strict equality checks (`===`) |
| Fall-through Behavior | Executes only one matching block | Requires break to avoid falling into the next case |
Best Practices
- Use **if-else** when conditions involve comparisons (
>, <, ==) or logical operators. - Use **switch** when you need to check a variable against multiple known values.
- Always **use break statements** in a switch to avoid unintended fall-through.
- Avoid deeply nested **if-else** statements by refactoring into functions.
- Use **ternary operators** (
condition ? value1 : value2) for short, simple conditions.
Pros & Cons of Conditional Statements
| Conditional Type | Pros | Cons |
|---|---|---|
| If-Else | Flexible, can handle any condition | Can become complex and harder to read |
| Switch | Faster and more readable for checking fixed values | Only works with discrete values, not ranges |
| Ternary Operator | Concise for simple conditions | Harder to read when nested |
Final Thought
Conditional statements are fundamental in programming, helping us **control the flow of execution**. Knowing when to use **if-else, switch, and ternary operators** improves both performance and code readability.