-
Notifications
You must be signed in to change notification settings - Fork 0
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
}-
for (int i = 0; i < 5; i++): This is aforloop that initializes the variableito 0, checks the conditioni < 5, and incrementsiafter each iteration.-
Initialization:
int i = 0initializes the loop counterito 0. -
Condition:
i < 5ensures the loop runs as long asiis less than 5. -
Increment:
i++increases the value ofiby 1 after each iteration.
-
Initialization:
-
printf("i: %d\n", i);: Inside the loop, the current value ofiis printed on each iteration. The%dformat specifier is used to print the integer value ofi. -
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.