/    /  Initializing  and Updating Loops

Initializing  and Updating Loops

 

Loop initialization

 

  • Loop initialization is the preparation done before a loop starts.
  • It must be done before the first execution of the loop body.
  • The initialization may be explicit or implicit.
  • When the initialization is explicit, we include code to set the beginning values of key loop variables.
  • Implicit initialization provides no direct initialization code, rather, it relies on pre-existing situation to control the loop.

 

Loop Update

 

  • To not to go into an infinite loop, we must change something inside the body of the loop so as to change the condition.
  • The actions that cause change in the loop are known as loop update.
  • Updating is done in each iteration, if the body of the loop is repeated n times, then the updating is also done n times.

 

Initializing  and Updating Loops

 

Loop Comparison:

 

  • The number of iterations of a loop is given as n.
  • In a pre-test loop, when we come out of the loop the limit test has been executed n+1 times.
  • In a post-test loop, when we come out of the loop the limit test has been executed only n times.