Evaluation Of for Loops

Evaluation Of for Loops

The loop we just saw (here) works according to following steps :-

These steps are explained below : 
  1. Area I, in program its initialize i=1.
  2. 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.
  3. We execute the body of loop just like a normal program. Line by line,Step by step.Just the easy way.
  4. Area III, in program its an increment statement. So increment value of i by 1.
  5. Jump to step (ii) and keep repeating the process until condition gets false.
  6. 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.



<<for loop                                                    Home                                               An Odd Loop>>

No comments:

Post a Comment