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...






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.