|
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:
x = 0
DO
x = x + 1
Print x
If x = 10 Then ExitDO
LOOP
Print "Loop complete"
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:
x = 0
DO
x = x + 1
Print x
LOOP x < 10
Print "Loop complete"
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.
|
|
|
Looking for More Tutorials?:
|
|