Selected topic
Flow Control
Prefer practical output? Use related tools below while reading.
if statement allows you to execute code if a specified condition is true.javascript
let x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}else statement allows you to execute code if the condition in the if statement is false.javascript
let x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}else if statement allows you to check multiple conditions.javascript
let x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else if (x == 5) {
console.log("x is equal to 5");
} else {
console.log("x is less than 5");
}
for loop allows you to execute code repeatedly for a specified number of times.javascript
let fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}while loop allows you to execute code repeatedly as long as a condition is true.javascript
let x = 5;
while (x > 0) {
console.log(x);
x--;
}do-while loop allows you to execute code at least once and then repeat it as long as a condition is true.javascript
let x = 5;
do {
console.log(x);
x--;
} while (x > 0);
break statement allows you to terminate a loop or switch statement.javascript
let i = 0;
while (i < 5) {
console.log(i);
if (i == 3) break;
i++;
}continue statement allows you to skip the current iteration of a loop and continue with the next one.javascript
let i = 0;
while (i < 5) {
if (i == 3) {
i++;
continue;
}
console.log(i);
i++;
}