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

### What is control flow ?

Control flow means the order to follow the instruction to execute a program. Like the path computer need to follow in a program. It is like a traffic signal when the signal lights turn green the car need to go and when it is red it need to stop it is like that only.

![](https://cdn.hashnode.com/uploads/covers/6952a8da1702bb0b40b79e89/acf182b7-1660-4c7d-aa21-a66d8b2b740d.png align="center")

### Why it is used ?

It is used to control the flow of the program when it runs if we don't control the flow it will run line by line without making any decision we need it. It allows the code to make decision, repeat task and and skip certain instructions.

**The if statement**

It is the most basic decision making statement programming only if the statement is true then only the code executes.

```xml
let age = 20; 
if (age >= 18) { 
  console.log("You are eligible to vote."); 
}
```

Here the program will check the age if the age if found the condition true then only it will print that desired output.

**The if-else statement**

But we don't have only true statement every time we may have the other answers for a certain decision so we have if-else statement. Here if we don't found the condition true in order to print nothing we can print any other thing if the statement is false.

```xml
let age = 16; 
if (age >= 18) { 
  console.log("You can vote."); 
} 
else { 
  console.log("You are not eligible to vote yet."); 
}
```

Here we have given that if the persons age is not found true we will execute the second statement. It insures the two block of the code.

**The else-if ladder**

This else if ladder is used when we have multiple blocks of code to execute at a time checking every block of code having multiple conditions. We use else if ladder.

```xml
let marks = 75; 
if (marks >= 90) {
 console.log("Grade A"); 
} else if (marks >= 75) {
 console.log("Grade B"); 
} else if (marks >= 60) {
 console.log("Grade C"); 
} else {
 console.log("Grade D"); 
}
```

Here we have multiple conditions to check it check the all conditions line by line until the statements get the true value it not then we have the last else condition which will be executed if none of the statements get true.

**Switch Statement**

The switch statement is the another conditional checking statement which compares a variable by switching the case which have been provided to check It checks the multiple cases which matches the condition true to execute a value.

![](https://cdn.hashnode.com/uploads/covers/6952a8da1702bb0b40b79e89/95ce8607-9745-46b3-a4f0-47f2c3966bee.png align="center")

```xml
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; 
default: 
    console.log("Invalid day"); 
}
```

Now here it also checks the multiple statements but we have a default case also to give the condition that if none of the statement gets true execute the default statement.

### When to Use switch vs if-else

Both switch and if-else statements help programs make decisions, but they are used in different situations.

**Use if-else when:**

You need to check ranges or complex conditions. Conditions involve comparisons like >, <, >=, <=. Multiple variables are involved.

Example:

```plaintext
if (temperature > 30) {
  console.log("It's hot outside.");
}
```

**Use switch when:**

*   You are comparing one variable with many exact values
    
*   The program needs to choose one option from multiple fixed choices
    
*   The code becomes cleaner than long if-else chains
    

**Example:**

```plaintext
switch (color) {
  case "red":
    console.log("Stop");
    break;

  case "green":
    console.log("Go");
    break;
}
```
