JavaScript Control Flow: A Deep Dive
In JavaScript, control flow determines the order in which statements are executed in a program. Control flow structures allow us to make decisions, loop through data, and execute code conditionally. Let's explore these structures in depth with explanations and multiple examples for better understanding.
1. Conditional Statements
Conditional statements help us execute code based on certain conditions.
1.1 if Statement
The if
statement executes a block of code if a specified condition evaluates to true
.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example 1:
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
}
Explanation: Since age
is 18, which meets the condition (>= 18
), the message will be printed.
Example 2:
let temperature = 25;
if (temperature > 30) {
console.log("It's a hot day.");
}
Explanation: The condition temperature > 30
is false
, so the code inside if
will not execute.
Example 3:
let num = 0;
if (num) {
console.log("Number is non-zero");
}
Explanation: Since 0
is a falsy value in JavaScript, the condition fails, and nothing is printed.
1.2 if-else Statement
The if-else
statement provides an alternative block of code to execute when the condition is false
.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example 1:
let marks = 45;
if (marks >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
Explanation: Since marks
is less than 50, the else
block executes, printing "Fail".
Example 2:
let light = "red";
if (light === "green") {
console.log("Go");
} else {
console.log("Stop");
}
Explanation: The condition fails because light
is "red", so the else
block runs.
Example 3:
let loggedIn = false;
if (loggedIn) {
console.log("Welcome!");
} else {
console.log("Please log in");
}
Explanation: Since loggedIn
is false
, the else
block executes.
1.3 if-else if-else Statement
Used for multiple conditions.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
Example 1:
let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
Explanation: Since score
is 75, the second condition is met, so "Grade: B" is printed.
2. Switch Statement
The switch
statement evaluates an expression and matches it against multiple cases.
Syntax:
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no match found
}
Example 1:
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");
}
Explanation: Since day
is 3, "Wednesday" is printed.
3. Looping Statements
Loops allow us to execute a block of code multiple times.
3.1 for Loop
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute
}
Example 1:
for (let i = 1; i <= 5; i++) {
console.log("Hello World!", i);
}
Explanation: Prints "Hello World!" 5 times.
3.2 while Loop
Syntax:
while (condition) {
// Code to execute
}
Example 1:
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Explanation: Prints numbers from 1 to 5.
3.3 do-while Loop
Syntax:
do {
// Code to execute
} while (condition);
Example 1:
let num = 1;
do {
console.log(num);
num++;
} while (num <= 5);
Explanation: Runs at least once, then checks the condition.
4. Break and Continue
4.1 break Statement
Used to exit a loop immediately.
Example:
for (let i = 1; i <= 10; i++) {
if (i === 5) break;
console.log(i);
}
Explanation: Loop stops when i
reaches 5.
4.2 continue Statement
Skips the current iteration and moves to the next one.
Example:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
Explanation: 3
is skipped in the loop.
This guide provides a deep understanding of JavaScript's control flow, helping beginners grasp the concepts through well-explained examples. Happy coding!