C while Loop and do-while
while repeats its body as long as the condition in the parentheses is true. Use it when you don't know the number of repetitions in advance, only "until something happens". There is also do-while, which checks the condition afterwards, so it runs at least once.
while (condition) {
statements to repeat
}
do {
statements to repeat
} while (condition);
Repeating while a condition is true
The condition is checked before each pass. If it is true, the body runs and then it goes back to the condition. The body has to change a value used in the condition, or the loop never ends.
#include <stdio.h> int main(void) { int money = 1000, years = 0; while (money < 2000) { money += money / 10; years++; } printf("%d years, %d\n", years, money); return 0;}8 years, 2142
This counts how many years it takes to pass 2000 when the amount grows by 10% every year. You can't know in advance how many passes it takes, so while fits better than for.
Reading until an end marker
When the input doesn't give a count and the rule is "0 means the end", read it like this: read one value first, process values while they are not the end marker, and read the next value at the end of the body.
#include <stdio.h> int main(void) { int x, sum = 0, count = 0; scanf("%d", &x); while (x != 0) { sum += x; count++; scanf("%d", &x); } printf("%d %d\n", count, sum); return 0;}5 8 2 0 9
3 15
The 9 after the 0 is not read. If the first value is 0, the body doesn't run even once.
do-while: at least once
do { ... } while (condition); runs the body once first and then checks the condition. It suits jobs like "try it, and go once more if that wasn't enough". Don't forget the semicolon at the end.
#include <stdio.h> int main(void) { int x, total = 0, tries = 0; do { scanf("%d", &x); total += x; tries++; } while (total < 10); printf("%d tries, total %d\n", tries, total); return 0;}4 3 5 9
3 tries, total 12
It reads numbers until the total is 10 or more. The first number has to be read whatever the condition says, so do-while is the natural choice.
while (1) and break
When the stopping condition has to be checked in the middle of the loop, make an infinite loop with while (1) and leave it with break. In C, 1 is true, so the condition is always true.
#include <stdio.h> int main(void) { int n, steps = 0; scanf("%d", &n); while (1) { printf("%d\n", n); if (n == 1) break; if (n % 2 == 0) n /= 2; else n = 3 * n + 1; steps++; } printf("steps: %d\n", steps); return 0;}6
6 3 10 5 16 8 4 2 1 steps: 8
It keeps halving an even number, or tripling an odd number and adding 1, until the number becomes 1. for and while can replace each other. Pick for when there is a clear count or index, and while when there is only a condition.
Common mistakes
Not changing the value used in the condition
int i = 0;
while (i < 3) {
printf("%d\n", i);
/* forgot i++ */
}
→ i stays 0, so 0 is printed forever (an infinite loop)
The body has to change a value so that the condition becomes false at some point. Be careful with continue too. If continue comes before i++, the increment is skipped and the loop fails to stop in the same way.
Putting a semicolon after while (condition)
int i = 0;
while (i < 3); /* it gets stuck here */
{
printf("%d\n", i);
i++;
}
→ Nothing is printed and the program never ends
The semicolon becomes the body (an empty statement), so the condition is checked forever while i never changes. A semicolon goes only at the end of a do-while.
Using while for something that must happen at least once
#include <stdio.h>
int main(void) {
int n = 0, digits = 0;
while (n > 0) {
digits++;
n /= 10;
}
printf("%d\n", digits);
return 0;
}
→ 0, although 0 is a one-digit number
When n is 0 the condition is false from the start, so the loop doesn't run even once. Written as do { digits++; n /= 10; } while (n > 0);, it counts 0 as one digit too.