Selected topic

Control Flow

Flow Control

Prefer practical output? Use related tools below while reading.

Conditional Statements

  1. If Statement: The if statement allows you to execute code if a specified condition is true.
Example:
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");
}
  1. If-Else Statement: The else statement allows you to execute code if the condition in the if statement is false.
Example:
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");
}
  1. If-Else If Statement: The else if statement allows you to check multiple conditions.
Example:
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");
}

Loops

  1. For Loop: The for loop allows you to execute code repeatedly for a specified number of times.
Example:
javascript
let fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}
  1. While Loop: The while loop allows you to execute code repeatedly as long as a condition is true.
Example:
javascript
let x = 5;
while (x > 0) {
    console.log(x);
    x--;
}
  1. Do-While Loop: The do-while loop allows you to execute code at least once and then repeat it as long as a condition is true.
Example:
javascript
let x = 5;
do {
    console.log(x);
    x--;
} while (x > 0);

Break and Continue

  1. Break Statement: The break statement allows you to terminate a loop or switch statement.
Example:
javascript
let i = 0;
while (i < 5) {
    console.log(i);
    if (i == 3) break;
    i++;
}
  1. Continue Statement: The continue statement allows you to skip the current iteration of a loop and continue with the next one.
Example:
javascript
let i = 0;
while (i < 5) {
    if (i == 3) {
        i++;
        continue;
    }
    console.log(i);
    i++;
}