For, While, Do-While — A Complete Guide to Loops in C

For, While, Do-While — A Complete Guide to Loops in C


"When I first started learning C, loops confused me. I understood the idea — repeat some code — but I never knew which loop to use when.

Then I learned the difference. And now, I use all three without thinking.

Here‘s what I learned."

5529825e443e58b1c3ab5e3d67073ba21761af61e4e13bb0a21789da21499eef.jpg

1. What Is a Loop?

A loop is a way to repeat a block of code multiple times. Instead of writing the same code 10 times, you write it once and tell the computer to repeat it.

Think of it like a washing machine cycle. You set it, it runs, and it stops when it‘s done.

 

2. The for Loop — When You Know Exactly How Many Times

The for loop is best when you know exactly how many times you want to repeat something.

The structure:

 

for (initialization; condition; increment) {

    // code to repeat

}

Example:

for (int i = 0; i < 5; i++) {

    printf("Number: %d\n", i);

}

This prints numbers 0 to 4. You know it will run exactly 5 times.

When to use it: When you're counting — arrays, loops with a fixed number of steps.

051fa7a2462b3c745c62d4a90882c4e1db29ca5430451e4d2881042e5e548550.jpg

3. The while Loop — When You Don't Know How Many Times

The while loop is best when you don‘t know how many times the loop will run.

The structure:

 

while (condition) {

    // code to repeat

}

Example:

int i = 0;

while (i < 5) {

    printf("Number: %d\n", i);

    i++;

}

 

This does the same thing as the for loop above, but the condition is checked at the beginning.

When to use it: When you're waiting for something to happen — reading user input, reading from a file, etc.

 

4. The do-while Loop — Run First, Check Later

The do-while loop is like while, but it runs the code first and then checks the condition.

The structure:

do {

    // code to repeat

} while (condition);

 

Example:

int i = 0;

do {

    printf("Number: %d\n", i);

    i++;

} while (i < 5);

 

The code runs at least once, even if the condition is false from the start.

When to use it: When you want to run the code at least once — like showing a menu to the user before asking for input.

 

5. Which Loop Should You Use?

Situation

Use

You know how many times to repeat

for

You don't know how many times

while

You want to run the code at least once

do-while

 

That‘s it. Once you know this, you can always pick the right loop.

5be5f858b32d6e17e2ef25e0d37ca3f6ef3bf07070b1ae320724b391e8d7da91.jpg

6. Common Mistakes (and How to Avoid Them)

Beginners often make these mistakes:

  • Forgetting to update the variable → infinite loop
  • Using the wrong condition → off-by-one errors
  • Putting a semicolon after the condition → empty loop

Don‘t worry — everyone makes these mistakes. You‘ll learn to spot them quickly.

 

7. My Personal Rule

I use for loops when I‘m working with arrays or anything that needs a counter. I use while loops when I‘m reading data. And I use do-while when I need to show something to the user before asking for input.

It‘s not a rule — it‘s just what works for me.

 

What About You?

"Which loop do you use the most? Do you have a favorite?

Let me know in the comments — I'd love to hear your experience."

 

"If this article helped you, leave a tip or a like — it keeps me writing simple explanations for you."

How do you rate this article?

3



TheNightCoder's Tech Diaries
TheNightCoder's Tech Diaries

Practical guides on web development and life hacks for IT enthusiasts.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.

Page not displaying correctly?