for loop is used to execute statement or group of statements repeatedly. It is an entry controlled loop that combines three steps such as initialization, condition and increment/decrement.
Syntax:
for(initialization; condition; increment/decrement)
{
Statements;
}
In for loop, initialization of index variable is done first. In condition checking, value of index variable is checked. If condition is true then control enters into the loop and executes loop statements. After every iteration, value of index variable increments or decrements by one.Then control passes to condition again. Loop executes till the condition is true. Once the condition becomes false, control comes out of for loop.
Example:
int i;
for(i=1;i<=5;i++)
{
ptintf("%d",i);
}
In the above example, variable i is a index variable initialized to 1. printf () statement inside loop executes 5 times i.e. till the condition is true. After each iteration value of index variable i increments by one. When value of i becomes 6, condition in for loop becomes false and control comes out of loop.
0 Comments