C For Loop Examples

Welcome to The Coding College, your go-to resource for learning coding and programming concepts. In this article, we’ll dive into practical examples of the for loop in C, showcasing its versatility and how to use it efficiently.

What is a For Loop in C?

A for loop in C is used to execute a block of code repeatedly, based on a specified condition. It’s particularly useful when the number of iterations is known beforehand.

Syntax

for (initialization; condition; increment/decrement) {  
    // Code to execute  
}  
  • Initialization: Sets the starting point for the loop.
  • Condition: Determines whether the loop should continue running.
  • Increment/Decrement: Updates the loop variable after each iteration.

C For Loop: Practical Examples

Example 1: Printing Numbers

#include <stdio.h>

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

Output:

Number: 1  
Number: 2  
Number: 3  
Number: 4  
Number: 5  

Explanation:

The loop starts at i = 1 and runs until i is greater than 5. After each iteration, i is incremented by 1.

Example 2: Sum of First N Natural Numbers

#include <stdio.h>

int main() {
    int sum = 0;

    for (int i = 1; i <= 10; i++) {  
        sum += i;  
    }  

    printf("Sum of first 10 numbers: %d\n", sum);  

    return 0;  
}

Output:

Sum of first 10 numbers: 55  

Explanation:

This loop adds the numbers from 1 to 10 and stores the result in the sum variable.

Example 3: Displaying Multiplication Table

#include <stdio.h>

int main() {
    int number = 5;

    for (int i = 1; i <= 10; i++) {  
        printf("%d x %d = %d\n", number, i, number * i);  
    }  

    return 0;  
}

Output:

5 x 1 = 5  
5 x 2 = 10  
...  
5 x 10 = 50  

Explanation:

This loop generates the multiplication table for number = 5.

Example 4: Reversing Numbers

#include <stdio.h>

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

    return 0;  
}

Output:

10  
9  
8  
...  
1  

Explanation:

The loop starts at 10 and decrements i by 1 in each iteration until i becomes less than 1.

Example 5: Nested For Loops

#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {  
        for (int j = 1; j <= 2; j++) {  
            printf("Outer: %d, Inner: %d\n", i, j);  
        }  
    }  

    return 0;  
}

Output:

Outer: 1, Inner: 1  
Outer: 1, Inner: 2  
...  
Outer: 3, Inner: 2  

Example 6: Printing a Pattern

#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {  
        for (int j = 1; j <= i; j++) {  
            printf("* ");  
        }  
        printf("\n");  
    }  

    return 0;  
}

Output:

*  
* *  
* * *  
* * * *  
* * * * *  

Explanation:

The outer loop (i) determines the rows, and the inner loop (j) prints stars in each row.

Example 7: Looping Through an Array

#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++) {  
        printf("Element at index %d: %d\n", i, numbers[i]);  
    }  

    return 0;  
}

Output:

Element at index 0: 10  
Element at index 1: 20  
...  
Element at index 4: 50  

Benefits of Using For Loops

  1. Control Over Iterations: Ideal for situations where the number of iterations is known.
  2. Compact Syntax: Combines initialization, condition, and increment in one line.
  3. Versatility: Works seamlessly with arrays, data structures, and patterns.

Use Cases for For Loops

  • Generating reports or tables.
  • Manipulating arrays or data collections.
  • Pattern printing for user interfaces.

Conclusion

Mastering the for loop is essential for any C programmer. By practicing these examples, you’ll develop a strong foundation for solving real-world programming problems.

Leave a Comment