Loop
Do .. Loop
 
Parameters: NONE
Returns: NONE
 

      Do / Loop pairs in their most simplest form are a way to create unconditional infinite loops. The Do represents the start or entry point of a section of code that is to be repeated, while the loop acts as the jump back to start instruction, telling the running program to jump back to the preceding matching do instruction. So when a Do-Loop is executed, PlayBASIC will run any code placed inside the Do/Loop infinitely.

      Do / Loop pairs can also be used to created conditional loops by placing a conditional expression after either the Do or Loop statements.



FACTS:


      * Do / Loops must be paired.

     * The Do must preceed the Loop statement. You'll get an error optherwise.

      * None conditional Do / Loops will repeat a section of code forever, or until the loop is broken. Eg via EXIT or EXITDO

      * Not sure about loops ? Make sure you read the Loops tutorial!




Mini Tutorial #1:


      In this example, we see a Do-Loop being used to keep the program constantly running. This cde will randomly draw dots to the screen. It will do so, until the program is interrupted, by either the break key (ESC key) or clicking upon the close gadget on the window.


  
  Do
     DotC Rnd(800),Rnd(600),RndRGB()
     Sync
  Loop
  


      To examine more closely what's happening, lets step through the logic that this piece of code is telling PlayBASIC to perform.

  
  
  >> FIRST Loop
  Do     ; <<<<  Tell PB to remember this point in the program.
     DotC Rnd(800),Rnd(600),RndRGB()     ; <<<<  Draw Dot to the Screen at a random position
     Sync   ; <<<<  Tell PlayBASIC to display the screen to the user.
  Loop  ; <<<<  Tell PlayBASIC to jump back to the previous DO, and continue executing from there.
  
  >> SECOND Loop
  Do     ; <<<<  Tell PB to remember this point in the program.
     DotC Rnd(800),Rnd(600),RndRGB()     ; <<<<  Draw Dot to the Screen at a random position
     Sync   ; <<<<  Tell PlayBASIC to display the screen to the user.
  Loop  ; <<<<  Tell PlayBASIC to jump back to the previous DO, and continue executing from there.
  
  >> THIRD Loop
  Do     ; <<<<  Tell PB to remember this point in the program.
     DotC Rnd(800),Rnd(600),RndRGB()     ; <<<<  Draw Dot to the Screen at a random position
     Sync   ; <<<<  Tell PlayBASIC to display the screen to the user.
  Loop  ; <<<<  Tell PlayBASIC to jump back to the previous DO, and continue executing from there.
  
  >>  Etc etc.... Until program is stopped by the user.
  
  






Mini Tutorial #2:


      In this example, we see a conditional Do-Loop being used to keep the program constantly running. The program uses the computers Timer() value to execute the code within the Do-Loop structured for a period of 5 seconds (5000 milliseconds). Once that time is up, PlayBasic exits the Do-Loop and continues on. Making a conditional Do-Loop the equivalent of While/EndWhile loop.


  
  
; First we create variable called 'EndTime'. This variable
; will hold the current timer() value in milliseconds
; plus 5000.  Since there's 1000 milliseconds per second, this
; means we're setting the variable to be 5 seconds ahead of
; now
  
  EndTime=Timer()+5000
  
; Start Conditional DO/LOOP.  The code inside this do/loop will
; only be executed if the current Timer() value is bellow our
; expected end value.  When this occurs the PlayBasic will
; abort the Do/loop and continue on.
  
  Do Timer()<EndTime
     
   ; Randomly draw a Dot on the screen
     DotC Rnd(800),Rnd(600),RndRGB()
     
   ; Show/Refresh the Screen to the user.
     Sync
     
   ; Loop back to the DO statement to keep this program running
  Loop
  
  
  Print "Done - Press any key"
  
  Sync
  WaitKey
  
  
  







 
Related Info: Continue | Do | Exit | ExitDo | For | Loops | Repeat | While :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com