Iteration (Looping)

Post date: 09-Jan-2015 04:53:48

Iteration (Looping)

Introduction

Iteration is also referred to as looping or repetition. Its designed to execute the same block of code again and again until a certain condition is fulfilled.

The three main looping controls are:-

  1. The WHILE Loop
  2. The REPEAT....UNTIL Loop
  3. The FOR Loop

The WHILE Loop

The "WHILE" loop is used if a condition has to be met before the statements within the loop are executed.

General Format
WHILE condition DO
    statement
ENDWHILE
whileloop

The REPEAT....UNTIL loop

The REPEAT....UNTIL loop allows the statements within it to be executed at least once since the condition is tested at the end of the loop.

General Format
REPEAT
statements
UNTIL <condition>
repeatuntilloop

The FOR Loop

The FOR loop is used in circumstances where the execution of the chosen statements has to be repeated a predetermined number of times.

General Format
//pseudocode for 'FOR" loop that counts from the lower limit
FOR loop variable = lower limit to upper limit DO
statements
ENDFOR
forloop1
//pseudocode for a 'FOR' loop that counts from the upper limit down to lower limit
FOR loop variable = upper limit DOWN TO lower limit DO
statements;
ENDFOR
forloop2