Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

Home » C Popular & Interesting Questions

What is the Difference between Entry Controlled and Exit Controlled Loop in C?

Learn: What is Entry Controlled and Exit Controlled loops in C/C++ programming language, what are the differences between them?

Entry and Exit Controlled Loop in C

Loops are the technique to repeat set of statements until given condition is true. C programming language has three types of loops - 1) while loop, 2) do while loop and 3) for loop.

These loops controlled either at entry level or at exit level hence loops can be controlled two ways

  1. Entry Controlled Loop
  2. Exit Controlled Loop

Entry Controlled Loop

Loop, where test condition is checked before entering the loop body, known as Entry Controlled Loop.

Example: while loop, for loop

Exit Controlled Loop

Loop, where test condition is checked after executing the loop body, known as Exit Controlled Loop.

Example: do while loop

Consider the code snippets

Using while loop

int count=100;
while(count<50)
printf("%d",count++);

Using for loop

int count;
for(count=100; count<50; count++)
printf("%d",count);

In both code snippets value of count is 100 and condition is count<50, which will check first, hence there is no output.

Using do while loop

int count=100;
do
{
printf("%d",count++);
}while(count<50);

In this code snippet value of count is 100 and test condition is count<50 which is false yet loop body will be executed first then condition will be checked after that. Hence output of this program will be 100.

Differences Table

Entry Controlled Loop Exit Controlled Loop
Test condition is checked first, and then loop body will be executed. Loop body will be executed first, and then condition is checked.
If Test condition is false, loop body will not be executed. If Test condition is false, loop body will be executed once.
for loop and while loop are the examples of Entry Controlled Loop. do while loop is the example of Exit controlled loop.
Entry Controlled Loops are used when checking of test condition is mandatory before executing loop body. Exit Controlled Loop is used when checking of test condition is mandatory after executing the loop body.

Related topics...



Sponsored Links






COMMENTS