JavaScript: Step 3- Control flow statements (if/else, switch/case, loops)

Control flow statements help determine the order in which different parts of your code run. In JavaScript, you have if/else statements, switch/case statements, and various types of loops. They work similarly to Java’s control flow constructs.

1. If/Else Statements

These statements let you execute different blocks of code based on a condition. This is similar to Java’s if/else statements.

Syntax:

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

Example Program:

let x = 5;

if (x > 2) {
  console.log("x is greater than 2");
} else {
  console.log("x is less than or equal to 2");
}

Output:

x is greater than 2

Comparison to Java:

This is very similar to Java’s if statements:

int x = 5;

if (x > 2) {
    System.out.println("x is greater than 2");
} else {
    System.out.println("x is less than or equal to 2");
}

2. Switch/Case Statements

These are used to select one block of code to execute based on the value of an expression. It’s similar to Java’s switch statements.

Syntax:

switch (expression) {
  case value1:
    // code to execute if expression equals value1
    break;
  case value2:
    // code to execute if expression equals value2
    break;
  default:
    // code to execute if no cases match
}

Example Program:

let day = "Monday";

switch (day) {
  case "Monday":
    console.log("Today is Monday");
    break;
  case "Tuesday":
    console.log("Today is Tuesday");
    break;
  default:
    console.log("Today is neither Monday nor Tuesday");
}

Output:

Today is Monday

Comparison to Java:

This is similar to Java’s switch statement:

String day = "Monday";

switch (day) {
    case "Monday":
        System.out.println("Today is Monday");
        break;
    case "Tuesday":
        System.out.println("Today is Tuesday");
        break;
    default:
        System.out.println("Today is neither Monday nor Tuesday");
}

3. Loops

Loops let you repeat a block of code multiple times. JavaScript has for, while, and do/while loops, just like Java.

a. For Loop

Use this when you know how many times you need to loop.

Syntax:

for (initialization; condition; increment/decrement) {
  // code to execute in each iteration
}

Example Program:

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

Output:

1
2
3
4
5

Comparison to Java:

Similar to Java’s for loop:

for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}

b. While Loop

Use this when you don’t know how many times you need to loop but have a condition.

Syntax:

while (condition) {
  // code to execute in each iteration
}

Example Program:

let i = 1;

while (i <= 5) {
  console.log(i);
  i++;
}

Output:

1
2
3
4
5

Comparison to Java:

Similar to Java’s while loop:

int i = 1;

while (i <= 5) {
    System.out.println(i);
    i++;
}

c. Do/While Loop

This loop executes the block of code at least once, even if the condition is false.

Syntax:

do {
  // code to execute in each iteration
} while (condition);

Example Program:

let i = 1;

do {
  console.log(i);
  i++;
} while (i <= 5);

Output:

1
2
3
4
5

Comparison to Java:

Similar to Java’s do/while loop:

int i = 1;

do {
    System.out.println(i);
    i++;
} while (i <= 5);

Control flow statements are essential for making decisions and repeating actions in your programs. By understanding and using these statements effectively, you can create more complex and dynamic JavaScript programs.


Leave a Reply

Your email address will not be published. Required fields are marked *