js-control-flow
Control Flow Structures in JavaScript
I. if
Statement
if
StatementThe if statement is like a decision-making tool in JavaScript. It helps the program make choices based on certain conditions. Here's how it works:
if (condition) {
// Do something if the condition is true
} else {
// Do something else if the condition is false
}
Explanation:
You have a condition, which is like a question your program asks. If the answer to the question is "yes" (the condition is true), your program does something specific inside the curly braces {} after if. If the answer is "no" (the condition is false), your program skips the code inside if and does something else inside the else block.
Example:
let temperature = 25;
if (temperature > 30) {
console.log("It's hot!");
} else if (temperature > 20) {
console.log("It's warm.");
} else {
console.log("It's cold.");
In this example, the program checks if the temperature is greater than 30. If it is, it says "It's hot!" Otherwise, it says "It's not too hot." This way, your program can decide what to do based on different situations.
II. for
Loop
for
LoopThe for
loop lets your program do something over and over again. It's often used to go through a list of things (like numbers or words) or to repeat a task a certain number of times. Here's how it works:
for (start; condition; change) {
// Code to be done each time
}
Explanation:
Start: You decide where to begin (like starting from 0).
Condition: You set a rule for when to stop (like when reaching a certain number).
Change: You say how to move forward after each time (like adding 1).
Example:
for (let i = 0; i < 5; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4
}
In this example, the program starts counting from 0, stops when it reaches 5, and adds 1 each time. So, it prints the numbers 0 to 4.
III. while
Loop
while
LoopThe while loop is another way for a program to keep doing something as long as a certain rule is true. Here's how it works:
while (condition) {
// Code to do as long as the condition is true
}
Example:
let i = 0;
while (i < 5) {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
}
the program keeps counting from 0 to 4 as long as i is less than 5.
IV. switch
Statement
switch
StatementTheswitch
statement is like a traffic cop directing your program based on different situations. It helps your program make decisions in a cleaner way than using many if statements. Here's how it looks:
switch (expression) {
case value1:
// Code to do if expression matches value1
break;
case value2:
// Code to do if expression matches value2
break;
default:
// Code to do if none of the cases match
}
Explanation:
expression This is the variable or value you want to check against. It can be a variable, literal value, or an expression.
case value1: This is where you define a specific value that you want to compare with the expression. If the expression matches value1, the code inside this block is executed.
break; This is crucial. It tells JavaScript to exit the switch statement after executing the corresponding case. Without it, JavaScript would continue executing the code for all subsequent cases.
default: This is optional and provides a default case to be executed if none of the defined cases matches the expression.
Example:
let day = "Monday";
switch (day) {
case "Monday":
console.log("It's the start of the week.");
break;
case "Friday":
console.log("Weekend is approaching.");
break;
default:
console.log("It's a regular day.");
}
In this example, the switch statement checks the value of the day variable. If it matches "Monday," it executes the first case. If it matches "Friday," it executes the second case. If it doesn't match any of the defined cases, the code inside the default block is executed.
Important Notes:
Each case must end with
break
to prevent falling through to the next case.The default case is optional but useful for handling unexpected or undefined values.
The switch statement is a powerful tool for handling multiple conditions, especially when you have several cases to consider.
It enhances code readability and helps organize decision-making logic.
V. Other Control Flow Structures
for...in
Loop: Go through the properties of anobject
and picks up the names of each property during each turn.
const person = { name: "John", age: 30, job: "Developer" };
for (let property in person) {
console.log(property); // Outputs: name, age, job
}
for...of
Loop: go through the items of a list (likearrays
orstrings
) and grabs the actual values of the items as it moves forward.
const colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color); // Outputs: red, green, blue
}
do...while
Loop: This loop is like a task that you keep doing until you're satisfied. It's similar to when you want to count to five: you say the numbers (do) while checking if you've reached five (while).
let count = 1;
do {
console.log(count);
count++;
} while (count <= 5);
// Outputs: 1,2,3,4,5
try...catch
: It's a way to handle potential issues or errors in your code without letting them crash your entire program.
try {
// code that might cause an error
let result = 10 / 0; // This line could cause a division by zero error
console.log(result); // This line won't be executed if an error occurs
} catch (error) {
// What you do if you miss catching the ball (handle the error)
console.log("Oops! Something went wrong: " + error.message);
}
In this example, the try block attempts to perform a division by zero, which is an error. The catch block is like your backup plan. If an error occurs, it jumps to the catch block and logs a message saying, "Oops! Something went wrong," along with the error message.
These control flow structures are like different tools in your programming toolbox. They help your program make choices, go through lists, repeat tasks, and handle different situations.
Last updated