PlayBasic TUTORIALS


     New to programming and need some help getting started ? - Well, then our tutorial section will help kick start your programming adventure. If you're looking for more Source Code / Tutorials & Media ? - Then remember to visit the PlayBasic Resource board on our forums.

Found #1 item in Loop category


 Can you explain what the DO / LOOP control structure is in PlayBasic ?

By: Kevin Picone Added: January 7th, 2023

Category: All,Loops,Do,Loop,Learn to Code


Can you explain what the DO / LOOP control structure is in PlayBasic ?


   In PlayBasic, the DO/LOOP control structure allows you to repeatedly execute a block of code a specific number of times or until a certain condition is met. There are two forms of the DO/LOOP structure: the DO/LOOP form and the DO/LOOP WHILE form.

  The DO/LOOP form has the following syntax:

DO
  ' code to be executed repeatedly
LOOP


  The code block between DO and LOOP will be executed repeatedly until the EXIT DO statement is encountered.

  Here is an example of a DO/LOOP loop in PlayBasic:

PlayBasic Code:
x = 0

DO
   x = x + 1
   Print x
   If x = 10 Then ExitDO
LOOP

Print "Loop complete"
sync
waitkey

COMMANDS USED: PRINT | SYNC | WAITKEY |


  In this example, the loop will repeat 10 times, printing the value of x each time. When x reaches 10, the Exit DO statement will be encountered and the loop will exit. The message "Loop complete" will then be printed.

  The DO/LOOP WHILE form has the following syntax:


DO
  ' code to be executed repeatedly
LOOP Optional While condition


 The code block between DO and LOOP will be executed repeatedly as long as the condition is met. When the condition is no longer met, the loop will exit and control will be passed to the next line of code after the LOOP statement.

 Here is an example of a DO/LOOP WHILE loop in PlayBasic:

PlayBasic Code:
x = 0

DO
   x = x + 1
   Print x
LOOP  x < 10

Print "Loop complete"

COMMANDS USED: PRINT |


 In this example, the loop will repeat until x is no longer less than 10. The loop will print the value of x each time it is executed, and when x reaches 10 the LOOP WHILE condition will no longer be met and the loop will exit. The message "Loop complete" will then be printed.

 So, in general, you might use a DO/LOOP loop when you want to repeat a block of code an unknown number of times, or when you want to repeat a block of code until a certain condition is met. On the other hand, you might use a DO/LOOP WHILE loop when you want to repeat a block of code a specific number of times, or when you want to repeat a block of code while a certain condition is true.








Viewing Page [1] of [0]





Looking for More Tutorials?:



 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]