while and do-while loops
Repeat code in C with while and do-while loops — including condition testing, loop body execution, and the difference between the two forms.
- Write a while loop that repeats until a condition becomes false
- Write a do-while loop that executes the body at least once
- Identify infinite loops and the condition that terminates them
- Choose between while and do-while appropriately
Loops are what give programs their power to do large amounts of work. Without them, every line of code executes exactly once. With loops, a five-line program can process a million inputs. C has three loop constructs: while, do-while, and for. This lesson covers the first two.
The while loop
while (condition) {
/* body executes repeatedly while condition is non-zero */
}A concrete countdown:
#include <stdio.h>
int main(void) {
int count = 5;
while (count > 0) {
printf("%d\n", count);
count--;
}
printf("Liftoff!\n");
return 0;
}Output:
5
4
3
2
1
Liftoff!The structure is:
- Evaluate the condition: is
count > 0? - If true, execute the body.
- After the body, go back to step 1.
- If false, exit the loop.
The condition is tested before the body executes. If the condition is false from the start, the body never runs:
int x = -1;
while (x > 0) {
printf("This never prints\n"); /* condition false immediately */
}Updating the loop variable
The most common mistake with while loops is forgetting to update the variable that the condition tests:
int i = 0;
while (i < 10) {
printf("%d\n", i);
/* forgot: i++; */
}
/* This loop runs forever */Every while loop should have three things: an initialiser (before the loop), a condition (in the while), and an update (inside the body that eventually makes the condition false).
Summing with while
#include <stdio.h>
int main(void) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i++;
}
printf("Sum 1 to 100 = %d\n", sum); /* 5050 */
return 0;
}The do-while loop
do {
/* body executes at least once */
} while (condition);The difference: the condition is tested after the body, so the body always executes at least once.
#include <stdio.h>
int main(void) {
int n;
do {
printf("Enter a positive number: ");
/* In real code you'd use scanf here -- using a fixed value for the example */
n = 5; /* pretend the user typed 5 */
} while (n <= 0);
printf("You entered: %d\n", n);
return 0;
}The classic use for do-while is input validation: you always want to read input at least once before checking whether it is valid.
do-while is less common than while and for. Most loops should be written with while or for. Reach for do-while specifically when the loop body must run at least once — typically for reading user input or processing a stream where you need to read before you can test.
Infinite loops
A while (1) loop runs forever — the condition 1 is always non-zero:
while (1) {
/* infinite loop -- useful for event loops and servers */
/* must use break, return, or exit() to escape */
}Infinite loops are legitimate in systems programming: event loops, server request handlers, and embedded firmware often run forever by design. You exit them with break, return, or the exit() function from <stdlib.h>.
A practical example: digit sum
#include <stdio.h>
int main(void) {
int n = 12345;
int digit_sum = 0;
while (n > 0) {
digit_sum += n % 10; /* extract last digit */
n /= 10; /* remove last digit */
}
printf("Digit sum: %d\n", digit_sum); /* 15 */
return 0;
}This pattern — extract the last digit with % 10, then remove it with / 10 — comes up repeatedly in programming exercises.
Where to go next
Next: for loops — a loop form that keeps the initialiser, condition, and update in one place, which is cleaner for loops that count through a range.