 # What are the counter and sentinel controlled loops?

Based on the nature of the control variable and the condition or limit, the loops can be of two general categories; counter controlled and sentinel controlled loops. Counter controlled loops are where we know the number of loop executions beforehand. Unlike counter controlled loops, in sentinel controlled loops the number of loop executions in unknow at the beginning and depends on decisions made inside the loop.

## Counter controlled loop

Counter controlled loops are those loops where we know the number of the executions in advance. The control variable is known as counter. In this loop, we set the value of the counter and max limit or condition beforehand.

### Example

```int sum = 0;
int n = 1;

while (n <= 10){
sum = sum + n*n;
n = n+ 1;
}
```

In the above example, the loop executes exactly 10 times for n = 1,2,3,……,10. The initial value of the counter `n` is 1, and it increments by 1 in each loop. When it reaches 11, the loop breaks as the condition breaks.

## Sentinel controlled loop

Sentinel controlled loop are those loop where where we do not know the number of execution in advance. In this case, the value of the control variable differs within a limitation and the execution can be terminated at any moment. The control variable in this case is termed by sentinel variable. The value of this variable changes inside the loop. The loop breaks when the value of the control variable matches the condition.

### Example

```do{
printf("Input a number.n");
scanf("%d", &num);
}while(num > 0);
```

In the above example, the loop executes till the entered value of the variable `num` is not 0 or less then 0. This is a sentinel controlled loop and here the variable `num` is a sentinel.

### 3 thoughts on “What are the counter and sentinel controlled loops?”

1. Good that's nice definition with excellent example.