BM-Header-Blog

Beginner Level: PLOT, RND, GOTO, GOSUB

We look at simple navigating within a NextBASIC program

At it's simplest, when you write a BASIC program, the computer follows each line of code and executes it. Not executing in the sense of killing it, but carrying out the instructions (or commands) it sees.

Almost immediately, we find that this is too limiting, if the program needs to loop around, repeating something, we may need to go back to the beginning, or jump into a separate section of code (what we call a routine).

First example, we want to print random dots on the screen:

PLOT RND*255,RND*172

Each time you enter this, the dot will appear in a different place on the screen.

PLOT prints out a single dot (a pixel) on the screen where you tell it to within the X and Y co-ordinates of the screen area with 0,0 being in the bottom left corner. Here, we're setting a random value for the X co-ordinate at any number between 0 and 255 (the width of the screen), and same for the Y co-ordinate for the height of the screen (172 being the useable area). You can skip the RND* and just use a specific value, but we want a random starfield in our example.

If we want to do this 5 times, we can write this...

10 PLOT RND*255,RND*172
20 PLOT RND*255,RND*172
30 PLOT RND*255,RND*172
40 PLOT RND*255,RND*172
50 PLOT RND*255,RND*172

It's a lot of typing for a simple result, and you guessed it, there's a much easier way. We can reuse that PLOT command over and over by looping back to the beginning, continuing indefinitely, until we stop the program running by pressing BREAK.

Replace line 20 with:

20 GOTO 10

You can keep lines 30, 40 and 50, for now they've become useless, as they will now be ignored because of the loop returning the program to the beginning. It executes line 10 to PLOT a random dot on the screen, and then loops back to line 10 using GOTO. When we run this now, we see a whole starfield building up.

Beginner Level: FOR, TO, STEP, NEXT

Let's stay on the subject of loops, and limit the number of times the loop works.

We'll start a new program that prints out 20 stars, and then stops.

10 FOR l=1 TO 50
20 PLOT RND*255,RND*172
30 NEXT l

l is just a made up variable name, you can use any letter you want, we used l for "loops". Try and keep your variable names relative to what they are for. We're using it to count a number of loops, and any lines you write between the FOR and NEXT statement will be repeated that many times.

We can also do nested loops, that's one loop within another.

10 FOR x=0 TO 255
20 FOR y=0 TO 172
30 PLOT x,y
40 NEXT Y
50 NEXT X

Here, we'll get stripes being plotted from the bottom (y=0) to the top (y=172), going from left (x=0) to right (x=255) of the screen. The nested loop is completed first before the main loop.

The demonstration was a little slow drawing all those lines with individual dots. We're incrementing (adding) 1 to each loop. Saw we wanted to draw stripes that were 5 pixels apart, we can use a STEP command to increment the loop value in steps of 5.

Edit Line 10 to include a STEP command as follows:

10 FOR x=1 to 255 STEP 5

... and now you'll see stripes being drawn spaced 5 pixels apart. You can add STEP 5 at the end of line 20 to create a grid of dots.