DEV Community

Cover image for LOOPING
Vinoth Kumar
Vinoth Kumar

Posted on

LOOPING

LOOP:

when we need a specific task in repetition. They're essential as they reduce hours of work to seconds.we will explore the basics of loops, with the different types and best practices.

TYPES OF LOOP:

  1. Entry-Controlled loops.
  2. Exit-Controlled loops.

ENTRY CONTROLLED LOOPS:
The test condition is checked before entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.

FOR LOOP:

It's a control flow structure that iterates over a sequence of elements, such as a range of numbers, items in a list, or characters in a string.The loop is entry-controlled because it determines the number of iterations before entering the loop.

let OUTPUT = '';
for (let i = 0; i < 5; i++) {
    OUTPUT += i + ' ';
}
console.log(OUTPUT);
Enter fullscreen mode Exit fullscreen mode

WHILE LOOP:

It's is an entry-controlled control flow structure that repeatedly executes a block of code as long as a specified condition is true.The loop continues to iterate while the condition remains true, and it terminates once the condition evaluates to false.

    let i = 0;
    while (i < 5) {
        process.stdout.write(i + " ");
        i++;
Enter fullscreen mode Exit fullscreen mode

EXIT CONTROLLED LOOPS:
The test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. Do-while Loop is an example of Exit Controlled loop.

Do-While Loop:
It's an exit-controlled control flow structure that executes a block of code at least once and then repeatedly executes the block as long as a specified condition remains true.
The distinctive feature of a do-while loop is that the condition is evaluated after the code block, ensuring that the block is executed at least once, even if the condition is initially false.

NESTED LOOP:
when one loop is placed inside another. This allows for the iteration of one or more loops within the body of another loop. Each time the outer loop executes, the inner loop completes its full iteration.
Nested loops are commonly employed for tasks involving multidimensional data processing, pattern printing, or handling complex data structures.

Top comments (0)