June 14, 2026•2 min read

Loop Control Structures: When to Use For, While, and Do-While

Tired of writing the same lines of code over and over? Here is how you make the computer repeat the work for you, and which loop to pick.

Read in Indonesian

Key takeaways

  • Looping keeps code tidy because it runs a block of code repeatedly and automatically.
  • Use for when you already know exactly how many iterations you need.
  • Choose while or do-while when the number of iterations is not fixed in advance.
  • Unlike while, do-while guarantees 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:

PYTHON
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:

C
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:

PYTHON
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:

C
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 for when the count is certain. Use while or do-while when it is still not fixed.
  • Where the condition is checked: for and while check at the start, while do-while checks at the end.
  • Minimum executions: for and while may not run at all. do-while is guaranteed to run at least once.

Which one should you pick? Match it to the logic your program needs!

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.