C For Loop

Master repetitive tasks with controlled iterations

🔄 What is a For Loop?

A for loop in C repeats a block of code a specific number of times. It's perfect when you know exactly how many iterations you need, making it ideal for counting and array processing.


// Simple for loop example
#include <stdio.h>

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

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

For Loop Structure

🎯

Initialization

Set starting value

int i = 0

Condition

Check if loop continues

i < 10
⬆️

Update

Change loop variable

i++
🔄

Body

Code to repeat

printf("%d ", i);

🔹 Basic For Loop Syntax

The for loop provides a compact syntax for repetition with three components: initialization, condition, and update, all separated by semicolons. The structure for(initialization; condition; update) { body; } executes initialization once, checks the condition before each iteration, runs the body if true, then executes the update expression. For example, for(int i=0; i<5; i++) { printf("%d", i); } prints numbers 0 through 4. This loop type is ideal when iteration count is known beforehand, making it perfect for array traversal, counting operations, and fixed repetitions. All three components are optional; for(;;) creates an infinite loop equivalent to while(1), demonstrating the flexibility of this fundamental control structure.

for(initialization; condition; update) {
    // Code to execute
}

// Example: Print numbers 1 to 10
#include <stdio.h>

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

Output:

1 2 3 4 5 6 7 8 9 10

🔹 Common For Loop Examples

🔸 Counting Backwards

#include <stdio.h>

int main() {
    printf("Countdown: ");
    for(int i = 10; i >= 1; i--) {
        printf("%d ", i);
    }
    printf("Blast off!\n");
    return 0;
}

Output:

Countdown: 10 9 8 7 6 5 4 3 2 1 Blast off!

🔸 Skip Numbers (Step by 2)

#include <stdio.h>

int main() {
    printf("Even numbers: ");
    for(int i = 2; i <= 20; i += 2) {
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}

Output:

Even numbers: 2 4 6 8 10 12 14 16 18 20

🔹 Nested For Loops

Nested for loops place one loop inside another, enabling multi-dimensional iteration essential for matrix operations, pattern printing, and complex data structure traversal. The outer loop controls primary iteration while inner loops complete all their iterations for each single outer loop iteration. For example, for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf("%d,%d ", i, j); } } prints all row-column combinations in a 3x3 grid. Nested loops are fundamental for working with two-dimensional arrays, generating multiplication tables, creating visual patterns, implementing sorting algorithms like bubble sort, and processing hierarchical or grid-based data structures. Understanding nesting depth and iteration counts is crucial for analyzing time complexity and optimizing performance in computational algorithms.

#include <stdio.h>

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

Output:

Multiplication Table:
1 x 1 = 1	1 x 2 = 2	1 x 3 = 3	
2 x 1 = 2	2 x 2 = 4	2 x 3 = 6	
3 x 1 = 3	3 x 2 = 6	3 x 3 = 9

🧠 Test Your Knowledge

What will this for loop print: for(int i = 0; i < 3; i++)?