for loop in c
In c, for loop is an entry control loop, which repeat statements until test condition become false. It is one of the easy to understand loop among other loops.
Syntax
for(initialization; condition; iteration)
{
// statements;
}
For loop has three parts
initialization | The initial value from where the for loop starts. |
condition | It specifies the number of times loop will repeat itself. |
iteration | It either increases or decreases the loop cycle to continue. |
Example
#include<stdio.h>
void main(){
for(int i = 0; i < 10; i++)
{
printf("%d ", i);
}
}
Output:
1 2 3 4 5 6 7 8 9
Explanation
- i=0; means initial or starting value of i is 0 and loop starts from value 0.
- i < 10; It is test condition. Until test condition is true means value of i is less than 9, loop executes statements, Once value of i becomes 9, test condition becomes false and loop stops execution.
- i++ means i = i + 1, iteration increases the value of i by 1 each time, until test condition becomes false.