Do...Loop Structure

Purpose

Declares an infinite programming loop.

Syntax

Do

// program segment

Loop

Description

Do...Loop is an endless loop which can only be terminated by the conditional command Exit If or unconditional command Exit Do.

Example

OpenW # 1

Local r

Do

r = 0

Input "Enter radius";r

If r < 0 Then Exit Do

Print "The circumference of the circle is: "; 2 * PI * r

Print

Loop

Print

Print "End of program!"

The program requests the user to enter the radius of a circle. If the entered value is greater than or equal to zero, the circumference of the circle is calculated and displayed.

You are then requested to enter another value. If you enter a negative value the loop is terminated and "End of program!" is displayed.

Remarks

The Do...Loop statement is the most universal programming loop and it can be used to emulate all other loops:

Example

i% = 0 For i%=1 To n%
Do // programsegment
If i% > n% Then Exit Do  
// programsegment  
Loop Next
 
Do While Inkey$ <> "A"
If Inkey$ = "A" Then Exit Do // programsegment
// programsegment  
Loop Wend
 
Do Repeat
// programsegment // programsegment
If Inkey$ = "A" Then Exit Do Until Inkey$ = "A"
Loop  

Even more powerful loop conditions can be created by combining the Do...Loop with the evaluation part of the While...Wend and/or Repeat...Until loops:

Local a$ = "ABCDE...Z", b$, n%

Do Until n% > Len(a$)

Inc n%

b$ = Mid$(Trim$(a$), n%, 1)

Print b$;

Loop While Upper$(b$) <> "."

Reads a sequential character from string a$, until the end of the string is reached and while the character string starts with something other than a full stop.

OpenW 1

Do While MouseK = 0 : Loop

Do While (MouseK And 1)

Box MouseX, MouseY, Add(MouseX, 10), Add(MouseY, 10)

Loop Until Upper$(InKey$) = "A"

When the left mouse button is pressed it draws a rectangle at the current mouse position, until a lowercase or uppercase "a" is typed on the key-board.

The following loop combinations are possible:

Do ... Loop
Do ... Loop Until
Do ... Loop While
Do ... Wend
Do ... Until

While ... LoopDo While ... Loop
While ... Loop UntilDo While ... Loop Until
While ... Loop WhileDo While ... Loop While
While ... WendDo While ... Wend
While ... UntilDo While ... Until

Repeat ... LoopDo Until ... Loop
Repeat ... Loop UntilDo Until ... Loop Until
Repeat ... Loop WhileDo Until ... Loop While
Repeat ... WendDo Until ... Wend
Repeat ... UntilDo Until ... Until

Do ... Loop
Do ... Loop Until
Do ... Loop While
Do ... Wend
Do ... Until

See Also

For Next, While Wend, Repeat Until

{Created by Sjouke Hamstra; Last updated: 03/10/2014 by James Gaite}