Skip to content

C_For_Loop.c

ani9977 edited this page Sep 11, 2024 · 1 revision

Here's the updated code with comments and explanations:

#include <stdio.h>  // Includes the standard input-output library for input-output operations

int main() {  // Main function where program execution starts
    // A for loop that iterates from 0 to 4 (5 iterations in total)
    for (int i = 0; i < 5; i++) {
        // Prints the value of 'i' in each iteration
        printf("i: %d\n", i);  // %d is the format specifier for integers
    }

    return 0;  // Returns 0 to indicate successful program execution
}

Explanation:

  • for (int i = 0; i < 5; i++): This is a for loop that initializes the variable i to 0, checks the condition i < 5, and increments i after each iteration.
    • Initialization: int i = 0 initializes the loop counter i to 0.
    • Condition: i < 5 ensures the loop runs as long as i is less than 5.
    • Increment: i++ increases the value of i by 1 after each iteration.
  • printf("i: %d\n", i);: Inside the loop, the current value of i is printed on each iteration. The %d format specifier is used to print the integer value of i.
  • return 0;: This returns 0 to indicate that the program has executed successfully.

This program demonstrates how a for loop works by printing the values of i from 0 to 4. Each iteration prints the current value of i, which increases by 1 after each loop iteration.

Clone this wiki locally