1

Lesson 4: Control Flow in JavaScript

Control flow refers to the order in which statements or instructions are executed in a program. JavaScript provides several ways to control the flow of execution using conditional statements and loops.

Conditional Statements

1. if and else Statements

The if statement is used to execute a block of code if a specified condition is true. The else statement can be used to execute code if the condition is false.

      if (condition) {
          // Code to execute if condition is true
      } else {
          // Code to execute if condition is false
      }
          

Example of if-else Statements:

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

2. else if Statement

The else if statement is used to specify a new condition if the first condition is false.

      if (condition1) {
          // Code to execute if condition1 is true
      } else if (condition2) {
          // Code to execute if condition2 is true
      } else {
          // Code to execute if all conditions are false
      }
          

Example of else-if Statements:

      let score = 85;
      if (score >= 90) {
          console.log("Grade: A");
      } else if (score >= 80) {
          console.log("Grade: B");
      } else {
          console.log("Grade: C");
      }
              

3. switch Statement

The switch statement evaluates an expression and executes the corresponding case block depending on the result. If no case matches, the default block is executed.

      switch(expression) {
          case value1:
              // Code to execute for value1
              break;
          case value2:
              // Code to execute for value2
              break;
          default:
              // Code to execute if no case matches
      }
          

Example of switch Statement:

      let day = 2;
      switch(day) {
          case 1:
              console.log("Monday");
              break;
          case 2:
              console.log("Tuesday");
              break;
          default:
              console.log("Unknown Day");
      }
              

Loops

1. for Loop

The for loop repeats a block of code a specified number of times.

      for (initialization; condition; increment/decrement) {
          // Code to be executed
      }
          

Example of for Loop:

      for (let i = 1; i <= 5; i++) {
          console.log("Iteration " + i);
      }
              

2. while Loop

The while loop repeats a block of code as long as the condition is true.

      while (condition) {
          // Code to be executed
      }
          

Example of while Loop:

      let i = 1;
      while (i <= 5) {
          console.log("Iteration " + i);
          i++;
      }
              

3. do-while Loop

The do-while loop is similar to the while loop, but it will always execute the block of code at least once, even if the condition is false.

      do {
          // Code to be executed
      } while (condition);
          

Example of do-while Loop:

      let i = 1;
      do {
          console.log("Iteration " + i);
          i++;
      } while (i <= 5);
              

Assignment

Create a program that:

  1. Uses an if statement to check if a number is positive, negative, or zero.
  2. Uses a switch statement to display the day of the week based on a number (1 for Monday, 2 for Tuesday, etc.).
  3. Loops through an array of numbers and prints only the even numbers.