Loops in C

For Loop • While Loop • Do‑While Loop

1. What Are Loops?

Loops let a program repeat a block of code multiple times. They are useful when you need to run the same logic again and again — like counting, processing lists, or repeating user prompts.

A loop basically says: "Do this task again until a condition is met."

There are three types of loops in C:

  • for loop
  • while loop
  • do‑while loop

For Loop

A for loop lets you repeat something many times automatically. It is used when you already know how many times you want the repetition to happen.

A for loop has three parts:
1. Initialization – where the loop starts
2. Condition – when the loop should stop
3. Update – what happens after each repeat (usually increasing or decreasing a number)


for (initialization; condition; update) {
    // code that repeats
}
        

A for loop repeats code step-by-step. To understand it, imagine the loop follows a fixed routine every time it runs:

1. Run the initialization (this happens only once)
2. Check the condition
- If the condition is true → run the loop body
- If the condition is false → stop the loop
3. After the loop body finishes → update the variable
4. Go back to step 2


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

How this loop works step-by-step

Step 1: Initialization The loop starts by setting i = 0. This happens only once.

Step 2: Condition Check The loop asks: “Is i < 5?“ If yes → continue If no → stop

Step 3: Run the loop body Since i = 0 is less than 5, it prints 0.

Step 4: Update After printing, the loop does i++ → now i becomes 1.

Now it repeats the cycle:

  • Check: is 1 < 5? yes → print 1 → update to 2
  • Check: is 2 < 5? yes → print 2 → update to 3
  • Check: is 3 < 5? yes → print 3 → update to 4
  • Check: is 4 < 5? yes → print 4 → update to 5
  • Check: is 5 < 5? no → loop stops

The loop ends because 5 is NOT less than 5. So the loop exits and moves on to the rest of the program.

Easy Summary

  • Start: set i = 0
  • Repeat while condition is true
  • Print the value
  • Increase i by 1 every time
  • Stop when the condition becomes false

3. Custom Step Size

Sometimes you don’t want to increase the number by 1. You can choose how much the number changes every time the loop repeats.


        for (int i = 0; i <= 10; i += 2) {
            printf("%d ", i);
        }
        

How this loop works step-by-step

Step 1: Initialization
The loop starts by setting i = 0.

Step 2: Condition Check
It asks: “Is i <= 10?” If yes → run the loop If no → stop

Step 3: Run the loop body
Since 0 <= 10 is true, it prints 0.

Step 4: Update
Instead of i++, this loop uses i += 2. This means: i = i + 2. So now i becomes 2.

Now the loop repeats:

  • Check: is 2 <= 10? yes → print 2 → update to 4
  • Check: is 4 <= 10? yes → print 4 → update to 6
  • Check: is 6 <= 10? yes → print 6 → update to 8
  • Check: is 8 <= 10? yes → print 8 → update to 10
  • Check: is 10 <= 10? yes → print 10 → update to 12
  • Check: is 12 <= 10? no → loop stops

The loop ends when i becomes 12, because 12 is NOT less than or equal to 10.

Easy Summary

  • Start at 0
  • Print numbers as long as they are ≤ 10
  • Increase by 2 each time instead of 1
  • Stop when the number becomes greater than 10

Counting Down (Decrementing)

A decrementing loop is a loop that goes backward. Instead of increasing the number each time, it decreases it.


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

How this loop works step-by-step

Step 1: Initialization
The loop starts by setting i = 5.

Step 2: Condition Check
It asks: “Is i >= 1?” If yes → run the loop If no → stop

Step 3: Run the loop body
Since 5 >= 1 is true, it prints 5.

Step 4: Update
The loop uses i--, which means: i = i - 1. So now i becomes 4.

Now the loop repeats:

  • Check: 4 >= 1? yes → print 4 → update to 3
  • Check: 3 >= 1? yes → print 3 → update to 2
  • Check: 2 >= 1? yes → print 2 → update to 1
  • Check: 1 >= 1? yes → print 1 → update to 0
  • Check: 0 >= 1? no → loop stops

The loop stops because 0 is NOT greater than or equal to 1.

Easy Summary

  • Start at 5
  • Go down one step at a time
  • Stop when the number goes below 1

7. Break (Stopping a Loop Early)

break is used when you want the loop to stop immediately, even if the condition is still true.


                         for (int i = 0; i < 10; i++) {
                             if (i == 5) break;
                             printf("%d\n", i);
                         }
                         

How this loop works step-by-step

Step 1: Initialization
The loop starts with i = 0.

Step 2: Condition Check
It asks: “Is i < 10?” If yes → run the loop If no → stop

Step 3: Loop Body Starts

Since i = 0, the loop checks the line:

if (i == 5) break;

This asks: “Is i equal to 5?” Since it's not, the loop prints 0.

Step 4: Update
i++ makes i become 1.

Now the loop repeats:

  • i = 1 → not 5 → prints 1 → update to 2
  • i = 2 → not 5 → prints 2 → update to 3
  • i = 3 → not 5 → prints 3 → update to 4
  • i = 4 → not 5 → prints 4 → update to 5
  • i = 5 → condition inside loop becomes true

What happens at i = 5?

if (i == 5) break;

Now the condition is true, so break runs. This instantly stops the entire loop.

No printing happens for 5, 6, 7, 8, or 9.

Easy Summary

  • The loop starts at 0 and runs normally
  • When i becomes 5, break stops the loop
  • Only 0, 1, 2, 3, 4 are printed

