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

Hey Everyone,

In this blog, we will learn about JavaScript control flows.

When writing programs, we often need the computer to **make decisions**.

For example:

*   If it is raining → take an umbrella
    
*   If your marks are above 90 → grade A
    
*   If today is Sunday → relax
    

Programs work the same way.  
They decide **which code should run based on conditions**.

This idea is called **control flow**.

* * *

### What is Control Flow?

**Control flow** determines **which part of the program runs and when**.

Instead of executing code line by line, the program can make decisions.

Example:

```javascript
let age = 18;

if (age >= 18) {
  console.log("You can vote");
}
```

If the condition is true, the code runs.

If it is false, the code is skipped.

* * *

### The if Statement

The `if` statement runs code **only if a condition is true**.

### Example

```javascript
let marks = 85;

if (marks > 50) {
  console.log("You passed the exam");
}
```

### How it works

1.  Check the condition → `marks > 50`
    
2.  If true → run the code inside `{ }`
    
3.  If false → skip it
    

* * *

### The if-else Statement

Sometimes we want **two possible outcomes**.

Example:

```javascript
let age = 16;

if (age >= 18) {
  console.log("You can vote");
} else {
  console.log("You cannot vote yet");
}
```

### Step-by-step

1.  Check condition
    
2.  If true → run `if` block
    
3.  If false → run `else` block
    

* * *

### The else-if Ladder

Sometimes we have **multiple conditions**.

Example: grading system.

```javascript
let marks = 75;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else if (marks >= 50) {
  console.log("Grade C");
} else {
  console.log("Fail");
}
```

### How it runs

The program checks **conditions from top to bottom**.

As soon as one condition is **true**, the rest are skipped.

* * *

### The switch Statement

The `switch` statement is used when we want to **check a value against multiple options**.

Example: printing the day of the week.

```javascript
let day = 2;

switch (day) {
  case 1:
    console.log("Monday");
    break;

  case 2:
    console.log("Tuesday");
    break;

  case 3:
    console.log("Wednesday");
    break;

  default:
    console.log("Invalid day");
}
```

* * *

### Why a break is Important

`break` stops the switch after a match is found.

Without `break`, JavaScript continues executing the next cases.

Example without break:

```javascript
let day = 1;

switch(day) {
  case 1:
    console.log("Monday");
  case 2:
    console.log("Tuesday");
}
```

Output:

```javascript
Monday
Tuesday
```

Because the program **falls through to the next case**.

* * *

### When to Use switch vs if-else

| Use Case | Best Choice |
| --- | --- |
| Checking ranges (marks, age, score) | if-else |
| Checking exact values | switch |
| Multiple numeric or string cases | switch |
| Complex conditions | if-else |

Example:

```javascript
marks > 90
marks < 50
```

These are **range conditions**, so `if-else` works better.

But:

```javascript
day === "Monday"
day === "Tuesday"
```

Here `switch` is cleaner.

* * *

### Flow of if-else Decision

```plaintext
        Condition?
        /      \
     True      False
      |          |
   Run Code   Run Else
```

This is how the program decides which path to follow.

* * *

### Switch Branching Diagram

```plaintext
        switch(value)
             |
    ---------------------
    |    |    |    |
  case1 case2 case3 default
    |    |    |    |
   code code code code
```

The program jumps to the **matching case**.

* * *

### Practice Assignment

Try these programs in your browser console.

* * *

### 1\. Check Number Type

Write a program that checks if a number is:

*   Positive
    
*   Negative
    
*   Zero
    

Example:

```javascript
let num = -5;

if (num > 0) {
  console.log("Positive number");
} else if (num < 0) {
  console.log("Negative number");
} else {
  console.log("Zero");
}
```

I used **if-else** because we are checking **different conditions**.

* * *

### 2\. Print Day of Week

Write a program using `switch`.

```javascript
let day = 3;

switch(day) {
  case 1:
    console.log("Monday");
    break;

  case 2:
    console.log("Tuesday");
    break;

  case 3:
    console.log("Wednesday");
    break;

  case 4:
    console.log("Thursday");
    break;

  case 5:
    console.log("Friday");
    break;

  default:
    console.log("Invalid day");
}
```

Here I used a **switch** because we are checking **exact values**.

* * *

And now, you know what control flow is in JavaScript.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!

* * *
