Evaluation Of for Loops
The loop we just saw (here) works according to following steps :-
These steps are explained below :
- Area I, in program its initialize i=1.
- Area II, in program its check whether i<=5 is true or not. If its true then enter the loop body. If its false then execute the statement immediately after body of loop.
- We execute the body of loop just like a normal program. Line by line,Step by step.Just the easy way.
- Area III, in program its an increment statement. So increment value of i by 1.
- Jump to step (ii) and keep repeating the process until condition gets false.
- When Condition gets false, we step out of loop body and the step immediately after the body is executed. In our program, "We are out of loop" gets printed on screen.
Remember
area1, area2 and area3 shown above doesn't necessarily need to
have initialize condition check, increment/decrements statement in
them.They may have blank space or multiple statement in them.For putting
multiple statement we just separate them by commas.Lets check an
example.
No comments:
Post a Comment