Table of Contents
The for loop combines initialization, condition checking, and iteration in a single loop statement. It is commonly used when the number of iterations is controlled by a counter.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
return 0;
}
Example Output #
1
2
3
4
5
Explanation #
The for loop contains three expressions:
for (i = 1; i <= 5; i++)
i = 1initializes the loop counter.i <= 5is the condition checked before each iteration.i++increments the counter after each iteration.
The execution order is:
iis initialized to1.- The condition
i <= 5is checked. - The loop body executes.
i++incrementsi.- Steps 2–4 repeat until the condition becomes false.
When i becomes 6, the condition evaluates to 0, so the loop terminates.