# Understanding Control Flow in Programming (If, Else, Switch)

Programming is basically giving instructions to a computer.  
But sometimes the program needs to **make decisions**.

Just like real life.

Example:  
If it’s raining → take an umbrella.  
If not → just go outside.

This idea in programming is called **control flow**.

* * *

## What control flow means in programming

**Control flow** means deciding **which part of the code runs and when**.

Instead of running every line blindly, the program checks conditions and then chooses what to do next.

This is how programs become smart and interactive.

* * *

## The `if` statement

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

Example:

```plaintext
let age = 18;

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

Here the program checks the condition first.  
  
If it’s true, the code inside runs.

* * *

## The `if-else` statement

Sometimes we need two possible outcomes.

Example:

```plaintext
let age = 16;

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

So the program chooses **one path or the other**.

* * *

## The `else if` ladder

When there are **multiple conditions**, we use `else if`.

Example:

```plaintext
let marks = 75;

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

The program checks each condition **one by one** until one becomes true.

* * *

## The `switch` statement

`switch` is another way to handle multiple choices.

Example:

```plaintext
let day = 3;

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");
}
```

Important thing: `break` **stops the next cases from running**.

Without `break`, the program continues to the next case.

* * *

## When to use `switch` vs `if-else`

Use **if-else** when:

*   conditions involve ranges or comparisons
    
*   logic is more flexible
    

Use **switch** when:

*   checking one variable
    
*   matching exact values
    

Both solve similar problems, but sometimes one is cleaner.

* * *

# Simple Assignment Examples

## 1\. Check if a number is positive, negative, or zero

```plaintext
let num = -5;

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

Here I used **if-else** because we are comparing ranges (`> 0`, `< 0`).

* * *

## 2\. Print the day of the week using switch

```plaintext
let day = 2;

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;
  case 6:
    console.log("Saturday");
    break;
  case 7:
    console.log("Sunday");
    break;
  default:
    console.log("Invalid day");
}
```

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