# Control Flow In JavaScript: If, Else and Switch Explained

Control flow in JavaScript helps decide which part of the code runs and when. It allows us to make decisions, repeat tasks, and handle different situations. let’s understand with different stories.

### **If Statement**

Renil really wants ice cream! He asks his mom for one.

His mom says: "If the ice cream shop arrives, then I will give you ice cream."

Now, Renil is waiting and watching. If the ice cream shop arrives, he gets his treat. Otherwise, nothing happens.

This is just like an `if` statement in JavaScript - it only runs when the condition is true!

In code, it looks like this:

```javascript
let iceCreamShopArrived = true;

if (iceCreamShopArrived) {
    console.log("Here's your ice cream, Renil! 🍦");
}
```

If the ice cream shop arrives, Renil gets ice cream. Otherwise, his mom does nothing—just like an `if` statement!

---

### **If-Else Statement**

One evening, Renil is playing video games when his mom asks: "Did you finish your homework?"

* If Renil has finished his homework, his mom says, "Great! You can keep playing."
    
* Else (if he hasn’t finished), his mom says, "No more games! Finish your homework first."
    

This is exactly how if-else works—it checks a condition and takes different actions based on the result!

In JavaScript, it looks like this:

```javascript
let homeworkDone = false; 

if (homeworkDone) {
    console.log("Great! You can keep playing. 🎮");
} else {
    console.log("No more games! Finish your homework first. 📖");
}
```

So, just like Renil’s mom decides what he can do based on his homework, `if-else` helps a program make decisions!

---

### **Switch Statement**

At a **train ticket counter**, a traveler asks for a ticket. The cashier checks the destination and gives the correct ticket.

* If the traveler says "Delhi", the cashier gives a Delhi ticket.
    
* If the traveler says "Mumbai", the cashier gives a Mumbai ticket.
    
* If the traveler says "Chennai", the cashier gives a Chennai ticket.
    
* If the destination is not listed, the cashier says, "Sorry, no tickets available for that destination."
    

This is exactly how a `switch-case` works in JavaScript—it checks different options and runs the matching one!

```javascript
let destination = "Delhi";

switch (destination) {
    case "Rajkot":
        console.log("Here's your ticket to Delhi");
        break;
    case "Mumbai":
        console.log("Here's your ticket to Mumbai");
        break;
    case "Bengaluru":
        console.log("Here's your ticket to Chennai");
        break;
    default:
        console.log("Sorry, no tickets available for that destination");
}
```

So, just like the cashier gives different tickets based on the traveler's destination, a `switch-case` runs different code based on a given value! Break statement important in each case.

---

### **Conclusion**

* **if** runs code only when a condition is true, like Renil getting ice cream if the shop arrives.
    
* **if-else** chooses between two actions, like deciding whether Renil can play based on his homework.
    
* **switch** handles multiple cases, like a ticket counter giving tickets based on destinations.
    

---

Don't forget to give feedback and share with your friends!
