Javascript Control Structures
if statement: The if statement is used to execute a block of code if a condition is true. For example:
let x = 10;
if (x > 0) {
console.log("x is positive");
//This code will output "x is positive" to the console, because the condition x > 0 is true.
}
If-else statement: You can also use an else clause to specify a block of code that should be executed if the condition is false:
let x = 10;
if (x > 0) {
console.log("x is positive");
//This code will output "x is positive" to the console, because the condition x > 0 is true.
} else {
console.log("x is not positive");
//This code will output "x is not positive" to the console, if the condition x > 0 is false.
}
Switch statement: The switch statement is used to execute a block of code based on the value of a variable. For example:
let x = "red";
switch (x) {
case "red":
console.log("x is red");
break;
case "blue":
console.log("x is blue");
break;
default:
console.log("x is neither red nor blue");
}
This code will output "x is red" to the console, because the value of x is "red". The switch statement is often used as an alternative to a series of if statements, when you have to check multiple values.
Javascript Loops: In JavaScript, a loop is a control flow statement that allows you to execute a block of code repeatedly. There are several types of loops in JavaScript:
for Loop: The for loop is used to execute a block of code a specified number of times. It has the following syntax:
for (initialization; condition; increment / decrement) {
// code to be executed
}
The initialization statement is executed only once at the beginning of the loop. It is used to initialize a counter variable. The condition is evaluated at the beginning of each iteration. If the condition is true, the loop continues. If the condition is false, the loop terminates. The increment/decrement statement is executed at the end of each iteration. It is used to update the counter variable.
A counter variable is a variable that keeps track of a numerical value. It's often used to count the number of times something happens or to iterate through a set of data.
an example of the for loop
for (let i = 1; i <= 10; i++) {
console.log(i);
//This code will display 1-10 in the browser console.
//It display that because initially the value of i is 1, 1 is add to i using (i++) and it is made to repeat itself using (for) but not exceeding 10 according to (i<=10).
}
While Loop: The while loop is used to execute a block of code as long as a certain condition is true. It has the following syntax:
while (condition) {
// code to be executed
}
The condition is evaluated at the beginning of each iteration. If the condition is true, the loop continues. If the condition is false, the loop terminates.
an example of the while loop
let i = 1; // Initialize a variable 'i' with the value 1.
while (i <= 10) {
// Start a while loop that runs as long as 'i' is less than or equal to 10.
console.log(i); // Log the message "this is the no X" to the console, where X is the current value of 'i'.
i++; // Increment the value of 'i' by 1.
}
do-while Loop: The do-while loop is similar to the while loop, but the block of code is always executed at least once. It has the following syntax:
do {
// code to be executed
} while (condition);
The condition is evaluated at the end of each iteration. If the condition is true, the loop continues. If the condition is false, the loop terminates.
an example of the do-while loop
let i = 1; // Initialize a variable 'i' with the value 1
do {
console.log(i); // Print the current value of 'i' to the console
i++; // Increment the value of 'i' by 1
} while (i <= 10); // Continue the loop as long as 'i' is less than or equal to 10
for-in Loop: The for-in loop is used to iterate over the properties of an object. It has the following syntax:
for (variable in object) {
// code to be executed
}
The variable takes on the value of each property in the object, and the loop continues until all properties have been processed.
an example of the for-in loop
// Create an object named 'person' with three properties: name, age, and job
let person = {
name: "John", // The 'name' property with the value "John"
age: 30, // The 'age' property with the value 30
job: "developer", // The 'job' property with the value "developer"
};
// Use a for-in loop to iterate over the properties of the 'person' object
for (let prop in person) {
// Inside the loop, 'prop' will be assigned the name of each property in the object in turn
// Log the property name and its value to the console in the format: "property: value"
console.log(prop + ": " + person[prop]);
// 'person[prop]' accesses the value of the property named 'prop' in the 'person' object
}
// The expected output will be:
// name: John
// age: 30
// job: developer
for-of Loop: The for-of loop is a modern way to iterate over the values of an iterable object, such as an array or a string. It was introduced in ECMAScript 6 (ES6) and has the following syntax:
for (variable of object) {
// code to be executed
}
The variable takes on the value of each element in the object, and the loop continues until all elements have been processed.
an example of the for-of loop
// Define an array called 'numbers' with five elements: 1, 2, 3, 4, and 5
let numbers = [1, 2, 3, 4, 5];
// Use a 'for...of' loop to iterate over each element in the 'numbers' array
for (let num of numbers) {
// Inside the loop, log the current element (num) to the console
console.log(num);
}
Break and continue statements in Loops: The break and continue statements are used to control the flow of a loop in JavaScript. The break statement is used to exit a loop prematurely. It can be used in any type of loop, such as a for, while, or do-while loop. When the break statement is encountered, the loop is immediately terminated and the flow of control moves to the statement following the loop.
break: Here is an example of a for loop with a break statement:
for (let i = 0; i < 10; i++) {
if (i == 5) {
break; // If i is 5, break the loop and stop further iterations.
}
console.log(i);
}
In this example, the loop will terminate when i is 5 and the break statement is encountered. The output will be the numbers 0 through 4.
continue: The continue statement is used to skip the rest of the current iteration of a loop and move on to the next one. It can also be used in any type of loop. When the continue statement is encountered, the loop's counter is updated and the flow of control returns to the loop's condition.
let i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
console.log(i);
}
In this example, the continue statement will skip the rest of the current iteration when i is even and move on to the next iteration. The output will be the odd numbers 1 through 9