Difference Between Entry Controlled Loop and Exit Controlled Loop

In this article, we’ll be discussing the meaning of loop and also see the difference between Entry Controlled Loop and Exit Controlled Loop with program example.

Entry vs exit controlled loop

What is a Loop?

A loop empowers repetitive execution of a certain piece of code until a given condition turns out to be false.

In simple terms, it enables us to run a particular sequence of statements repeatedly based on a certain condition.

Generally a looping process involves:

  • Control variable initialization
  • Condition Evaluation
  • Loop body execution
  • Control Variable Updation

There are mainly three types of loops:

1) While Loop : loops through a piece of code as long as a specified expression is evaluated as true
2) Do While Loop : similar to while loop, except Do-While loop is guaranteed to execute at least one time
3) For Loop : executes loop body for specific number of times ( allows control over number of iterations)

In programming, these are considered as controlled statements that can regulate the flow of the program execution.

They use a conditional expression in order to decide to what extent a particular block should be repeated.

Every loop consists of two sections, loop body and control statement.

A control statement is a condition that dictates how long a loop would be iterated and the loop body is a block that contains the intended iterative statements.

In case of While Loop :

while(condition)    // Control Statement
  {
   // Loop Body
  }

In case of Do-While Loop :

do{
   // Loop Body      
  }while(condition);   // Control Statement

In case of For Loop :

for(controlVariableInitialization ;condition; increment/decrement)   // Control Statement
    {
     // Loop Body
    }

 

Based on the position of these two sections, loop execution can be handled in two ways that are at the entry-level and exit level.

So, loops can be categorized into two types:

Entry controlled loop: When a condition is evaluated at the beginning of the loop.
Exit controlled loop: When a condition is evaluated at the end of the loop.

Also Read: Difference between While and Do-While Loop

Entry Controlled Loop

An entry control loop checks condition at entry level (at beginning ), that’s why it is termed as entry control loop.

It is a type of loop in which the condition is checked first and then after the loop body executed. For loop and While loop fall in this category.

If the test condition is true, the loop body would be executed otherwise, the loop would be terminated.

 

Entry control loop flow chart

 

Execution Flow of Entry Control Loop

  1. The control variable is initialized first and acts as a base for comparison
  2. This variable is then evaluated with a conditional statement.
  3. If the condition is evaluated as false, the loop would be terminated, otherwise
  4. The loop body would be executed and go to the next iteration, where the variable would get updated with new incremented/ decremented value
  5. Repeat 2nd to 4th steps until the condition is said to be false.

Program to show the working of entry controlled loop:

#include<stdio.h>
void main()
{
int i=10;
while(i<10)
{
printf("I will not be executed as it is entry controlled loop");
i++;
}
getch();
}

Explanation:

In the above code, No output would be displayed as the condition is false. We used ‘i’ as a counter variable and assigned it 10.  Loop condition ‘i<10 ‘got evaluated as false . As a result, the compiler transferred the control to the next statement after the loop.

Exit Controlled Loop

An exit control loop checks condition at exit level (in the end ), that’s why it is termed as exit control loop. Oppose to Entry controlled loop, it is a loop in which condition is checked after the execution of the loop body. Do-While loop is the example.
The loop body would be executed at least once, no matter if the test condition is true or false.

Exit control loop flow chart

 

Execution Flow of Exit Control Loop

  1. The control variable is initialized first and acts as a base for comparison
  2. Then, the loop body is executed and the variable would get also updated with new incremented/ decremented value
  3. This variable is then evaluated with a conditional statement.
  4. If the condition is evaluated as false, the loop would be terminated, otherwise
  5. It repeats, 2nd to 4th steps until the condition is said to be false.

Program to show the working of exit controlled loop:

#include<stdio.h>
void main()
{
  int i=10;
  do
  {
  printf("I will be executed at once as it is exit controlled loop");
  i++;
  }while(i<10);
  getch();
}

Explanation:

In the above code, print statement would be executed at least once even though condition is false.

We used ‘i’ as counter variable and assigned it 10. In this, loop body got executed first as a result a message got printed on screen. After that, condition was checked which evaluated as false, this terminated the loop.

Difference between entry controlled loop and exit controlled loop in Tabular form ?

BasisENTRY CONTROLLED LOOPEXIT CONTROLLED LOOP
Execution FlowA loop in which the given condition is checked first, before entering the loop bodyA loop in which the loop body is executed first and then after the given condition is checked
Condition EvaluationThe loop body would be executed, only if the given condition is trueThe loop body would be executed at least once, even if the given condition is evaluated as false
ExampleFor Loop and While Loop are examples of this type of loopDo While Loop is an example of exit controlled loop
UseIt is used when condition evaluation is mandatory before executing loop bodyIt is used when one requires to iterate through loop body at least once before condition evaluation

Key Differences

  1. Exit control loop always executes at least once, regardless of condition. But, the entry control loop only executes if and only if the condition is evaluated as true.
  2. In this type of loop, body execution comes first, whereas in, entry control loop condition expression always comes first.
  3. Do-While falls under the exit control loop, whereas For loop and While loop falls under entry control loop

Leave a Reply