Difference between Sentinel and Counter Controlled Loop in C
Last Updated :
25 Jan, 2023
Sentinel Controlled Loop
A sentinel controlled loop is also called an indefinite repetition loop because the number of iterations is not known before the loop starts executing. In a sentinel controlled loop, a special value called sentinel value is used to change the loop control expression from true to false in order to determine whether to execute the loop body. Sentinel controlled loop is useful when we don't know in advance how many times the loop will be executed. An example of a sentinel controlled loop is the processing of data from a text file of unknown size. Below is the program to illustrate sentinel controlled loop in C:
C
// The following program does the work
// of finding a length of a string using
// sentinel controlled loop
#include <stdio.h>
// Function to find the
// length of the string
void lengthOfString(char* string)
{
int count = 0, i = 0;
char temp;
// Pointer to string
temp = string[0];
// Iterate till temp points to NULL
while (temp != '\0') {
count++;
i++;
temp = string[i];
}
// Print the length of the string
printf("The length of string is %d",
count);
}
// Driver Code
int main()
{
// Given String
char string[] = "GeeksForGeeks";
// Function Call
lengthOfString(string);
return 0;
}
Output:The length of string is 13
Counter Controlled Loop
A counter controlled loop is also known as definite repetition loop, since the number of iterations is known before the loop begins to execute. The counter-controlled loop has the following components:
- a control variable.
- the increment (or decrement)value by which the control variable is modified at each iteration of the loop.
- the loop terminating condition that checks if looping should continue.
Since the counter controlled loop is controlled by a counter value, at each iteration counter value will increase or decrease with a definite value and condition will be checked, so the number of loop execution becomes definite. Any task involving definite iteration can be solved using a counter controlled loop for example printing the first 10 natural numbers.
C
// Program to print first K
// natural numbers
#include <stdio.h>
// Function to print the Natural Number
void printNaturalNumbers(int K)
{
int i = 1;
while (i <= K) {
// Print the number i
printf("%d ", i);
i++;
}
return;
}
// Driver Code
int main()
{
// Given Number N
int N = 5;
// Function Call
printNaturalNumbers(N);
return 0;
}
The main difference between Sentinel and Counter Controlled Loop in C is that in a Sentinel Controlled Loop, exactly how many times loop body will be executed is not known and in a Counter Controlled Loop, how many times loop body will be executed is known.
Difference between Sentinel and Counter Controlled Loop in C is given below:
S.NO. | BASIS OF COMPARISON | SENTINEL CONTROLLED LOOP | COUNTER CONTROLLED LOOP |
---|
1. | Definition | A sentinel controlled loop is the indefinite repetition loop as the number of repetitions is not known before the loop begins executing | A counter controlled loop is the definite repetition loop as the number of repetitions is known before the loop begins executing |
2. | Controlling variable | Controlled variable used is known as sentinel variable. | Controlled variable used is known as counter. |
3. | Number of iteration | Unknown number of iteration | Known number of iteration. |
4. | Value of variable | The value of the variable is not strict and it varies. | The value of the variable is strict. |
5. | Limitation of variable | The Limitation of the variable is strict. | The Limitation of the variable is strict also. |
6. | Example | A do....while loop is an example of sentinel controlled loop. | A while loop is an example of counter controlled loop. |
Similar Reads
Difference between for and while loop in C, C++, Java In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let's understand the basic differences between a for loop and a while loop. for Loop A for loop prov
5 min read
Difference between while and do-while loop in C, C++, Java while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax :while (boolean condition){ loop statements...}Flowchart:Example:C++ #include <iostream> usin
2 min read
Difference between for and do-while loop in C, C++, Java for loop: for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. Syntax: for (initialization condition; testing con
2 min read
Difference Between Counting and Binary Semaphores Semaphores are signaling devices used in computer systems to facilitate the control of access to common resources. For it is employed to prevent several processes (or programs) from conflicting with one another when all of them are competing for the same resource. There are two main types of semapho
5 min read
Difference between Semaphore and Condition Variable 1. Semaphore :Semaphore, as name suggests, is basically an object that includes counter, waiting list of process and supports two different operations i.e., wait and signal. Its type includes counting semaphores and binary semaphores. It is simply a synchronization tool that can be used to deal with
2 min read