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.
A FOR NEXT control loop is a type of loop that allows you to repeat a block of code a specific number of times. Here is an example of how to use a FOR NEXT loop in BASIC:
PlayBasic Code:
FOR i =1TO10PRINT i
NEXT i
This FOR NEXT loop will print the numbers from 1 to 10. Let's break down the code:
The FOR statement initializes a loop variable i to the value of 1.
The TO keyword specifies the end value of the loop variable, which is 10 in this case.
The block of code between the FOR and NEXT statements is executed as long as the loop variable i is less than or equal to 10.
The PRINT statement prints the value of the loop variable i.
The NEXT statement increments the loop variable i by 1 and checks if it is still less than or equal to 10. If it is, the loop continues. If not, the loop ends.
Here is the output of this FOR NEXT loop:
1
2
3
4
5
6
7
8
9
10
You can also specify a step value for the loop variable, like this:
PlayBasic Code:
FOR i =1TO10STEP2PRINT i
NEXT i
This FOR NEXT loop will print the numbers from 1 to 10, incrementing the loop variable i by 2 each time. The output of this loop will be:
1
3
5
7
9
You can also specify a step value that is negative, like this:
PlayBasic Code:
FOR i =10TO1STEP-2PRINT i
NEXT i
This FOR NEXT loop will print the numbers from 10 to 1, decrementing the loop variable i by 2 each time. The output of this loop will be:
10
8
6
4
2
Here are some additional details about the FOR NEXT loop in BASIC:
The FOR NEXT loop is a pretest loop, which means that the loop condition (the value of the loop variable) is checked before the loop is executed. If the loop condition is FALSE, the loop is skipped and the program continues with the next statement after the NEXT statement.
The loop variable i is a local variable that is only accessible within the loop. It is created when the loop starts and is destroyed when the loop ends.
The STEP keyword specifies the amount by which the loop variable is incremented or decremented each time the loop iterates. If you omit the STEP keyword, the default step value is 1.
You can nest FOR NEXT loops, which means you can have a FOR NEXT loop inside another FOR NEXT loop. Here is an example of a nested FOR NEXT loop:
PlayBasic Code:
FOR i =1TO3FOR j =1TO3PRINTstr$(i)+" "+str$( j)NEXT j
NEXT i
This nested FOR NEXT loop will print all possible combinations of the numbers from 1 to 3. The output of this loop will be:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
You can use the EXIT FOR statement to exit a FOR NEXT loop prematurely. For example:
PlayBasic Code:
FOR i =1TO10IF i =5THENEXITFORPRINT i
NEXT i
This FOR NEXT loop will print the numbers from 1 to 4, then exit the loop when the loop variable i reaches 5. The output of this loop will be:
1
2
3
4
Can you explain what the DO / LOOP control structure is in PlayBasic ?
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 =0DO
x = x +1Print x
If x =10ThenExitDOLOOPPrint"Loop complete"syncwaitkey
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 =0DO
x = x +1Print x
LOOP x <10Print"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.
Repeat / Until and While / EndWhile Control Loops In PlayBasic
In PlayBasic, the Repeat/Until control structure allows you to repeatedly execute a block of code until a certain condition is met. The syntax for the Repeat/Until structure is as follows:
Repeat
' code to be executed repeatedly
Until condition
The code block between Repeat and Until will be executed repeatedly until the condition is met. When the condition is met, the loop will exit and control will be passed to the next line of code after the Until statement.
Here is an example of a Repeat/Until loop in PlayBasic:
PlayBasic Code:
x =0Repeat
x = x +1Print x
Until x =10Print"Loop complete"syncwaitkey
In this example, the loop will repeat 10 times, printing the value of x each time. When x reaches 10, the Until condition will be met and the loop will exit. The message "Loop complete" will then be printed.
In PlayBasic, the While/EndWhile control structure allows you to repeatedly execute a block of code as long as a certain condition is met. The syntax for the While/EndWhile structure is as follows:
While condition
' code to be executed repeatedly
EndWhile
The code block between While and EndWhile 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 EndWhile statement.
Here is an example of a While/EndWhile loop in PlayBasic:
PlayBasic Code:
x =0While x <10
x = x +1Print x
EndWhilePrint"Loop complete"syncwaitkey
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 While condition will no longer be met and the loop will exit. The message "Loop complete" will then be printed.
This tutorial takes the new basic coder through the bare bones of the FOR / NEXT loop. The tutorial was recorded live and is uploaded virtually as is, warts and all. In this tutorial we use loops to draw simple graphical objects such as boxes moving through to making a grid of scrolling boxes.
Commands used in this tutorial. For / Next / Step / Exit / Continue / Print / Line / Box / Sync / Ink / RGB() / RND() / RndRGB() and possibly a few others.