Difference between Sentinel and Counter Controlled Loop in C - GeeksforGeeks

12 May, 2022
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 stringvoid 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 Codeint main(){    // Given String    char string[] = "GeeksForGeeks";     // Function Call    lengthOfString(string);    return 0;}
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.
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 Numbervoid printNaturalNumbers(int K){    int i = 1;    while (i <= K) {         // Print the number i        printf("%d ", i);        i++;    }    return;} // Driver Codeint main(){    // Given Number N    int N = 5;     // Function Call    printNaturalNumbers(N);    return 0;}
1 2 3 4 5
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:
C-Loops & Control Statements
loop
C Language
Difference Between
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Exception Handling in C++
'this' pointer in C++
Multithreading in C
Arrow operator -> in C/C++ with Examples
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Difference between var, let and const keywords in JavaScript
Differences between IPv4 and IPv6