Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 8

for Loop - Counted Iteration

The `for` loop packs initialization, condition check, and update into one line. It's the idiomatic choice when the number of iterations is known in advance. Understanding that all three parts are optional (creating infinite loops or unusual patterns) and that the loop variable's scope depends on the C standard in use is important for reading real-world code.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

for Loop - All Patterns & Nested Loops

for Loop - All Patterns & Nested Loops
#include <stdio.h>

int main(void) {
    /* ── Basic counted loop ─────────────────────────── */
    for (int i = 0; i < 5; i++) {   /* C99: declare i inside for */
        printf("%d ", i);
    }
    printf("
");   /* 0 1 2 3 4 */

    /* ── Count down ─────────────────────────────────── */
    for (int i = 10; i > 0; i--) {
        printf("%d ", i);
    }
    printf("
");   /* 10 9 8 ... 1 */

    /* ── Step by 2 ───────────────────────────────────── */
    for (int i = 0; i <= 20; i += 2) {
        printf("%d ", i);
    }
    printf("
");   /* 0 2 4 ... 20 */

    /* ── All three parts optional ────────────────────── */
    int i = 0;
    for (; i < 3; ) {   /* init and update omitted */
        printf("i=%d
", i);
        i++;
    }

    /* ── Infinite loop (common in embedded/servers) ───── */
    /* for (;;) { ... break when done; } */

    /* ── Nested loops - 2D array fill ────────────────── */
    int matrix[3][3];
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            matrix[row][col] = row * 3 + col;
        }
    }

    /* Print the matrix */
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            printf("%2d ", matrix[row][col]);
        }
        printf("
");
    }

    /* ── Loop over array with sizeof - important pattern */
    int arr[] = {10, 20, 30, 40, 50};
    int len = (int)(sizeof(arr) / sizeof(arr[0]));   /* never hardcode length */
    int sum = 0;
    for (int j = 0; j < len; j++) {
        sum += arr[j];
    }
    printf("sum = %d
", sum);   /* 150 */

    return 0;
}

Common Mistakes

  • Off-by-one - `for (i = 0; i <= n; i++)` with array `arr[n]` accesses `arr[n]` which is out of bounds (valid indices are 0 to n-1). The correct condition is `i < n`.
  • Hardcoding array length in loops - `for (i = 0; i < 5; i++)` breaks silently when the array grows. Use `sizeof(arr) / sizeof(arr[0])` for stack arrays. For heap arrays, track the length separately.
  • Modifying the loop variable inside the loop - `for (i = 0; i < n; i++) { if (cond) i++; }` causes double-stepping that's hard to reason about. Use `break` or a flag instead.

Tip

Tip

Practice for Loop Counted Iteration in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

For loops package counter initialization, exit condition, and increment into a unified header

Practice Task

Note

Practice Task - (1) Write a working example of for Loop Counted Iteration from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with for Loop Counted Iteration is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • The `for` loop packs initialization, condition check, and update into one line.
  • Off-by-one - `for (i = 0; i <= n; i++)` with array `arr[n]` accesses `arr[n]` which is out of bounds (valid indices are 0 to n-1). The correct condition is `i < n`.
  • Hardcoding array length in loops - `for (i = 0; i < 5; i++)` breaks silently when the array grows. Use `sizeof(arr) / sizeof(arr[0])` for stack arrays. For heap arrays, track the length separately.
  • Modifying the loop variable inside the loop - `for (i = 0; i < n; i++) { if (cond) i++; }` causes double-stepping that's hard to reason about. Use `break` or a flag instead.

Topics in This Module

0/8 done