7. Continue (Skip One Loop Step)

continue does NOT stop the loop. It simply skips the rest of the current loop step and jumps to the next one.


                                          for (int i = 0; i < 10; i++) {
                                              if (i % 2 == 0) continue;
                                              printf("%d\n", i);
                                          }
                                          

How this loop works step-by-step

Step 1: Initialization
The loop starts with i = 0.

Step 2: Condition Check
It asks: “Is i < 10?” If yes → continue If no → stop

Step 3: Loop Body Starts

The loop checks:

if (i % 2 == 0) continue;

i % 2 means “remainder when dividing by 2”. If the remainder is 0 → the number is even.

Now let’s follow the loop:

  • i = 0 → even → continue runs → printing is skipped → update to 1
  • i = 1 → odd → printing happens → prints 1 → update to 2
  • i = 2 → even → continue → skip print → update to 3
  • i = 3 → odd → print 3 → update to 4
  • i = 4 → even → skip → update to 5
  • i = 5 → odd → print 5 → update to 6
  • i = 6 → even → skip → update to 7
  • i = 7 → odd → print 7 → update to 8
  • i = 8 → even → skip → update to 9
  • i = 9 → odd → print 9 → update to 10
  • i = 10 → condition false → loop stops

Important: Whenever continue runs, the loop instantly jumps to the update step and then checks the condition again. It does NOT print anything for that value of i.

Easy Summary

  • Even numbers are skipped
  • Only odd numbers are printed
  • The loop never stops early — it just skips certain steps

3. The while Loop

A while loop repeats code as long as the condition is true. Use it when you don’t know how many times something needs to repeat.

Real-life example: You keep checking if your download is done. While it’s not done → keep waiting.


    while (condition) {
        // repeated code
    }
    

1. Counting Up

This loop starts at 0 and keeps printing numbers until it reaches 4.


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

How it works step-by-step:

  • Start: i = 0
  • Check: is i < 5? yes → print 0 → i becomes 1
  • Check again: is 1 < 5? yes → print 1 → i = 2
  • Repeats until i becomes 5
  • When i = 5, condition is false → loop stops

2. Counting Down

This loop goes backward from 5 to 1.


    int i = 5;
    
    while (i >= 1) {
        printf("%d\n", i);
        i--;  // decrease
    }
    

3. Using Custom Step Size

Increase the number by 2 each time.


    int i = 0;
    
    while (i <= 10) {
        printf("%d ", i);
        i += 2;
    }
    

4. Infinite While Loop

This loop never ends because the condition is always true.


    while (1) {          // 1 means true
        printf("Running...\n");
    }
    

5. Break and Continue

break: stop the loop immediately.


    int i = 0;
    
    while (i < 10) {
        if (i == 5) break;
        printf("%d\n", i);
        i++;
    }
    

continue: skip this loop step and continue to the next one.


    int i = 0;
    
    while (i < 10) {
        i++;  // update goes first
        if (i % 2 == 0) continue;
        printf("%d\n", i);
    }
    

6. Real-Life Style Example

Imagine checking your phone battery. While battery > 20% → keep using phone normally. When battery <= 20% → stop and charge.


    int battery = 100;
    
    while (battery > 20) {
        printf("Battery OK: %d%%\n", battery);
        battery -= 5;   // using phone reduces battery
    }
    

4. The do...while Loop

A do...while loop runs the code at least once before checking the condition. After running, it checks the condition — if true, it repeats.

Real-life example: You taste food while cooking. You always taste once, then decide if you need to taste again.


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

1. Basic Example

This prints numbers from 0 to 4.


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

How it works:

2. Counting Down

Runs at least once, even if starting value is small.


    int i = 5;
    
    do {
        printf("%d\n", i);
        i--;
    } while (i >= 1);
    

3. Always Runs Once

Even though the condition is false, the body runs one time.


    int x = 10;
    
    do {
        printf("Runs once!\n");
    } while (x < 5);   // false
    

4. Using Step Size

Increase by 3 each time.


    int i = 0;
    
    do {
        printf("%d ", i);
        i += 3;
    } while (i <= 12);
    

5. Break and Continue

break: stop the loop immediately.


    int i = 0;
    
    do {
        if (i == 4) break;
        printf("%d\n", i);
        i++;
    } while (i < 10);
    

continue: skip this step and continue to next.


    int i = 0;
    
    do {
        i++;
        if (i % 2 == 0) continue;
        printf("%d\n", i);
    } while (i < 10);
    

6. Real-Life Style Example

Asking for a password: You always ask once, and if wrong, you keep asking.


    int attempts = 0;
    int correct = 0;
    
    do {
        printf("Enter password:\n");
        attempts++;
    
        // fake check for example
        if (attempts == 3) correct = 1;
    
    } while (!correct);
    

While vs Do-While

1. Main Difference


2. Example: While Loop


    int i = 5;
    
    while (i < 5) {
        printf("Hello\n");
        i++;
    }
    

Output:


    (No output)
    

Reason: Condition i < 5 is false from the start → loop never runs.


3. Example: Do-While Loop


    int i = 5;
    
    do {
        printf("Hello\n");
        i++;
    } while (i < 5);
    

Output:


    Hello
    

Reason: Body runs once before checking the condition.


4. When to Use While

Choose while when:

Real-life examples:


5. When to Use Do-While

Choose do...while when:

Real-life examples:

6. Example Programs for All Loops

6. Example Programs

// Loading...
⬅ Back to Modules