JavaScript Control Flow Statements

In this chapter, we will learn about JavaScript control flow statements. These statements are used to control the execution of code based on certain conditions and loops. We will cover:

  • if Statement
  • else Statement
  • else if Statement
  • switch Statement
  • for Loop
  • while Loop
  • do...while Loop
  • break Statement
  • continue Statement

if Statement

The if statement is used to execute a block of code if a specified condition is true.

Syntax

if (condition) { // code to be executed if the condition is true } 

Example

let age = 18; if (age >= 18) { console.log("You are an adult."); } // Output: You are an adult. 

else Statement

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

Syntax

if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false } 

Example

let age = 16; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); } // Output: You are a minor. 

else if Statement

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

Syntax

if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition1 is false and condition2 is true } else { // code to be executed if condition1 and condition2 are false } 

Example

let marks = 75; if (marks >= 90) { console.log("Grade: A"); } else if (marks >= 80) { console.log("Grade: B"); } else if (marks >= 70) { console.log("Grade: C"); } else { console.log("Grade: D"); } // Output: Grade: C 

switch Statement

The switch statement is used to execute one of many blocks of code based on the value of an expression.

Syntax

switch (expression) { case value1: // code to be executed if expression === value1 break; case value2: // code to be executed if expression === value2 break; // add more cases as needed default: // code to be executed if expression doesn't match any case } 

Example

let day = 2; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Another day"); } // Output: Tuesday 

for Loop

The for loop is used to execute a block of code a specified number of times.

Syntax

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

Example

for (let i = 0; i < 5; i++) { console.log(i); } // Output: 0 1 2 3 4 

while Loop

The while loop is used to execute a block of code as long as a specified condition is true.

Syntax

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

Example

let i = 0; while (i < 5) { console.log(i); i++; } // Output: 0 1 2 3 4 

do…while Loop

The do...while loop is similar to the while loop, but it executes the block of code at least once before checking the condition.

Syntax

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

Example

let i = 0; do { console.log(i); i++; } while (i < 5); // Output: 0 1 2 3 4 

break Statement

The break statement is used to exit a loop or switch statement before it completes its execution.

Example

for (let i = 0; i < 10; i++) { if (i === 5) { break; } console.log(i); } // Output: 0 1 2 3 4 

continue Statement

The continue statement is used to skip the current iteration of a loop and continue with the next iteration.

Example

for (let i = 0; i < 10; i++) { if (i === 5) { continue; } console.log(i); } // Output: 0 1 2 3 4 6 7 8 9 

Conclusion

In this chapter, you learned about JavaScript control flow statements, including if, else, else if, switch, for loop, while loop, do...while loop, break, and continue statements. These control flow statements are essential for making decisions and repeating tasks in your code.

Leave a Comment

Scroll to Top