/    /  Iteration and Repetitive Execution of Loops

Iteration and Repetitive Execution of Loops

 

Selection and iteration statements are the basic tools of designing a logical process.

 

Concept of a Loop

 

  • A loop allows one to execute a statement or a block of statements repeatedly. It never stop.
  • To ensure that it ends, we need to have a condition that controls the loop that it ends either before or after iteration, if this is not done, the loop repeats itself once again; if it is done, the loop terminates.
  • This test is known as loop control expression.

 

loop iteration

 

  • Two types of iteration/loops are:
    • Unbounded iteration/ loop
    • Bounded iteration/loop

 

Unbounded iteration / Unbounded Loop:

 

When one doesn’t know, how many iterations may be required ahead of time, such cases require unbounded loops.

Two types of such loops are:

  • while
  • do_while

These loops are also known as indeterminate or indefinite loops.

 

Bounded iteration / Bounded loop:

 

When we know, how many times we need to loop; such cases are called bounded loops.

    • For loop is an example of this type.

 

Pre-test and Post-test Loops:

 

Pre-test Loop:

 

The condition is checked before we start and at the beginning of each iteration. If the test condition is true, we run the code; if the test condition is wrong, we terminate the loop. The statements associated with this type of construct may not be executed even once.

 

loop iteration

 

Post-test Loop:

 

The condition is tested ad if the expression is true, the loop repeats, if the expression (condition) is false, the loop terminates. But, before the condition is tested for the first time, the actions are executed at least once. The statements associated with this construct may be executed at least once.

loop iteration