DO Statement

Top  Previous  Next

Repeats a block of statements while a condition is TRUE or until a condition becomes TRUE

 

Syntax

 

DO  [WHILE  | UNTIL condition ]

   [statements ]

LOOP

 

condition                        Expression that evaluates to True  or False .

 

statements                        One or more statements executed while or until condition is True .

 

In this case if a WHILE is used, the condition is checked first and if the condition is TRUE then the statements are executed and the condition is reevaluated again until the condition becomes FALSE at which point the loop stops.

 

If an UNTIL the condition is checked first and if the condition is FALSE then the statements are executed and the condition is reevaluated again until the condition becomes TRUE at which point the loop stops.

 

Or, you can use this syntax:

 

DO

   [statements ]

LOOP  [WHILE  | UNTIL condition ]

 

condition                        Expression that evaluates to True  or False .

 

statements                        One or more statements executed while or until condition is True .

 

The second form where the WHILE and UNTIL follow the LOOP keyword at end of the DO ... LOOP differs from the first form in that the statements are always executed at least once after which the condition is evaluated.

 

If WHILE follows the LOOP and the condition is TRUE, the statements are executed again and the condition is reevaluated until the condition becomes FALSE.

 

If UNTIL follows the LOOP and the condition is FALSE, the statements are executed again and the condition is reevaluated until the condition becomes TRUE.

 

 

Examples

 

DO

   print a

   a = ( a + 1 )

 

LOOP WHILE ( a < 11 )

 

 

VARIABLES: fibonacci, lastFibonacci TYPE: INTEGER

fibonacci = 1

lastFibonacci = 1

DO UNTIL fibonacci > 100

   ' Print out the Fibonacci number.

   PRINT fibonacci

 

   ' Compute the next fibonacci number

   fibonacci = ( fibonacci + lastFibonacci )

   lastFibonacci = fibonacci

LOOP