Skip to main content

Control Structures (if statements, loops)

Control structures are essential for managing the flow of your code. They allow you to make decisions, repeat tasks, and control the execution of your program. In this section, we'll explore various control structures in JavaScript.

If Statements:

  • The if statement is used to execute a block of code if a condition is true.
  • if Statements are used for conditional execution of code. You can execute a block of code if a specified condition is true.

Example:

index.js
let age = 18;

if (age < 18) {
console.log("You are a minor.");
} else {
console.log("You are an adult.");
}

Switch Case:

The switch statement allows you to compare a value against multiple possible case values and execute a block of code corresponding to the matched case.

Example:

index.js
let day = "Monday";

switch (day) {
case "Monday":
console.log("It's the start of the week.");
break;
case "Friday":
console.log("It's almost the weekend.");
break;
default:
console.log("It's a regular day.");
}

Do-While Loop:

The do-while loop is used to execute a block of code at least once, and then the condition is checked.

Example:

index.js
let count = 0;

do {
console.log("Count: " + count);
count++;
} while (count < 3);

While Loop:

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

Example:

index.js
let count = 0;

while (count < 3) {
console.log("Count: " + count);
count++;
}

For Loop:

The for loop is used to iterate a specific number of times.

Example:

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

These control structures are fundamental for directing the flow of your JavaScript code. They allow you to make decisions, handle various cases, and create loops to perform repetitive tasks. Understanding and using these structures is crucial for writing effective JavaScript programs.