Loops are described in several ways: by where the condition is tested, by what controls the number of passes, and by whether that number is known beforehand. These labels overlap, and a single loop usually carries more than one of them.
Entry-Controlled and Exit-Controlled Loops
Definition: Entry-controlled loop. A loop whose condition is checked before the body is carried out, so the body may run zero times.
Definition: Exit-controlled loop. A loop whose condition is checked after the body is carried out, so the body runs at least once.
In C, for and while are entry-controlled, and do-while is exit-controlled. An exit-controlled loop suits tasks such as displaying a menu, where the body must be performed once before any decision to repeat can be made.
Counter-Controlled Repetition
In counter-controlled repetition, a variable called a counter records the number of passes. The counter is given a starting value, tested against a limit and changed by a fixed amount on every pass. Displaying the first ten natural numbers is a counter-controlled task.
Definite and Indefinite Repetition
Definition: Definite repetition. Repetition in which the number of passes is known before the loop begins.
Definition: Indefinite repetition. Repetition in which the number of passes is not known in advance and depends on a condition or on the data entered.
Adding the marks of exactly 60 students is definite repetition. Reading numbers until the user enters a negative one is indefinite, because nobody can say beforehand how many numbers will be entered.
Sentinel-Controlled Repetition
Definition: Sentinel. A special input value, different from all valid data, that signals the end of the data.
A sentinel-controlled loop keeps reading values until the sentinel appears. The sentinel must be a value that can never occur as real data. For marks from 0 to 100, the value −1 is a suitable sentinel. The sentinel is a signal only; it must not be processed as data. Table 2.4 brings the types together.
Table 2.4: Types of repetition
| Type | Distinguishing feature | Example in C |
|---|---|---|
| Entry-controlled | Condition tested before the body | while, for |
| Exit-controlled | Condition tested after the body | do-while |
| Counter-controlled | A counter decides the passes | for (i = 1; i <= 10; i++) |
| Definite | Number of passes known in advance | Sum of 60 marks |
| Indefinite | Number of passes not known in advance | Read until a condition fails |
| Sentinel-controlled | A special value ends the input | Read marks until −1 |
Example 2.4: Sum with a sentinel
Problem: Read marks one at a time until −1 is entered, and display their sum.
Solution:
- Read the first mark before the loop.
- While the mark is not −1, add it to the sum and read the next mark.
- Display the sum after the loop.
#include <stdio.h>
int main(void)
{
int mark, sum = 0;
printf("Enter marks (-1 to stop):\n");
scanf("%d", &mark);
while (mark != -1)
{
sum = sum + mark;
scanf("%d", &mark);
}
printf("Sum = %d\n", sum);
return 0;
}
Input:
45 60 72 -1
Output:
Enter marks (-1 to stop):
Sum = 177
Result: The sum is 45 + 60 + 72 = 177. The sentinel −1 stopped the loop and was not added. This loop is entry-controlled, indefinite and sentinel-controlled at the same time.
Common mistakes
- Adding the sentinel value to the total as though it were data.
- Choosing a sentinel that can also occur as valid data, such as 0 for marks.
- Using a do-while loop where the body must not run when the very first value is the sentinel.
Key takeaways
- Entry-controlled loops test before the body; exit-controlled loops test after it.
- A counter-controlled loop is governed by a counter with a start, a limit and a step.
- Definite repetition has a known number of passes; indefinite repetition does not.
- A sentinel ends the input and is never processed as data.
Practice questions
- Classify the do-while loop as entry-controlled or exit-controlled.
- Why is 0 a poor sentinel for a list of marks?
- Is "display the multiplication table of 7 up to 10" definite or indefinite repetition?
Answers to the practice questions
- Exit-controlled, because its condition is tested after the body.
- A student may actually score 0, so 0 can occur as valid data.
- Definite, because exactly 10 passes are needed and this is known in advance.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II
