[Javascript] Controlling code flow (1)

Controlling code flow

Conditional statement

When controlling the flow of our code, we have several options.

If we want to execute different blocks of code based on certain conditions, we can use if-else statement. These statements allow us to specify which code block should be executed based on carefully defined conditions.

Below, we see see cases of if-else statements. The code block that matches the condition will be executed. In the third case, we can create as many conditions as necessary using else-if.

// 1) The first case
if (condition) {
    // code to be executed if condition is true
}

// 2) The second case
if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}


// 3) The third case
if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition1 is false and condition2 is true
} else {
    // code to be executed if both condition1 and condition2 are false
}

Another option for controlling code flow is using the switch statement. The switch statement allows us to select code blcoks based on the value of an expression.

switch(expression) {
    case value1:
        // code to be executed if expression === value1
        break;
    case value2:
        // code to be executed if expression === value2
        break;
    default:
        // code to be executed if expression doesn't match any case
}

If we cannot think of sophisticated conditions, we can just easily select code block based on a value. The value of expression will determine which code block to executed.

Looping statment

Not just choosing a code block to be executed for a single time, we might want to a code block to be executed repeatedly. If so, we have several options: for, while, and do…while statements.

For example, the following piece of code shows how the code blocks are repeatedly executed.

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

// while statement
let i = 0;
while (i < 5) {
    console.log("Iteration number: " + i);
    i++;
}

// do while statement
let i = 0;
do {
    console.log("Iteration number: " + i);
    i++;
} while (i < 5);

// result:
// Iteration number: 0
// Iteration number: 1 
// Iteration number: 2
// Iteration number: 3
// Iteration number: 4

Conclusion

In this post, we covered how to control the flow of code execution using conditional and looping statements. Understanding these concepts is essential for writing efficient and effective programs.

In the next post, we will explore the use of labels in JavaScript, which can be used in conjunction with loops and conditional statements to create more complex control flows. Following that, we will delve into error handling using try-catch statements. Stay tuned for more in-depth discussions on these topics!

Leave a Reply

Your email address will not be published. Required fields are marked *