Loops that stop when the data says so
When you do not know the count in advance, the loop condition comes from the data — and do-while exists for the case where the body must run at least once.
After this lesson
You should be able to
- Write a sentinel-controlled loop that stops on a marker value.
- Explain when do-while is the right choice over while.
- Trace a nested loop and count its total passes.
Sentinel-controlled loops
Often you cannot know how many values a user will enter. Instead of a count, you agree on a sentinel — a value that cannot be real data and therefore means stop. For marks out of 100, -1 works, because no genuine mark is negative.
Read the first value before the loop, then read the next value at the end of the body. This ordering means the sentinel is tested before it is ever treated as data — get it wrong and -1 gets added to your total.
int mark, total = 0, count = 0;
printf("Enter marks, -1 to finish: ");
scanf("%d", &mark);
while (mark != -1) {
total = total + mark;
count = count + 1;
scanf("%d", &mark);
}do-while runs first, asks later
while tests before the first pass, so a body may run zero times. do-while tests after, so the body always runs at least once. Menus and input validation are the natural fit: you must show the menu once before you can know whether the choice was valid.
Note the semicolon after the closing while — do { ... } while (cond); requires it. Leaving it out is a confusing syntax error because the compiler blames the following line.
int choice;
do {
printf("1 Add 2 View 3 Exit\n");
scanf("%d", &choice);
} while (choice < 1 || choice > 3);Nested loops multiply
A loop inside a loop runs the inner one completely for every single pass of the outer one. Outer 4 passes with inner 3 passes means 12 executions of the inner body — nested counts multiply, they do not add.
The inner counter must be reset for each outer pass. With for, its initialisation does that automatically; with while, you must reset it yourself inside the outer body, and forgetting is why the second row of a pattern often comes out empty.
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= row; col++)
printf("* ");
printf("\n");
}Try it yourself
Keep asking for a positive number until the user gives one, then print a multiplication table for it from 1 to 10.
Need a hint?
One do-while for the validation, one for loop for the table.
Check the worked solution
Validation must run at least once — you cannot judge input you have not read — so do-while is the honest structure here, not a stylistic preference. The table count is known in advance, so for fits. %2d and %3d keep the columns aligned as the products grow into two and three digits.
#include <stdio.h>
int main(void)
{
int n;
do {
printf("Enter a positive number: ");
scanf("%d", &n);
} while (n <= 0);
for (int i = 1; i <= 10; i++)
printf("%d x %2d = %3d\n", n, i, n * i);
return 0;
}Quick check
How many times does the inner printf run in for (i=1;i<=3;i++) for (j=1;j<=4;j++) printf("*");?
Why this lesson exists
Syllabus mapping
Conditional Loops · Loop Design · Nested Loops · do-while Statement
Maps to course outcomes CO1, CO2, CO3.