DO..LOOP

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

DO and LOOP cause code to be executed while a certain condition evaluates to true, or until a certain condition evaluates to true. Use DO..LOOP when you want the code to execute at least once. If the condition evaluates to false, the code will exit the loop.

Syntax

  1. do..loop..while
    do
    {code statements}
    loop while booleanExpr
  2. do..loop..until
    do
    {code statements}
    loop until booleanExpr
  3. do..while..loop
    do while booleanExpr
    {code statements}
    loop
  4. do..until..loop
    do until booleanExpr
    {code statements}
    loop

Hints

Normally, a loop will execute over and over until a condition evaluates to false, at which point the loop is ended. Do not issue a GOTO statement or attempt to exit a DO..LOOP before the loop has finished. If it is necessary to exit a loop before it has finished, issue an EXIT DO command.

Example

    a = 3
    i = 0
' The basic LOOP UNTIL construct
    ' Loop UNTIL i equals 3
    do
        i = i + 1
        print "Loop no. "; i
    loop until i = a

    print
' Program flow continues with DO UNTIL construct
    i = 0
    do until i = a
        i = i + 1
        print i
    loop

    print
' Program flow continues with LOOP WHILE construct
    ' Loop WHILE i does not equal 3
    i = 0
    do
        i = i + 1
        print i
    loop while i <> a

    print
' Program flow continues with DO WHILE construct
    i = 0
    do while i <> a
        i = i + 1
        print i
    loop
    end

' Exiting a loop prematurely
    do while i < 100
        i = i + 4
        print i
        if i > 13 then exit do
    loop

Useful Procedures

' Place a useful function or sub using this keyword here