Loops: for, while and do-while
Loops run the same block several times. for suits a known number of steps, while repeats until a condition fails, and do-while always runs its body at least once.
What is Loops: for, while and do-while?
Loops run the same block several times. for suits a known number of steps, while repeats until a condition fails, and do-while always runs its body at least once.
Repeat work with for, while and do-while loops, plus break and continue.
Important points
- Compile with warnings enabled and fix every warning.
- Know the lifetime and type of every value.
- Validate input and keep array bounds explicit.
See how it works
Press Step to run the loop one move at a time. Watch the condition being checked, the body running and the trace table growing row by row.
C and C++ code examples
#include <stdio.h>
int main(void) {
/* for: start; condition; step */
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
/* while: repeat as long as the condition holds */
int n = 20;
int steps = 0;
while (n > 1) {
n /= 2;
steps++;
}
printf("halved %d times\n", steps);
/* do-while: the body runs at least once */
int tries = 0;
do {
tries++;
} while (tries < 3);
printf("tries = %d\n", tries);
/* break leaves the loop, continue skips to the next round */
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue;
if (i > 7) break;
printf("%d ", i);
}
printf("\n");
return 0;
}
1 2 3 4 5
halved 4 times
tries = 3
1 3 5 7
#include <iostream>
#include <vector>
int main() {
// for: start; condition; step
for (int i = 1; i <= 5; i++) {
std::cout << i << ' ';
}
std::cout << '\n';
// while: repeat as long as the condition holds
int n = 20;
int steps = 0;
while (n > 1) {
n /= 2;
steps++;
}
std::cout << "halved " << steps << " times\n";
// do-while: the body runs at least once
int tries = 0;
do {
tries++;
} while (tries < 3);
std::cout << "tries = " << tries << '\n';
// range-based for visits every element of a container
std::vector<int> values{1, 3, 5, 7};
int sum = 0;
for (int value : values) {
sum += value;
}
std::cout << "sum = " << sum << '\n';
}
1 2 3 4 5
halved 4 times
tries = 3
sum = 16
C and C++ comparison
The for header holds the start, the condition and the step. break leaves a loop early and continue jumps to the next round. C++ adds the range-based for, which visits every element of a container without an index.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Sum of Odd Numbers Add every odd number up to a limit. Easy
- Countdown to Liftoff Count down from a number and end with Liftoff! Easy
- Multiplication Table Row List the first multiples of a number. Easy