Conditional Statements in 6 Minutes! (if, else, elseif explained)

Опубликовано: 19 Июль 2026
на канале: Raw Learning
27
3

⏱️ Conditional Statements in 6 Minutes
Think of conditional statements as decision-makers in your code. They allow a program to perform different actions based on whether a specific condition is true or false. It's just like how you decide what to wear based on the weather! ☀️🌧️

1. The if Statement
The if statement is the most basic conditional. It runs a block of code only if its condition is met (evaluates to true).

Concept: "If this one thing is true, then do this."

Analogy: If it is raining, you will take an umbrella. If it's not raining, you do nothing with the umbrella. The action only happens if the condition (rain) is true.

Example (Pseudocode):

if (isRaining == true) {
takeUmbrella();
}
In this case, the takeUmbrella() action only occurs if isRaining is true.

2. The if-else Statement
The if-else statement provides an alternative path. It runs one block of code if the condition is true, and a different block of code if the condition is false.

Concept: "If this is true, do this. Otherwise, do that."

Analogy: If the coffee shop is open, you will buy coffee. Else, you will make coffee at home. You are guaranteed to perform one of the two actions.

Example (Pseudocode):

if (coffeeShopIsOpen == true) {
buyCoffee();
} else {
makeCoffeeAtHome();
}
Here, if coffeeShopIsOpen is true, you buyCoffee(). If it's false, the else block runs, and you makeCoffeeAtHome().

3. The else if (or elif) Statement
The else if statement lets you check multiple conditions in a chain. It's used when you have more than two possible outcomes. The code executes the block for the first condition that evaluates to true.

Concept: "If this is true, do this. If not, check the next thing. If that's true, do that. Otherwise, do this last thing."

Analogy: Think of a traffic light.

If the light is red, you stop.

Else if the light is yellow, you slow down.

Else (the only remaining option is green), you go.

Example (Pseudocode):

if (trafficLight == "red") {
stopCar();
} else if (trafficLight == "yellow") {
slowDown();
} else {
go();
}
The program checks each condition in order. As soon as one is true (e.g., trafficLight == "yellow"), it runs that code (slowDown()) and skips the rest of the chain. The final else is a catch-all that runs if none of the preceding if or else if conditions were true.