DO / LOOP

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Core Language > Program Flow >

DO / LOOP

 

Description

 

Define a block of program statements that are executed repeatedly for as long as certain conditions are met.

 

Syntax

 

Do [{While | Until} LogicalExpression]

 ...

 {statements}

 [Exit Do]

 [Iterate Do]

 {statements}

 ...

Loop {{While | Until} LogicalExpression}

 

Returns

 

Parameters

 

Remarks

 

Restrictions

 

Do/Loop executes cycles without giving other statements inside the loop any time to process windows events.

 

If your loop has statements that need to process windows events application will not be able to respond to windows events giving the impression it is hanged. In this case consider to change from Do/Loop to While/Wend loop (While/Wend has an automatic DoEvents inside it) or add a DoEvents statement inside you loop. This will give some time to process events without hanging your application.

 

See also

 

Examples

 

Thanks to Petr Schreiber for the following script example

USES "Console"

 

Dim CountSheep As Byte

 

Console_Writeline("Pure DO/LOOP")

 

' -- Classic infinite loop

' -- NOTE: To leave loop you must use EXIT statement

CountSheep = 0

Do 

 

  INCR CountSheep 

 

  Console_Writeline("-- Sheep :"+STR$(CountSheep)) 

 

  If CountSheep = 10 Then Exit Do

 

Loop

 

Console_Writeline("Press ENTER...")

console_Readline

 

Console_Writeline("Test DO WHILE/LOOP")

 

' -- Loop with condition which must be TRUE to PERFORM cycle

' -- NOTE: If the condition does not equal to TRUE on start, NO cycle is performed

' - try to change 'CountSheep = 0' to 'CountSheep = 10'

CountSheep = 0

Do While CountSheep < 10 ' -- Valid for CountSheep = 0 ... 9

 

  INCR CountSheep 

 

  Console_Writeline("-- Sheep :"+STR$(CountSheep)) 

 

Loop

 

Console_Writeline("Press ENTER...")

console_Readline

 

Console_Writeline("Test DO/LOOP UNTIL")

 

' -- Loop with condition on end which must be TRUE to LEAVE cycle

' -- CAUTION ! This way at least one loop is performed always

CountSheep = 0

Do 

 

  INCR CountSheep 

 

  Console_Writeline("-- Sheep :"+STR$(CountSheep)) 

 

Loop Until CountSheep = 10 ' -- If this is true we leave loop

 

Console_Writeline("End of demonstration, press ENTER to quit...")

console_Readline