YourCodingMentor

The for loop in C is a control flow statement that allows you to repeatedly execute a block of code a specific number of times. It is commonly used when the number of iterations is known beforehand or can be determined based on a condition.


Syntax

for (initialization; condition; increment/decrement) {
    // Code to be executed
}
  • initialization: This is where you set up the loop counter variable(s). It is executed once at the start of the loop.
  • condition: This expression is evaluated before each iteration. If it evaluates to true (non-zero), the loop continues; otherwise, the loop terminates.
  • increment/decrement: This is executed after each iteration. It is typically used to update the loop counter (e.g., incrementing or decrementing a variable).

Example 1: Basic for Loop

#include <stdio.h>

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

    return 0;
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

In this example, the for loop runs 5 times, printing the value of i from 1 to 5. The initialization int i = 1 sets the counter to 1, the condition i <= 5 ensures the loop runs as long as i is less than or equal to 5, and i++ increments i by 1 after each iteration.

Example 2: Decrementing in a for Loop

You can decrement the loop counter in the for loop.

#include <stdio.h>

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

    return 0;
}

Output:

Iteration 5
Iteration 4
Iteration 3
Iteration 2
Iteration 1

In this example, i-- decrements the value of i after each iteration, so the loop runs in reverse order, starting from 5 and ending at 1.

Example 3: Using Multiple Initialization and Increment Statements

A for loop allows you to use multiple initialization or increment/decrement statements, separated by commas.

#include <stdio.h>

int main() {
    for (int i = 1, j = 10; i <= 5; i++, j -= 2) {
        printf("i = %d, j = %d\n", i, j);
    }

    return 0;
}

Output:

i = 1, j = 10
i = 2, j = 8
i = 3, j = 6
i = 4, j = 4
i = 5, j = 2

Here, i starts at 1 and increments by 1, while j starts at 10 and decrements by 2 in each iteration.

Example 4: Infinite for Loop

A for loop can be made infinite by omitting the condition or using a condition that is always true.

#include <stdio.h>

int main() {
    for (;;) {  // Infinite loop (condition is always true)
        printf("This will run forever unless interrupted.\n");
    }

    return 0;
}

To stop an infinite loop, you can manually interrupt it using Ctrl+C in the terminal.

Example 5: Using break in a for Loop

The break statement can be used to exit the loop prematurely.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 6) {
            break;  // Exit the loop when i equals 6
        }
        printf("Iteration %d\n", i);
    }

    return 0;
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

In this example, the loop terminates early when i equals 6, due to the break statement.

Example 6: Using continue in a for Loop

The continue statement skips the remaining code in the current iteration and proceeds to the next iteration.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;  // Skip the iteration when i equals 3
        }
        printf("Iteration %d\n", i);
    }

    return 0;
}

Output:

Iteration 1
Iteration 2
Iteration 4
Iteration 5

Here, the continue statement skips the iteration where i equals 3, and the loop proceeds with i equal to 4.

Example 7: Nested for Loops

You can have one for loop inside another (nested loops) to perform operations that involve more than one variable.

#include <stdio.h>

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

    return 0;
}

Output:

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

In this example, the outer loop (i) runs from 1 to 3, and for each value of i, the inner loop (j) runs from 1 to 3.

Example 8: Using for Loop with Arrays

The for loop is commonly used to iterate over elements in an array.

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < n; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    return 0;
}

Output:

arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50

In this example, the for loop iterates over all elements in the array arr and prints each one.


Summary

  • The for loop is ideal for situations where you know the number of iterations in advance.
  • It consists of three parts: initialization, condition, and increment/decrement.
  • You can use break to exit the loop prematurely and continue to skip the current iteration.
  • Nested loops and arrays can be effectively handled using the for loop.
  • The for loop is a powerful construct for repetitive tasks and is widely used in C programming.

Leave a Reply

Your email address will not be published. Required fields are marked *