C Programming Day 5: Learn Loops in C with Examples – For, While & Do While

Welcome to Day 5 of our 30 Days of C Programming series! If you’ve been following along, you’ve already learned how to use variables, data types, and operators. Now it’s time to take a big step forward by learning about loops in C with examples an essential concept for making your code repeat tasks automatically.

In this post, you’ll explore what loops are, how they work, and how to use the three types of loops in C: for, while, and do while. Plus, you’ll get beginner-friendly code examples that you can run and tweak yourself!

New here? Start from Day 1 How to Start C Programming in 2025 – Day 1 Beginner’s Setup Guide
📘 Full series: 30 Day’s of C Programming for Beginners – Full Roadmap

What are Loops in C Programming?

A loop is a programming structure that lets you repeat a block of code multiple times, either for a specific number of times or until a condition is met.

In real life, think of loops like brushing your teeth every day. You do it again and again, as long as a condition (you’re not leaving the house!) is true.

C offers three types of loops:

do while loop – runs at least once, even if the condition is false

for loop – best when the number of repetitions is known

while loop – when repetitions depend on a condition

For Loop in C – Do It a Known Number of Times

The for loop is the most common and beginner-friendly loop. It’s perfect when you already know how many times you want something to repeat.

✅ Syntax:

for (initialization; condition; update) {
// code to be repeated
}

💡 How It Works:

Initialization: Sets a starting point (int i = 1)

Condition: Checked before every loop run (i <= 5)

Update: Increments/decrements (i++) after each run

🔍 Example: Print numbers from 1 to 5

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}
Output:

1
2
3
4
5

This is a classic example of loops in C with examples predictable, easy to use, and highly readable.

While Loop in C – Repeat While a Condition is True

The while loop is ideal when you don’t know in advance how many times you need to repeat something. It continues as long as the condition is true.

Syntax:

while (condition) {
    // code to be repeated
}

How It Works:

Checks the condition before running the code block

Stops immediately if the condition is false from the beginning

Example: Print numbers from 1 to 5

#include <stdio.h>

int main() {
    int i = 1;
    while (i <= 5) {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
Output:

1
2
3
4
5

The while loop gives you more flexibility than for, especially when looping based on dynamic values like user input or sensor readings.

Do While Loop in C – Run First, Check Later

The do while loop is slightly different: it executes the code block once before checking the condition. That means the loop always runs at least one time, no matter what.

Syntax:

do {
// code to be repeated
} while (condition);

How It Works:

Code inside do runs once, then checks the condition

Great for input validation or menus where action must happen at least once

Example: Print numbers from 1 to 5

#include <stdio.h>

int main() {
    int i = 1;
    do {
        printf("%d\n", i);
        i++;
    } while (i <= 5);
    return 0;
}
Output:

1
2
3
4
5

In tutorials about loops in C with examples, this type often gets overlooked but it’s extremely useful in real-world applications.

Loop Control Statements

These statements modify loop behavior:

break → exit the loop completely

continue → skip the current iteration and jump to the next one

Example: Using break

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3)
            break;
        printf("%d\n", i);
    }
    return 0;
}
Output:

1
2

Example: Using continue

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3)
            continue;
        printf("%d\n", i);
    }
    return 0;
}
Output:

1
2
4
5

Nested Loops in C

You can also place one loop inside another. This is useful for patterns, matrices, or multi-level logic.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 2; j++) {
            printf("i = %d, j = %d\n", i, j);
        }
    }
    return 0;
}

When to Use Which Loop?

Loop Type Use When You…

for – Know exactly how many times to repeat
while – Don’t know how many repetitions will be needed
do while – Need the code to run at least once

Practice Tasks for Loops in C

Try writing these on your own:

Keep asking the user to enter a number until they enter 0 (using do while loop)

Print even numbers from 1 to 20 using a for loop

Print numbers from 10 down to 1 using a while loop

Conclusion

By now, you should have a clear understanding of loops in C with examples how they work, when to use which one, and how they can make your code efficient.

Leave a Comment