PLAYBASIC TUTORIALS


FOR NEXT control loops in PlayBasic

FOR NEXT control loops in PlayBASIC

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 = 1 TO 10
 PRINT i
NEXT i
COMMANDS USED: PRINT |



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 = 1 TO 10 STEP 2
 PRINT i
NEXT i
COMMANDS USED: PRINT |



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 = 10 TO 1 STEP -2
 PRINT i
NEXT i
COMMANDS USED: PRINT |


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 = 1 TO 3
 FOR j = 1 TO 3
  PRINT str$(i) +" "+str$( j)
 NEXT j
NEXT i
COMMANDS USED: PRINT | STR$ |


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 = 1 TO 10
 IF i = 5 THEN EXITFOR
 PRINT i
NEXT i
COMMANDS USED: PRINT |


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

Operators and Compisons

This question came in recently.. So here's a quick response on the subject. You can find more on the PlayBASIC Help files (online link bellow)

What are ">=, >, <, <=, == , !=" all these and why would they need to get used?"

Most of those are comparison operators. So we use them to compare the terms on either side of the operator.

>= compare if the LEFT term is Bigger than or equal to the RIGHT term

<= compare if the LEFT term is Less than or equal to the RIGHT term

> compare if the LEFT term is Bigger than the RIGHT term

< compare if the LEFT term is Less than the RIGHT term

== compare if the LEFT term is equal to the RIGHT term

!= compare if the LEFT term is NOT EQUAL to the RIGHT term

So in regards to why we would use them; well whenever we need to check if two terms are numerically the same (Equal too) or similar or even not equal we can use the appropriate operator to do the comparison for us which will then return a boolean result of TRUE(1) or FALSE(0).. back to us.

Read more about Comparisons and Operators in our Help Files
COMPARISONS TUTORIAL OPERATORS TUTORIAL

PlayBASIC Tutorial: Intro To Functions

     Hi welcome to our Intro to PlayBASiC Functions tutorial. In this tutorial we start out with some revision of Gosub / Return statements, which are used to create a simple sub routine. Sub routines are changes in program flow, allowing the programmer to execute a chunk of code that's external to the section they may be writing, then upon completion the control returns to the caller. This is the same basic model that functions introduction to our programming, except functions are more formalized. Meaning they have some strict rules about syntax and intro a new concepts such as scope changes, which sub routines don't have.

The types of functions shown in video are very simple, initially we start by taking a sub routine that prints rows of text and convert that to function. Through this process we encounter our first problem which is variable scope and look at ways to solve it, such as making our variable global, or better yet passing a variable into the function as a parameter . Later in the video, we create our own custom distance function, as well building a function that does some simple string manipulation

NOTE: This video was recorded alive with only a few changes for length..






Get Source Code Example For This Tutorial

PlayBASIC Tutorial: From Arrays To Types (Intro To Types)

    This tutorial picks up where the previous variables to arrays tutorial left off, in that it takes the array code, demos that code then we set about converting the parallel array approach shown in the previous tutorial and we build a structure (TYPE) to hold each characters properties. Once the type has been defined that includes all the required properties, we then define a typed array that will house the collection of characters. Later in the video take a look at using typed lists also. So if your struggling with types this could be a good place to start.



Links:

    * PlayBASIC Tutorial: From Arrays To Types (Intro To Types) Source Code

PlayBASIC Tutorial: Saving a New Project ( IDE BASICS )

    This video takes the new PlayBASIC programmer through a couple of examples that demo how to save a new project and create a new project folder using the PlayBASIC IDE (Source Code editor) -

    1) In example one we write th classic Hello World example and then show how to save it as a project to the desktop

    2) In the second example we take this further by adding some sprite media to the project manually then demo how you might load the sprites and display them.