# Masterji Logic (If, Else & Switch)

Welcome back, bro! ☕

In our last session, we gathered our data (Variables) and learned how to do the math and comparisons (Operators). But right now, our code is a bit..... dumb. It just runs straight from line 1 to line 100 without thinking.

Real applications don't work like that. If you click a "Submit" button with empty fields, the app shouldn't just crash, it should say, *"Hey, fill this out first!"* To make our code actually think and make decisions, we use **Control Flow**.

Instead of our RPG game from last time, let's look at something we all know too well. Imagine we are building the backend for a brand new EdTech platform called **Masterji**. We need to automate the logic for giving out course certificates, bounties, and goodies. Let's write the logic!

### 1\. `if` Statement

The `if` statement is the simplest way to make a decision. You give JavaScript a condition (using the Operators we learned last time!). If the condition is `true`, it runs the code inside the block `{}`. If it's `false`, it completely ignores it.

Let's check if a student is ready to graduate:

```javascript
let courseProgress = 100;

// Masterji checks the progress...
if (courseProgress === 100) {
  console.log("Congratulations! Your JS Certificate is in your inbox.");
}

// If progress is 99, JavaScript just skips the block and says nothing.
```

### 2\. `else` Statement

Just using `if` is great, but what if the student *isn't* at 100%? We shouldn't just leave them hanging in silence. We need a fallback plan. That’s where `else` comes in.

Think of `else` as the default response when the `if` condition fails.

```javascript
let assignmentScore = 45;
let passingMarks = 50;

if (assignmentScore >= passingMarks) {
  console.log("Awesome job! You passed the assignment. 🎉");
} else {
  console.log("Masterji says: Beta, you need to study more. Try again!");
}
```

*Notice how there is no condition next to* `else`*? It simply catches EVERYTHING that didn't pass the* `if` *test.*

### 3\. `else if` Ladder

Okay, now let's build the fun part: The Masterji Reward System.

What if we have more than two outcomes? Pass or Fail isn't enough. We want to give a Mechanical Keyboard to top scorers, a T-shirt to good scorers, and just stickers to the rest.

We chain conditions together using `else if`. JavaScript will check them one by one, from top to bottom. **As soon as it finds one that is** `true`**, it runs that code and skips the rest.**

```javascript
let testScore = 92;

if (testScore >= 95) {
  console.log("Bounty Unlocked: Mechanical Keyboard!");
} else if (testScore >= 80) {
  console.log("Goodie Unlocked: Exclusive Masterji T-Shirt!");
} else if (testScore >= 60) {
  console.log("Goodie Unlocked: Laptop Stickers!");
} else {
  console.log("You passed, but no goodies this time. Just my blessings.");
}
```

*Bro, order matters here!* If we put `testScore >= 60` at the very top, a student with `99` marks would hit that first condition, get some stickers, and the code would stop running. They’d miss out on their keyboard! Always check your highest/strictest conditions first.

![](https://cdn.hashnode.com/uploads/covers/677e148dc0398e29546d0922/1aba3af2-3f7d-475c-9b96-909a3f682b33.png align="center")

### 4\. `switch` Statement

The `else if` ladder is great for checking ranges (like `>= 80`). But what if a student is selecting a specific learning track on their Masterji dashboard?

If we have 5 different tracks, writing `else if (track === "Frontend")`, `else if (track === "Backend")`, over and over again gets messy and annoying to read.

When you have a single variable and you want to check it against a bunch of *exact* matches, use a `switch` statement!

```javascript
let selectedTrack = "DevOps";

switch (selectedTrack) {
  case "Frontend":
    console.log("Roadmap loaded: HTML, CSS, React. Have fun making buttons!");
    break;

  case "Backend":
    console.log("Roadmap loaded: Node.js, Express, Databases. Welcome to the shadows.");
    break;

  case "DevOps":
    console.log("Roadmap loaded: Docker, AWS, CI/CD. You love pain, don't you? 🐳");
    break;

  default:
    console.log("Track not found. Are you just here for the free chai?");
}
```

#### `break` Trap!

Did you notice the `break` keyword at the end of every case? This is crucial. If you forget to write `break`, JavaScript suffers from something called **"Fall-through"**.

If we removed the `break`s and selected `"Frontend"`, Masterji would print the Frontend roadmap... AND the Backend roadmap... AND the DevOps roadmap. It literally executes every line below it until it hits the end. Always use `break` to tell JS: *"We found our match, stop checking!"*

### Shortcut (Ternary Operator)

Sometimes, you just want to do a simple `if/else` on one single line. Devs hate typing, remember?

Let's check if a user gets premium access. Instead of writing 5 lines of `if/else`, we use the **Ternary Operator (**`? :`**)**.

It reads exactly like plain English: `Condition ? If True : If False`

```javascript
let hasPaidSubscription = true;

// Is it true ? Yes do this : No do this
let accessLevel = hasPaidSubscription ? "Premium Videos Unlocked" : "Show Ads";

console.log(accessLevel);
```

Makes you look like a senior dev.

![](https://cdn.hashnode.com/uploads/covers/677e148dc0398e29546d0922/00e15106-d531-4794-abfa-2479ad3659f6.png align="center")

### What's Next?

Look at us! We are officially controlling how our code thinks. Masterji is now fully operational, handing out bounties and kicking out students who fail their `if` checks.

But right now, if we want to grade 100 students, we'd have to write our logic 100 times. That violates the #1 rule of coding: **DRY (Don't Repeat Yourself)**.

In our next **JS Exploration**, we are going to learn how to pack our code into reusable chunks. We are tackling **Functions (Declaration vs. Expressions)**.

Grab some water, review your `switch` cases, and I'll catch you in the next one!
