Contents
Control Structures
Control structures in JavaScript allow you to dictate the flow of your program, enabling it to make decisions, repeat actions, and handle different conditions dynamically. The primary control structures include if statements, switch statements, and loops (for
, while
, do-while
).
If Statements
The if statement is used to execute a block of code based on a condition. If the condition evaluates to true
, the code within the block is executed.
Syntax:
if (condition) {
// Code to be executed if condition is true
}
Example: Basic If Statement
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
}
1.1 If-Else Statement
The if-else statement provides an alternative block of code to execute if the condition is false
.
let temperature = 25;
if (temperature > 30) {
console.log("It's a hot day!");
} else {
console.log("The weather is nice.");
}
1.2 Else-If Ladder
An else-if ladder allows you to test multiple conditions in sequence.
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
Switch Statements
The switch statement evaluates an expression and matches its value against multiple case clauses. It is a cleaner alternative to a series of if-else
statements when dealing with multiple possible values for a single variable.
Syntax:
switch (expression) {
case value1:
// Code to be executed if expression === value1
break;
case value2:
// Code to be executed if expression === value2
break;
default:
// Code to be executed if none of the cases match
}
Example: Basic Switch Statement
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
console.log(dayName); // Output: "Wednesday"
break
: Ends the switch statement. Without it, JavaScript will continue to execute the next cases, even if they do not match the expression (known as “fall-through”).default
: Specifies the code to run if no case matches. It is optional but useful for handling unexpected values.
Loops (for, while, do-while)
Loops allow you to repeatedly execute a block of code as long as a specified condition is true. JavaScript supports several types of loops, including for
, while
, and do-while
.
3.1 For Loop
The for loop is commonly used when you know the number of iterations beforehand.
Syntax:
for (initialization; condition; increment) {
// Code to be executed in each iteration
}
Example: Basic For Loop
for (let i = 0; i < 5; i++) {
console.log("Number: " + i);
}
initialization
: Initializes the loop counter (let i = 0
).condition
: Checks if the loop should continue (i < 5
).increment
: Updates the loop counter (i++
).
3.2 While Loop
The while loop executes a block of code as long as a specified condition is true
. Use it when the number of iterations is not known in advance.
Syntax:
while (condition) {
// Code to be executed as long as condition is true
}
Example: Basic While Loop
let count = 0;
while (count < 3) {
console.log("Count is: " + count);
count++;
}
3.3 Do-While Loop
The do-while loop is similar to the while
loop, but it guarantees that the code block will execute at least once because the condition is evaluated after the code block runs.
Syntax:
do {
// Code to be executed
} while (condition);
Example: Basic Do-While Loop
let num = 5;
do {
console.log("Number is: " + num);
num++;
} while (num < 3);
In this example, “Number is: 5” is printed once, even though the condition (num < 3
) is false
from the start.
Summary
Control structures in JavaScript provide the means to control the flow of your code based on conditions and to repeat actions. The if statement is used for conditional execution, while the switch statement offers a clear way to handle multiple values for a single variable. Loops (for
, while
, do-while
) allow you to execute a block of code multiple times, making it easier to manage repetitive tasks. Understanding these control structures is essential for writing effective, dynamic JavaScript code.