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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.