Key takeaways
- Looping keeps code tidy because it runs a block of code repeatedly and automatically.
- Use
forwhen you already know exactly how many iterations you need. - Choose
whileordo-whilewhen the number of iterations is not fixed in advance. - Unlike
while,do-whileguarantees the code runs at least once.
Have you ever written the same line of code over and over? It tires you out, and it makes the code look messy.
That is where looping saves a programmer's life. It lets you execute one block of code many times automatically. You just set the condition and the limit.
There are three basic kinds of looping you must understand. Let's go through them one by one!
1. The For Loop
A for loop belongs to the counted loop category. It fits perfectly when you already know exactly how many iterations you need from the start.
Its structure is tidy too, because the three main elements sit on one line. There is initialization (the starting value), condition (the limit of the loop), and increment or decrement (how the value changes).
In Python, for usually walks through the contents of a sequence:
for i in range(1, 6):
print(i)
That code prints the numbers 1 through 5, one per line. Older languages write all three elements explicitly, C for example:
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
The result is exactly the same. The only difference is where you put the initialization, the condition, and the change.
But remember: if the condition is already false from the start, the block inside never runs at all. Zero times.
2. The While Loop
while is a different story. This loop is classified as an uncounted loop.
You reach for while when the number of iterations is unclear and depends heavily on a dynamic condition. Its main trait is that the check happens at the start, before the block runs.
To make the difference visible, here is a concrete case. A balance of 3 is withdrawn one at a time until it runs out:
saldo = 3
while saldo > 0:
print("tarik saldo, sisa", saldo - 1)
saldo = saldo - 1
The output is three lines: 2 left, 1 left, 0 left, then it stops on its own because saldo is no longer greater than 0. This loop earns its place exactly when you do not know up front how many turns it will take.
Just like for, if the check is already false at the start, the loop stops immediately and executes zero times.
3. The Do-While Loop
do-while is very close to while, and it is also an uncounted loop. The difference is where the condition is checked.
In do-while, the check happens at the end of the loop. That structure guarantees the code runs at least once.
After one pass, the condition is checked. If true, it repeats. If false, the loop stops.
The difference is clearest when the condition is false from the start. Compare these two C snippets, run with saldo set to 0:
int saldo = 0;
while (saldo > 0) {
printf("tarik saldo\n");
saldo--;
}
do {
printf("tarik saldo\n");
saldo--;
} while (saldo > 0);
The while version prints nothing, the do-while version prints one line. That is the only reason do-while exists.
One note: Python has no do-while. When you need the same behavior, you write while True: and exit with break inside. Other languages have their own names. Pascal calls it repeat ... until, and the check sits at the end there too.
Conclusion: So What's the Difference?
To make it easier to remember, here are the three fundamental differences:
- Number of iterations: Use
forwhen the count is certain. Usewhileordo-whilewhen it is still not fixed. - Where the condition is checked:
forandwhilecheck at the start, whiledo-whilechecks at the end. - Minimum executions:
forandwhilemay not run at all.do-whileis guaranteed to run at least once.
Which one should you pick? Match it to the logic your program needs!