Loops

In a loop, a program executes a sequence of statements many times until a specified condition is satisfied. A loop consists of two parts, a body (the sequence of statements needed to be executed repeatedly) and a control statement (which determines how many times or under what conditions the execution must be repeated, or stopped).

Depending upon the position of a control statement in a program, a loop is classified into two types:

  • Entry controlled
    In an entry-controlled loop, a condition is checked before executing the body of a loop. In such a loop, the body may never be executed.

  • Exit controlled
    In an exit controlled loop, a condition is checked, after executing the body of a loop. In such a loop, the body is executed at least once.

The control conditions must be well defined or the loop will go on executing forever. Such a loop is known as an infinite loop, and it causes the program to "hang", meaning it will never proceed with any further execution, and will have to be terminated by force (using an option that most operating systems have built in, usually pressing the keys Ctrl + C togther).

Loop Constructs

There are three main types of loop constructs:

  • while loop

This is an entry-controlled loop where the condition is evaluated before processing the body of the loop. If the condition is true, the body is executed. After the body is executed, control goes back to the beginning, and the condition is checked again. If it is still true, the body is executed again. This loop continues until the condition becomes false, changed by a statement in the body. Once the condition becomes false, the program exits the loop. It is important that the body must change the condition so that it will eventually become false.

After exiting the loop, the program starts execution from the statement immediately after the loop. In a while loop, if the condition is false the first time it is evaluated, its body will not be executed not even for once.

This is how a while loop is written:

let i = 0;
while(i < 10){
  console.log(i); // Will print the value of i from 0 to 9.
  i = i + 1;
}
 
// If you change the control variable such the exist condition is met right away
// the loop will execute only once.
let i = 10;
while(i < 10){
  console.log(i); // Will never reach this instruction.
  i = i + 10;
}
  • do-while loop

This is an exit-controlled loop where the condition is evaluated after processing the body of the loop once. After the body is executed once, then the condition is evaluated. If the condition is true, then it will execute the body again, else control is transferred out of the loop.

This is how a do-while loop is written:

let i = 0;
do{
  console.log(i); // Will print the value of i from 0 to 9.
  i = i + 1;
}while(i < 10)
 
// If you change the control variable such the exist condition is met right away
// the loop will execute only once.
let i = 0;
do{
  console.log(i); // Will print the value of i as 0 only once.
  i = i + 10;
}while(i < 10)
  • for loop

This loop runs for a specified number of times, in contrast to a while or a do-while loop that will execute as many times as required until the exit condition is satisfied. A for loop sets an initial value for the control variable, the condition for the loop to exit, and a change to the control variable so that the exit condition is reached. It is possible to set the initial value and the change to the control variable such that the exit condition is never met even in a for loop resuling in an infinite loop.

for loops are limited to running for a specified number of times, whereas while loops allow for more complex exit conditions to be specified. The test condition in a while loop could even include checking for external inputs received by the program. It is handy and necessary in solutions that receive signals from sensors, such as an IoT solution, and a specific sensor input could be an exit condition for a loop.

This is how a for loop is written:

// i++ is a short alternative to i = i + 1 which is incrementing the control variable by 1.
for(let i = 0; i < 10; i++){
	console.log(i); // Will print the value of i from 0 to 9.
}
 
// This is an infinite loop. If you do try it, Ctrl-C should stop the program.
// But be careful, you may end up needing to restart your computer.
for(let i = 10; i > 0; i++){
	console.log("In an infinte loop...");
}