Loops in computer programs refers to a block of code that is executed over and over based on meeting some criteria or condition. However a loop can be broke or exited from inside the loop also. The block of code in a loop keeps on executing again and again unless it meets a certain condition or being exited explicitly from inside the loop. Thus, if not well-conditioned a loop might keep on running causing the program to fail ultimately.
Depending on the position of the control statement in the loop, loops can be of two types
- Entry Controlled
- Exit Controlled
Beside this, loops can be categorized in to two other categories called counter controlled loops and sentinel controlled loops based on the nature of the control variables and the kind of value assigned to them. You can lean more about counter and sentinel controlled loops here and learn more about the differences between counter and sentinel controlled loops here.
Entry controlled loop:
The types of loop where the test condition is stated before the body of the loop, are known as the entry controlled loop. So in the case of an entry controlled loop, the condition is tested before the execution of the loop. If the test condition is true, then the loop gets the execution, otherwise not. For example, the for loop is an entry controlled loop. In the given figure, the structure of an entry controlled loop is shown.
The types of loop where the test condition is stated at the end of the body of the loop, are know as the exit controlled loops. So, in the case of the exit controlled loops, the body of the loop gets execution without testing the given condition for the first time. Then the condition is tested. If it comes true, then the loop gets another execution and continues till the result of the test condition is not false. For example, the do statement or the do….while loop is an exit controlled loop. The structure of an exit controlled loop is given in the given figure.
Please follow and like us: