Home »
C Popular & Interesting Questions
What is the Difference between Sentinel and Counter Controlled Loop in C?
Sentinel and Counter Controlled Loop in C
Previously we have learned about what is the difference between Entry and Exit Controlled Loops in C? According to logic of solving the problem, we use two type of looping logics - 1) Sentinel Controlled Loop and 2) Counter Controlled Loop.
Counter Controlled Loop
When we know how many times loop body will be executed known as Counter Controlled Loop, for example - print natural numbers from 1 to 100, such kind of problem will be solved using counter controlled loop.
Consider the code snippet
int count;
for( count=1; count<=100; count++)
printf("%d",count);
Sentinel Controlled Loop
When we don’t know exactly know how many times loop body will be executed known as Sentinel Controlled Loop, for example - Reverse a given number, such kind of problem will be solved using sentinel controlled loop.
Consider the code snippet
int reverseNumber=0;
int number=12345;
while(number>0)
{
reverseNumber= (reverseNumber*10)+ number%10;
number/=10;
}
printf("Reverse Number is: %d\n", reverseNumber);
Related topics...