MC-Basic:DO ... LOOP

From SoftMC-Wiki
Jump to: navigation, search

The DO…LOOP is used to execute a section of code an indefinite number of times. The statements are executed at least once, since the loop termination condition is evaluated at the end of the loop. Statements are optional. If none are included, DO…LOOP acts as a delay.

The loop may be executed either while the condition is true, or until the condition becomes true. This choice is made by using either LOOP UNTIL or LOOP WHILE.

All looping constructs (FOR, WHILE, DO) use the CPU as long as a task of higher priority is not interrupting the loop. The condition is evaluated as often as allowed by the speed of the CPU. Occasionally, a WHILE loop could be waiting on a condition that does not need frequent checking. In such cases, it is advisable to insert a SLEEP command into the loop, to enable other tasks (perhaps lower priority tasks) to use the CPU.

Syntax

Do
       <code to execute>

Loop While | Until <condition>

Availability

All versions

Scope

Task

Limitations

Read only. Do not use DO…LOOP in Config.prg. The DO…LOOP statements must be matched within each program block (PROGRAM..END PROGRAM, SUB.. END SUB, and ONEVENT…END ONEVENT).

Examples

Dim Shared I as Long

Program
       I=0
       Do
                   I=I+1
                   Print I
       Loop Until I=10

End Program

Dim Shared I as Long

Program
       Do
                   I=I+1
                   Print I
       Loop While I<10

End Program

See Also