# Loops in C – How Programs Repeat Tasks

> <div data-node-type="callout">
> <div data-node-type="callout-emoji">💡</div>
> <div data-node-type="callout-text">I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: <strong>C Programming for Absolute Beginners. </strong>I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.</div>
> </div>

So far, your programs can:

*   Store data
    
*   Make decisions
    
*   Respond to user input
    

Now we teach them something powerful:

**Repetition.**

Because in real programming, we often want to do something more than once. You can access the [previous lesson here](https://blog.ehoneahobed.com/if-else-else-if-logical-operators-user-input).

Print numbers from 1 to 10.  
Ask the user for input multiple times.  
Repeat a calculation.

That’s where **loops** come in.

## What Is a Loop?

A loop allows a block of code to run **multiple times**.

Instead of writing:

```c
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
```

We can write a loop that prints it 3 times automatically.

C has three main loops:

*   `while`
    
*   `do-while`
    
*   `for`
    

Let’s go step by step.

# 1\. The `while` Loop

The `while` loop runs **as long as the condition is true**.

### Syntax

```c
while (condition) {
    // code runs repeatedly
}
```

* * *

### Example: Print Numbers 1 to 5

```c
#include <stdio.h>

int main() {
    int i = 1;

    while (i <= 5) {
        printf("%d\n", i);
        i++;
    }

    return 0;
}
```

### How This Works

*   `i` starts at 1
    
*   The loop runs while `i <= 5`
    
*   After printing, `i++` increases the value
    
*   When `i` becomes 6, the loop stops
    

Important:  
If you forget `i++`, the loop will never stop.  
This is called an **infinite loop**.

# 2\. The `do-while` Loop

The `do-while` loop is similar to `while`.

The difference:

*   It runs **at least once**, even if the condition is false.
    

### Syntax

```c
do {
    // code
} while (condition);
```

Notice the semicolon at the end.

### Example

```c
#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("%d\n", i);
        i++;
    } while (i <= 5);

    return 0;
}
```

The loop:

*   Runs first
    
*   Then checks the condition
    

Even if `i` started at 10, it would still print once.

# 3\. The `for` Loop

The `for` loop is commonly used when you know **how many times** you want to repeat something.

### Syntax

```c
for (initialization; condition; update) {
    // code
}
```

### Example: Print 1 to 5

```c
#include <stdio.h>

int main() {

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

    return 0;
}
```

This does the same thing as the `while` loop, but in a shorter form.

Let’s break it down:

*   `int i = 1` → starting point
    
*   `i <= 5` → condition
    
*   `i++` → increase after each loop
    

Everything is in one line.

# When Should You Use Each Loop?

*   Use `while` when you don’t know exactly how many times it will run.
    
*   Use `do-while` when the code must run at least once.
    
*   Use `for` when you know the number of repetitions.
    

For beginners, the `for` loop is usually easier to manage.

# Common Beginner Mistakes

1.  Forgetting to update the counter (`i++`)
    
2.  Writing wrong conditions
    
3.  Creating infinite loops
    
4.  Forgetting semicolon in `do-while`
    

Example of infinite loop:

```c
while (i <= 5) {
    printf("%d\n", i);
}
```

Here, `i` never changes.  
The program will run forever.

# Mini Project: Print Even Numbers

Write a program that prints even numbers from 2 to 20.

Example solution:

```c
#include <stdio.h>

int main() {

    for (int i = 2; i <= 20; i += 2) {
        printf("%d\n", i);
    }

    return 0;
}
```

Here, `i += 2` increases by 2 each time.

# Practice Exercises

1.  Print numbers from 10 down to 1.
    
2.  Ask the user to enter a number and print its multiplication table.
    
3.  Use a loop to calculate the sum of numbers from 1 to 100.
    

# Final Thoughts

With loops, your programs can now:

*   Repeat tasks
    
*   Count automatically
    
*   Handle multiple inputs
    
*   Perform repeated calculations
    

You now understand:

*   Variables
    
*   Conditions
    
*   Input
    
*   Loops
    

That is the foundation of C programming.

Next, we’ll combine everything to build small programs that feel real.
