Control Flow

Control flow statements are used to dictate the order in which code is executed. They allow for decision-making, repetition, and branching within your programs.

Conditional Statements

Conditional statements execute code blocks based on whether a specified condition is true or false.

if Statement

The if statement executes a block of code if a specified condition is true.


if (condition) {
    // Code to execute if condition is true
}
        

if...else Statement

The if...else statement executes one block of code if the condition is true, and another block if it is false.


if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}
        

if...else if...else Statement

This allows for multiple conditions to be checked sequentially.


if (condition1) {
    // Code if condition1 is true
} else if (condition2) {
    // Code if condition2 is true
} else {
    // Code if all previous conditions are false
}
        

switch Statement

The switch statement is used to perform different actions based on different conditions. It is often a cleaner alternative to a long series of if...else if statements when checking the value of a single variable.


switch (variable) {
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    default:
        // Code if no match
}
        

Note on break

The break statement is crucial within each case to prevent "fall-through" to the next case. If break is omitted, execution will continue to the next case's code block.

Looping Statements

Looping statements allow you to execute a block of code repeatedly.

for Loop

The for loop is used to execute a block of code a specific number of times.


for (initialization; condition; increment) {
    // Code to execute in each iteration
}
        

Example:


for (let i = 0; i < 5; i++) {
    console.log("Iteration: " + i);
}
            

while Loop

The while loop executes a block of code as long as a specified condition is true.


while (condition) {
    // Code to execute while condition is true
}
        

Example:


let count = 0;
while (count < 3) {
    console.log("Count is: " + count);
    count++;
}
            

do...while Loop

The do...while loop is similar to the while loop, but it guarantees that the code block will be executed at least once, regardless of the condition.


do {
    // Code to execute at least once
} while (condition);
        

Example:


let num = 5;
do {
    console.log("This will print at least once.");
    num++;
} while (num < 5);
            

for...in Loop

The for...in loop iterates over the enumerable properties of an object.


for (const key in object) {
    // Access object[key]
}
        

for...of Loop

The for...of loop iterates over the values of an iterable object (like arrays, strings, Maps, Sets).


for (const element of iterable) {
    // Access element
}
        

Example:


const colors = ["red", "green", "blue"];
for (const color of colors) {
    console.log(color);
}
            

Branching Statements

Branching statements alter the normal flow of execution within loops or statements.

break Statement

The break statement terminates the current loop or switch statement.

Tip:

Use break to exit a loop early when a certain condition is met, improving efficiency.

continue Statement

The continue statement skips the rest of the current iteration of a loop and continues with the next iteration.

Example:


for (let i = 0; i < 5; i++) {
    if (i === 2) {
        continue; // Skips printing 'Iteration: 2'
    }
    console.log("Iteration: " + i);
}
            

Mastering control flow is fundamental to writing dynamic and responsive applications. Experiment with these statements to understand their behavior thoroughly.