Skip to main content

Command Palette

Search for a command to run...

Understanding Control Flow in Programming in JS

Updated
4 min read

Imagine this real-life situation:

  • if it is raining → take an umbrella

  • else → wear sunglasses

  • if your age is 18 or more → you can vote

  • else → you cannot vote

We make decisions like this every day.

In programming, this decision-making process is called control flow.

What is control flow?

Control flow determines which code runs and when.

By default, programs execute line by line (top to bottom).

But using control flow statements, we can:

  • Run code only if a condition is true

  • Choose between multiple options

  • Skip parts of code

The if Statement

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

Syntax

if (condition) {
    // code runs if condition is true
}

Example

let age = 20;

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

Here console will runs if age is greater than 18, else nothing will be printed in the console.

The if-else Statement

Used when you want two possible outcomes.

Syntax

if (condition) {
    // runs if true
}else {
    // runs if false
}

Example

let marks = 40;

if (marks >= 35) {
    console.log("You passed.");
}else {
    console.log("You failed.");
}

How it works

  • If condition true → first block runs

  • Otherwise → else block runs

Only one block executes.

The else if Ladder

Used when there are multiple conditions.

Syntax

if (condition1) {
    // block 1
}else if (condition2) {
    // block 2
}else {
    // default block
}

Example

let marks = 82;

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 works

  • Is 82 >= 90? ❌

  • Is 82 >= 75? ✅

  • Stops checking further

  • Prints "Grade B"

Important: It stops once a true condition is found.

The switch Statement

Used when comparing one value against many fixed options.

Syntax

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

Example

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

How it works

  • day = 3

  • Matches case 3

  • Prints "Wednesday"

  • break stops execution

Why break is Important

If you remove break, execution continues to next case.

Example without break

switch (2) {
    case1:
        console.log("One");
    case2:
        console.log("Two");
    case3:
        console.log("Three");
}

Output:

Two
Three

Because it keeps running after matching case.

So, Always use break unless you intentionally want fall-through.


When to Use switch vs if-else

Use if-else when Use switch when
Comparing ranges (age > 18) Comparing exact values
Complex logical conditions One variable, many fixed cases
Multiple different variables Clean menu-like options

Example:

  • Age check → use if

  • Day number (1–7) → use switch

  • Grade range → use if-else

Assignment

  1. Write a program that checks:

    • If a number is positive, negative, or zero
  2. Write a program that prints the day of the week using switch

  3. Explain which control structure you used and why

Solutions

Program to find a number positive, negative or zero

Here we are comparing range of values, so will implement using if-else

let num = 10;

if(num < 0){
  console.log("Negitive Number")
}else if (num > 0){
  console.log("Positive Number")
}else {
  console.log("Zero")
}

Program that prints the day of the week

Here we need to compare exact values, so we are using switch case

const 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;
  case 6:
    console.log("Saturday")
    break;
  case 7:
    console.log("Sunday")
    break;
  default:
    console.log("Enter valid week number from 1 to 7")
}