Show contents
Chapter 4 of 6 · 20 min
The while and do while loops
By the end of this chapter, you'll be able to
- Write a while loop that stops at the right moment
- Follow how variables change on each round of a loop
- Use do while when the block must run at least once
- Recognise and fix an infinite loop
while: as long as…
#include <stdio.h>
int main(void) {
double savings = 50000;
int years = 0;
while (savings < 100000) {
savings = savings * 1.05;
years++;
}
printf("Doubled in %d years: %.0f FCFA\n", years, savings);
return 0;
}
Doubled in 15 years: 103946 FCFA
The while loop works like this:
- it evaluates the condition;
- if it's false, the loop ends and the program carries on after the closing brace;
- if it's true, it runs the block, then goes back to step 1.
Each pass through the block is called an iteration, or a round of the loop. If the condition is false from the start, the block never runs.
The program carries on after the loop.
Text description of the animation
- A countdown from 3 to 1, then lift-off.
- n is 3.
- The condition is true: we enter the loop.
- First round: we display 3...
- n-- subtracts 1: n is 2. End of the block, back up to the condition.
- Still true: second round.
- We displayed 2..., and n becomes 1.
- Third round: 1... is displayed and n becomes 0.
- The condition is false: we jump past the closing brace.
- The program carries on after the loop.
do while: at least once
Sometimes you need to run the block before you can test the condition. The typical case is input: you ask for a value, then ask again as long as it's incorrect.
#include <stdio.h>
int main(void) {
int grade = -1;
do {
printf("Grade out of 20: ");
if (scanf("%d", &grade) != 1) {
return 1;
}
} while (grade < 0 || grade > 20);
printf("\nGrade recorded: %d\n", grade);
return 0;
}
25
-3
14
Grade out of 20: Grade out of 20: Grade out of 20:
Grade recorded: 14
The block runs first, then the condition is tested. Notice the semicolon after while (…): it's mandatory in a do while.
Your turn
Worked exercise
Worked exercise: the sum of the digits
Write a program that computes the sum of the digits of n = 4721 using a while loop (4 + 7 + 2 + 1 = 14). It must display Sum of the digits: 14.
Hint 1
n % 10 gives the last digit (1), and n / 10 removes that last digit (472).Hint 2
n > 0: add n % 10 to the sum, then replace n with n / 10.Show the solution and common mistakes
#include <stdio.h>
int main(void) {
int n = 4721;
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
printf("Sum of the digits: %d\n", sum);
return 0;
}
Sum of the digits: 14
Common mistakes:
- Forgetting
n /= 10;:nnever decreases and the loop is infinite. - Writing
while (n >= 0): whennis 0,0 / 10is still 0, and the loop never stops.
Summary
- 1while (condition) { … } repeats the block as long as the condition is true, testing it before each round.
- 2do { … } while (condition); runs the block at least once (don't forget the final ;).
- 3Each round must bring the condition closer to the exit, otherwise the loop is infinite.
- 4Ctrl + C stops a stuck program in the terminal.
- 5% 10 and / 10 let you go through the digits of a number.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.