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

 

There are few ways to use this keyword in a statement.

 

Syntax - Example 1:

'  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.

 
DO [WHILE | UNTIL condition ]
  [statements ]
LOOP

 
'  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.

Parameter 1:

Description:

condition

Expression that evaluates to True  or False .

statements

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

 

Or, you can use this syntax.  This second form where the WHILE and UNTIL follow the LOOP keyword is 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.

Syntax - Example 2:

'  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.
 
DO
  [statements ]
LOOP [WHILE | UNTIL condition ]
 
'  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.

Parameter 2:

Description:

condition

Expression that evaluates to True  or False .

statements

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

 

Example:

'  -------------------------------------------------
'  Example 1:
VARIABLES: a   Type: Integer
 
DO
  '  Print the value in the Integer variable 'a'
  PRINT a
  '  Increment the value of a each
  '  time the loop is executed
  a = ( a + 1 )
'  Keep executing the loop until
'  'a' is equal to '11'
Loop While ( a < 11 )
'  Current value of fibonacci variable

PRINT "Current value of variable a: ", a
 
'  -------------------------------------------------
'  Example 2:
VARIABLES: fibonacci, lastFibonacci Type: Integer
'  Initialize starting and last step values
fibonacci = 1
lastFibonacci = 1
 
'  Perform this loop until '' is equal
'  to or greater than 100
DO UNTIL fibonacci > 100
  '  Print out the Fibonacci number.
  PRINT fibonacci
 
  '  Compute the next fibonacci number
  '  and update current 'fibinacci number
  fibonacci = ( fibonacci + lastFibonacci )
  '  Update previous fibonacci variable
  lastFibonacci = fibonacci
Loop

'  Current value of fibonacci variable

PRINT "Current value of fibonacci variable: ", fibonacci
'  -------------------------------------------------

Returns:

Example 1 Output:

0

1

2

3

4

5

6

7

8

9

10

Current value of variable a:  11

 

Example 2 Output:

1

2

4

8

16

32

64

Current value of fibonacci variable:  128

 

 

Links:

Assignment

See Also:

VARIABLES

 


Edit Time: 9/25/2017 09:09:54 AM


Topic ID#: 269

 

Created with Help & Manual 7 and styled with Premium Pack Version 2.80 © by EC Software