Difference Between Sentinel And Counter Controlled Loop In C

A loop that runs until it encounters a specific value known as the "sentinel" is termed as a sentinel-controlled loop. This value serves as the signal for the loop to stop executing.

Condition of Termination

The software continues to iterate within the uLoop until it reaches the specified sentinel value.

Characteristics:

There are several characteristics of the Sentinel Controlled Loop in C. Some main characteristics of the Sentinel Controlled Loop are as follows:

  • Flexibility: It is more versatile because the number of iterations in a sentinel-controlled loop is set by an external input .
  • Dynamic Termination: When the sentinel is spotted, the loop breaks and adjusts to new input.
  • Example: Reading input from the user until they input a particular character or number to indicate that their input is finished.
  • Example:

Let's consider a scenario to demonstrate the sentinel-controlled loop in the C programming language.

Example

#include <stdio.h>

int main()

{

    char input;

    printf("Enter characters (terminate with 's'): ");

    while (1) 

    {

        scanf(" %c", &input);

        if (input == 's') 

        {

            break;  

// Exit the loop when 's' is encountered

        }

        // Process the input

        printf("Entered: %c\n", input);

    }

    return 0;

}

Output:

Output

Enter characters (terminate with 's'): wrgs

Entered: w

Entered: r

Entered: g

Explanation:

  1. Header Inclusion
  2. include <stdio.h>: By adding the standard input-output library, developers can execute input and output tasks through functions such as printf and scanf.

  3. main function

int main {... }: The main function is where each C program commences and concludes. Subsequently, the execution process begins.

  1. Variable Declaration

A character variable named input is defined using the 'char input;' syntax. It is designated to hold the input provided by the user.

  1. Display Prompt.

printf("Prompt the user to input characters (end by typing 's'):");: This line of code displays a message on the console, asking the user to input characters. The user is advised to conclude the input by typing the character 's'.

  1. Iteration Construct

While the condition within the parentheses remains true, the code block enclosed by "while (1) {... }" will continuously execute, creating an infinite loop. This perpetual cycle occurs because the value 1 is inherently true, leading to an unending execution of the loop.

  1. User Input

The scanf("%c", &input) function allows the user to input a single character which is then stored in the input variable. Any initial whitespace, such as newline characters, in the input buffer is removed by the space preceding %c.

  1. Loop Exit Condition

If the input matches the character 's', the program will exit the loop by executing the break statement. This step is crucial in handling the process and displaying the input effectively.

The character entered is handled unless it is 's', at which point it is displayed in a message on the console using printf("Entered: %c\n", input).

  1. An Infinite Loop Mechanism

The while loop with a condition of (1) generates an endless iteration until the user inputs the character 's' to execute the break statement.

  1. Exit Command

return 0;: This statement signifies the conclusion of the main function and provides the operating system with an exit code of 0, indicating successful completion of the program.

In essence, this software employs a while loop to consistently retrieve characters from the user until the user inputs the character 's', triggering the loop to terminate. The characters entered into the loop are then handled and exhibited on the console.

What is the Counter-Controlled Loop?

A loop control variable, often referred to as a counter, is responsible for determining and controlling the number of iterations in a counter-controlled loop.

Termination Condition

The loop persists until the loop control variable reaches a specified value.

Characteristics:

There are several characteristics of the Counter Controlled Loop in C. Some main characteristics of the Counter Controlled Loop are as follows:

  • Fixed Iterations: Counter-controlled loops have a fixed and known number of iterations before the loop starts.
  • Predictability: The number of iterations is determined by the loop control variable's initial value, condition, and update.
  • Example: Executing a code block a fixed number of times known in advance.
  • Example:

Let's consider a scenario to demonstrate the counter-controlled loop concept in the C programming language.

Example

#include <stdio.h>

int main()

{

    int m;

    // Loop Looptes 5 times (i takes values 1 through 5)

    for (m = 1; m <= 5; m++)

    {

        // Process the iteration

        printf("Value of Iteration - %d\n",m);

    }

    return 0;

}

Output:

Output

#include <stdio.h>

int main()

{

    int m;

    // Loop Looptes 5 times (i takes values 1 through 5)

    for (m = 1; m <= 5; m++)

    {

        // Process the iteration

        printf("Value of Iteration - %d\n",m);

    }

    return 0;

}

Explanation:

  1. Inclusion of Header
  2. include: This line imports the standard input-output library, enabling the utilization of functions such as printf for displaying output.

  3. Primary function

{... } In the C programming language, each program typically begins with the int main function, acting as the primary entry point for execution.

  1. Variable Declaration

The integer variable m is defined using the syntax 'int m;'. The forthcoming loop employs this variable as a loop control entity.

  1. For Loop

for (m = 1; m <= 5; m++) {... }: It is a counter-controlled loop of the for type.

  • Initialization: 'm = 1;' this initializes the loop control variable m to 1.
  • Condition: The loop condition is '<= 5;'. As long as this condition holds, the loop will keep going.
  • Update: After each repetition, 'm++' increases the value of m by 1.
  1. Loop Body

The for loop iterates through the code contained within curly braces, {... }.

  • printf("The Iteration Value is - %d\n", m);: Each time this line is displayed, the variable m gets refreshed. The escape sequence \n signifies a newline, and ' %d' denotes an integer format.
  1. Return Statement

Exiting with a return value of 0 signifies the end of the primary function and informs the operating system that the program executed successfully.

In summary, this software goes through five iterations by employing a for loop mechanism. The loop assigns the value of the loop control variable m during each iteration. The loop control variable initiates at 1 and increments by 1 for every iteration, continuing until the value of the variable equals or surpasses 5.

Key Differences between sentinel and counter controlled loop:

There exist multiple key distinctions between the sentinel and counter controlled loop in C. Here are some primary variances between these loop types:

Features Sentinel Controlled Loop Counter Controlled Loop
Condition Checking Depends on a condition (sentinel value) variable for terminating the loop. Uses a counter or index variable for determining the iteration count.
Initialization Before using the sentinel value inside the loop, it must be initialised. Before entering the loop, the counter variable must be initialised.
Loop Control Variable Usually, modifications are made to the loop control variable inside the loop body. Usually, the loop control variable is increased or decreased at the beginning or end of the loop body.
Termination Criteria Loop terminates when the sentinel value is reached or a specific condition becomes true. Loop terminates when the counter variable reaches a specified value or based on a relational condition.
Examples while(q!=-1){//loop bodyscanf("%d", &q);} for(q=0;q<10;q++){//loop body}
Use Cases Suitable when the exact number of iterations is uncertain and depends on input or dynamic conditions. Suitable when the number of iterations is known or can be easily determined.

Input Required

This code uses input(). Please provide values below: