When we are working with for loop it contains 3 parts-
Initialisation
Condition
Iteration
Syntax
For (initialization; Condition; iteration)
{
Statement block;
}
When we are working with a for loop, the execution process will start from initialisation.
Initialisation part will be executed only once when we are passing the control within the body the first time.
After execution of initialisation part, control will pass to condition, if condition evaluate is true, then control will pass to statement block.
After execution of statement block, control will pass to iteration, from iteration-once again it will pass block to condition.
Always repetition will come b/w condition, statement block and iteration only.
When we are working with a loop, everything is optimal but mandatory to place 2 semicolons.
While ( ) → error
For (; ;) → Valid
When the condition part is not given in for loop, then it repeats infinite times because the condition part is replaced with non – zero value (True value).
When we are working with a FOR loop, it repeats in an anti-clock direction.
Always pre checking the process occurs when we are working with FOR loop i.e. by execution of statement block condition part is executed.
While (0) → No repetition
For (; 0 ;) → I’s it will repeat
In for loop, when the condition part is replaced with constant 0 then it repeats once, because no.of instances became 1 at the time of compilation.
Int I;
I = c;
While (i); No repetition
For (; I;)
Programming:
Void main ( )
{
Int I;
For (i=1; i< = 10; i= l+2)
Print f (“%d”, i);
}
O/p: 1 3 5 7 9