Skip to main content
C Programming Basics
CHAPTER 08 Beginner

Loops in C

Updated: May 17, 2026
5 min read

# CHAPTER 8

Loops in C

1. Introduction

Imagine needing to print "Hello" 1,000 times. Writing printf a thousand times is terrible programming. Loops allow you to execute a block of code repeatedly as long as a specified condition is true. Loops are the workhorses of algorithms and data processing.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the while loop for unknown iteration counts.
  • Use the do-while loop for guaranteed execution.
  • Use the for loop for counted iterations.
  • Control loops using break and continue.
  • Write nested loops.

3. The while Loop

The while loop executes a block of code as long as a condition is true. It checks the condition *before* executing the body.
c
123456
int count = 1;

while (count <= 5) {
    printf("Count is: %d\n", count);
    count++; // CRITICAL: Without this, the loop runs forever!
}

Best for: When you don't know exactly how many times the loop should run (e.g., reading a file until the end).

4. The do-while Loop

Similar to the while loop, but it checks the condition *after* executing the body. This guarantees the loop runs at least once.
c
1234567
int num = 10;

do {
    printf("Number is: %d\n", num);
    num++;
} while (num <= 5);
// Output: Number is: 10 (It ran once even though 10 is not <= 5)

5. The for Loop

The for loop is best when you know exactly how many times you want to iterate. It combines initialization, condition, and increment into one neat line.

Syntax: for (initialization; condition; increment/decrement)

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

6. Control Statements: break and continue

  • break: Immediately exits the entire loop.
  • continue: Skips the rest of the current iteration and jumps to the next one.
c
1234567891011121314151617
// Example of break
for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break; // Stops the loop entirely at 5
    }
    printf("%d ", i);
}
// Output: 1 2 3 4

// Example of continue
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue; // Skips 3
    }
    printf("%d ", i);
}
// Output: 1 2 4 5

7. Nested Loops

A loop inside another loop. Often used for 2D graphics or matrices.
c
123456
for (int row = 1; row <= 3; row++) {
    for (int col = 1; col <= 3; col++) {
        printf("(%d,%d) ", row, col);
    }
    printf("\n"); // New line after each row
}

*Output:*

123
(1,1) (1,2) (1,3) 
(2,1) (2,2) (2,3) 
(3,1) (3,2) (3,3) 

8. Mini Project: Pattern Printing App

Let's build a program that prints a right-angled triangle using stars.
c
123456789101112131415161718
#include <stdio.h>

int main() {
    int rows;

    printf("=== PATTERN PRINTER ===\n");
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n"); // Move to the next line
    }

    return 0;
}

*Output (if rows = 4):*

1234
* 
* * 
* * * 
* * * * 

9. Memory-Level Explanation

When a loop runs, the CPU repeatedly executes the same block of machine code in memory. The CPU's Instruction Pointer (IP) jumps back to the memory address at the start of the loop as long as the condition evaluates to non-zero.

10. Common Mistakes

  • Infinite Loops: Forgetting to update the loop variable (e.g., forgetting count++).
  • Off-by-one errors: Using <= instead of < (or vice versa), causing the loop to run one time too many or too few.
  • Semicolon after for: for(int i=0; i<5; i++); { printf("Hi"); } The semicolon terminates the loop immediately. "Hi" prints only once!

11. Exercises

  1. 1. Write a for loop to print all even numbers from 2 to 20.
  1. 2. Write a while loop that asks the user to enter a positive number. If they enter a negative number, keep asking.
  1. 3. Write a program to calculate the factorial of a number using a loop.

12. MCQ Quiz with Answers

Question 1

Which loop is guaranteed to execute at least once?

Question 2

What causes an infinite loop?

Question 3

What does the break statement do?

Question 4

What does the continue statement do?

Question 5

In for(int i=0; i<5; i++), how many times does the loop run?

Question 6

What happens here: while(1) { }?

Question 7

Is for(;;) a valid statement in C?

Question 8

Which loop is best when you know the exact number of iterations?

Question 9

Nested loops mean:

Question 10

What is the output of for(int i=0; i<3; i++); printf("%d", i); ?

13. Interview Questions

  • Q: What is the difference between while and do-while loops?
  • Q: How do you break out of a nested loop completely?
  • Q: What is an off-by-one error, and how do you avoid it?

14. Summary

Loops execute code repeatedly. Use while for condition-based iteration, do-while when you need at least one execution, and for when counting iterations. break and continue offer fine-grained control over loop execution.

15. Next Chapter Recommendation

In Chapter 9: Functions in C, we will learn how to organize our code into reusable blocks, making programs cleaner and more modular.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·