Control Flow in JavaScript: If-Else, Switch, Loops
Control Flow in JavaScript: If-Else, Switch, Loops
Control flow in JavaScript determines the order in which statements are executed in a program. When building interactive web applications, mastering control flow structures like if-else
statements, switch
cases, and loops is essential. These tools help developers make decisions in their code, allowing it to execute different parts depending on conditions.
If-Else Statements
The if-else statement is one of the most fundamental control flow tools in JavaScript. It allows you to execute code blocks based on whether a condition is true or false.
Here’s the basic syntax of an if-else statement:
if (condition) {
// Code to run if condition is true
} else {
// Code to run if condition is false
}
Example:
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
In this example, if the age
is 18 or more, the message "You are eligible to vote" will be logged; otherwise, the program will output "You are not eligible to vote."
Else If Statements
The else if statement allows you to test multiple conditions. If the initial condition is false, the program moves on to check additional conditions until one is found to be true.
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: D");
}
In this example, the program checks the value of score
and assigns a grade based on the ranges defined by the conditions.
Switch Statements
The switch statement is another control flow tool that allows you to execute one of many code blocks based on the value of an expression. Unlike if-else
, which checks conditions, a switch
statement compares an expression to a series of possible values.
switch (expression) {
case value1:
// Code to run if expression === value1
break;
case value2:
// Code to run if expression === value2
break;
default:
// Code to run if no case matches
}
Example:
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
In this example, the program checks the value of day
and logs the corresponding day of the week. If no match is found, the default
case is executed.
Loops in JavaScript
Loops are control flow structures that allow you to run a block of code repeatedly based on a condition. JavaScript offers several types of loops, including for, while, and do-while loops. These loops are essential for tasks that involve iterating over arrays, performing repetitive actions, or processing data.
For Loops
The for loop is commonly used to execute a block of code a specific number of times. It has three main components: an initializer, a condition, and an increment expression.
for (initializer; condition; increment) {
// Code to run on each iteration
}
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
This for
loop prints the numbers 0 through 4 to the console. The loop runs as long as the condition (i < 5
) is true.
While Loops
The while loop repeats a block of code as long as the specified condition is true.
while (condition) {
// Code to run while the condition is true
}
Example:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
This while
loop behaves similarly to the for
loop but separates the initialization and increment from the loop’s structure.
Do-While Loops
The do-while loop is similar to the while
loop, but it guarantees that the block of code will run at least once, even if the condition is false.
do {
// Code to run at least once
} while (condition);
Example:
let i = 5;
do {
console.log(i);
i++;
} while (i < 5);
In this example, the code inside the do
block runs once, even though the condition (i < 5
) is false from the beginning.
Break and Continue
JavaScript provides the break
and continue
statements to control the flow of loops.
- Break: Exits the loop immediately.
- Continue: Skips the current iteration and moves to the next one.
Example using break
:
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exit loop when i is 5
}
console.log(i);
}
Example using continue
:
for (let i = 0; i < 10; i++) {
if (i === 5) {
continue; // Skip the iteration when i is 5
}
console.log(i);
}
Conclusion
Understanding control flow is crucial for writing efficient and effective JavaScript code. By mastering if-else statements, switch cases, and loops, you can create programs that handle decision-making and repetition with ease.
Start incorporating these control flow structures in your JavaScript projects to build dynamic and responsive applications that react to user input and perform complex tasks efficiently.
Comments
Post a Comment