Loops in C

1️⃣ What Is a Loop?

A loop lets your program repeat a block of code multiple times.

💡 For example:
👉 Print numbers 1 to 5
👉 Keep asking the user for input until they enter 0

Without loops, you’d have to write the same code over and over.


2️⃣ Types of Loops in C

Loop TypeWhen to Use
for loopWhen you know how many times you want to repeat something.
while loopWhen you want to repeat while a condition is true.
do-while loopLike while, but the code runs at least once.

3️⃣ The for Loop

Syntax:

for (initialization; condition; increment) {
    // code block
}

🛠 Example: Print 1 to 5

#include <stdio.h>

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

🔍 What’s Happening:

  • int i = 1; → Start from 1.
  • i <= 5; → Keep looping while i is ≤ 5.
  • i++ → Increase i by 1 after each loop.

4️⃣ The while Loop

Syntax:

while (condition) {
    // code block
}

🛠 Example: Print 1 to 5

#include <stdio.h>

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

🔍 Key Point:
The condition is checked first. If it’s false at the start, the loop may never run.


5️⃣ The do-while Loop

Syntax:

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

🛠 Example: Print 1 to 5

#include <stdio.h>

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

🔍 Key Point:
The code block runs at least once, even if the condition is false initially.


6️⃣ Infinite Loops

If you want a loop to run forever (until you break it manually), you can write:

while (1) {
    // runs forever
}

Or:

for (;;) {
    // runs forever
}

7️⃣ break and continue

  • break; → Immediately exits the loop.
  • continue;Skips to the next iteration.

🛠 Example: Skip 3, Stop at 5

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 3) {
            continue;  // Skip 3
        }
        if (i == 5) {
            break;     // Stop loop
        }
        printf("%d\n", i);
    }
    return 0;
}

Output:

1
2
4

8️⃣ Nested Loops

A loop inside another loop is called a nested loop.

🛠 Example: Print a 3×3 Grid

#include <stdio.h>

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

Output:

1 2 3 
1 2 3 
1 2 3 

🔧 Common Mistakes to Avoid

  • Forgetting to update the loop variable → causes an infinite loop.
  • ❌ Using = (assignment) instead of == (comparison) in conditions.
  • ❌ Not using braces {} when multiple statements are in the loop.

✅ When to Use Which Loop

Use This Loop…When…
forYou know the exact number of iterations.
whileYou don’t know how many times in advance (e.g., input).
do-whileYou want to run at least once no matter what.

🔚 Conclusion

  • Loops let you repeat tasks easily.
  • for, while, and do-while each have their use cases.
  • Control flow tools like break and continue give you extra flexibility.

💡 Next challenge: Write a program that calculates the sum of digits of a number using a loop.

Feedback Display

Learner Reviews