How To

BASIC Tutorial for Beginners

22

Timo 2019-03-01 11:08 (Edited)

Introduction

This is a beginners tutorial for people with little or no experience in programming. It's made for LowRes NX, but keeps its focus on the basics of the BASIC programming language (2nd generation). You will learn about simple text output and input, commands, parameters, variables, calculations, conditional expressions, loops, jumps and arrays.

Before you start this tutorial, make sure you know how to create program files and how to run them. This is explained in the "Getting Started" chapter of the manual.

Some Simple Programs

Let's start with the most simple program possible. Type the following line:

PRINT "HELLO WORLD!"

Now run the program and you will see the text HELLO WORLD on the screen. You just learned your first command PRINT. It outputs any text on the screen. Most commands need some more information for what they should do. In this case it's the text you want to output. An information for a command is called parameter. To indicate that a parameter is a text, you have to put it into quotation marks. This is called string.

Let's try something similar:

PRINT 100

This program outputs the number 100 on the screen. This time we don't use quotation marks, so the program knows we are actually talking about a number and not a text. PRINT can output strings as well as numbers, but most other commands are stricter and you have to use the correct types. We will soon see what exactly are the differences between strings and numbers.

Now let's write a program with more lines:

PRINT "HELLO!"
PRINT "THIS IS LOWRES NX."
PRINT "HAVE FUN!"

You may have expected it: This program outputs three lines of text. But you see that a program usually consists of a list of commands. The important thing to understand is, that these commands are executed one after another. LowRes NX reads one line, executes the command, goes to the next line, executes the command, and so on. This happens very fast, it looks like all lines appear at the same time. But it's important to know that only one command gets executed at a time, one after another.

Variables

Now we will make things a bit more complicated. Try this program:

T$="HELLO WORLD!"
N=100
PRINT T$
PRINT N

It will simply output one string and one number. We could have done this easier, right? So what is happening here? Let's look at the first line again:

T$="HELLO WORLD!"

"HELLO WORLD!" is a string, we know that already. But instead of writing it to the screen, we assign it to the variable T$ by using the equal sign. A variable is a location in the memory with a name. We can use any names from one to 20 letters, except of command names like PRINT. Variable names for strings always have to end with a dollar sign.

The second line is similar:

N=100

Now instead of a string we have a number and we assign it to the variable N. You see there is no dollar sign, this means that N is a number variable. If you try to assign a string to a number variable (T="HELLO") or a number to a string variable (N$=100) you will get the error message "Type Mismatch".

The last lines are:

PRINT T$
PRINT N

Parameters can be constant values (like "HELLO" or 50) or variables. If you pass a variable as a parameter, the command will use its assigned value.

Ok, but we still don't know, why we made it so complicated using variables. The advantage of variables is, that they can have any value, not only the one you write in your program. Let's write a new program:

INPUT "NAME:";T$
INPUT "AGE:";N
PRINT "HELLO ";T$;"!"
PRINT "YOU ARE ";N
PRINT "YEARS OLD."

This is your first program with user interaction. Nice, isn't it?

We learn a new command here: INPUT. It outputs a prompt and then lets the user input something with the keyboard. This input is assigned to a variable. If you want a text input, make sure you use a string variable (with a dollar sign). If you want a number, use a number variable (without dollar sign).

We also see that PRINT can output several values in one line using the semicolon, and that you can mix strings with variables.

Calculations

So far our programs didn't have a lot of usefulness. It would be more interesting to actually do something with the user's input. BASIC has a built in calculator, which we can use anywhere we work with numbers. Try this program:

INPUT "AGE:";N
PRINT "AGE NEXT YEAR:";N+1
D=N/10
PRINT "DECADES:";D

At first we use the calculation N+1 as a parameter. PRINT will output the result, but won't change the value stored in the variable. Then we use the result of the calculation N/10 to store it in the variable D.

BASIC knows addition (+), subtraction (-), division (/), multiplication (*) and exponentiation (^). It handles complex calculations in correct mathematical order. For example PRINT 10+2*3 outputs 16. You can use parentheses to change the default order, for example PRINT (10+2)*3 outputs 36.

Maths work well with numbers, of course, but what about strings? Try this:

A$="HELLO"
B$="LOWRES"
C$=A$+" "+B$+" NX!"
PRINT C$

Yes, you can concatenate strings using the + sign. But that's it, the other calculations don't work with strings.

Let's make a little experiment with strings and numbers to show their difference. Can you guess the output of the following program before you run it?

PRINT 1+2+3
PRINT "1"+"2"+"3"

Conditional Expressions

What we have now is not much more than a calculator with some text input and output. To make it really interactive, we want to react to the user and execute different commands depending on their input. Try this program:

PRINT "1 OR 2?"
INPUT N
IF N=1 THEN PRINT "NUMBER ONE"
IF N=2 THEN PRINT "NUMBER TWO"
IF N<1 OR N>2 THEN PRINT "THAT'S WRONG!"

Run it several times and enter different numbers to see the reactions. The magic is happening in the IF command. The word IF is followed by a conditional expression. That's basically a check which can result in true or false. In the case of true the command after the word THEN is executed. Otherwise the command is skipped and the program continues in the next line.

In the last line you see that conditional expressions can be more complex. You can combine several checks with the words AND (all checks must be true) or OR (at least one check must be true).

We can even compare strings. Let's try it:

PRINT "YOUR FAVORITE"
PRINT "COLOR?"
INPUT C$
IF C$="RED" THEN PRINT "LIKE A ROSE"
IF C$="GREEN" THEN PRINT "LIKE A TREE"
IF C$="BLUE" THEN PRINT "LIKE THE SEA"
PRINT "THANKS!"

Depending on the color you enter you may get a special message.

Loops

Type this simple calculator program:

INPUT "NUMBER A:";A
INPUT "NUMBER B:";B
PRINT "SUM:";A+B

Now run it, enter both numbers and you will get the result. Not bad, but now the program ended and you have to restart it to do another calculation. This is far from ideal. We want to make it repeat automatically. Add the line DO at the beginning and LOOP at the end:

DO
INPUT "NUMBER A:";A
INPUT "NUMBER B:";B
PRINT "SUM:";A+B
LOOP

Run the program again and now it repeats forever until you quit it. Much better. This is the most simple type of a loop. When the program executes the DO command, it memorizes its position and continues. When the LOOP command is reached, the program remembers the line of the DO and jumps back to it.

Sometimes you want to repeat your code only a specific number of times. There is another type of loop for this, it's called the FOR-NEXT loop:

INPUT "COUNT TO:";N
FOR I=1 TO N
PRINT "NUMBER ";I
NEXT I
PRINT "DONE!"

A FOR loop uses a number variable to count. In this example we use I, but you can use any you want. The loop ends with the command NEXT, followed by the count variable.

You can even combine different loops by putting one into another. Let's make our last program repeat forever:

DO
INPUT "COUNT TO:";N
FOR I=1 TO N
PRINT "NUMBER ";I
NEXT I
PRINT "DONE!"
LOOP

Arrays

We already know that variables are very useful. Let's create a simple program where you can enter your player name.

PRINT "PLAYER 1"
INPUT "NAME:";NAME1$
PRINT "---"
PRINT "P1: ";NAME1$

Nothing new here. What if we had two players instead of only one? Not a big deal, we can just use some more variables and duplicate the code for each player:

PRINT "PLAYER 1"
INPUT "NAME:";NAME1$
PRINT "PLAYER 2"
INPUT "NAME:";NAME2$
PRINT "---"
PRINT "P1: ";NAME1$
PRINT "P2: ";NAME2$

Ok, that works! It's just a little bit more to write. Now make the program for 5 players! Wow, no, write all this code 5 times?? We already know loops to repeat code, the only problem is that we want to use different variables for each repetition. The solution for this problem are arrays.

An array is like a simple variable, but it has space to store several values. Before you can use one you have to say how many values you want to store in it. This is done using the DIM (dimension) command.

DIM NAME$(5)
FOR P=1 TO 5
PRINT "PLAYER ";P
INPUT "NAME:";NAME$(P)
NEXT P
PRINT "---"
FOR P=1 TO 5
PRINT "P";P;": ";NAME$(P)
NEXT P

With only a few more lines we handle now 5 players. Let's have a look at the code. The first line (DIM) creates an array of strings called NAME$ with space for 5 elements.

Then we make use of a FOR loop counting from 1 to 5. The important part is the INPUT line. Instead of a simple variable we wrote NAME$(P). If the program finds parenthesis after a variable name, it knows that this is an array. We just have to write an element number to make clear which one we want to access. This number is called index. In our example we use the variable P instead of a number, so in each repetition of the FOR loop the index increases and the program asks for every player's name.

In the second FOR loop we print all the names on the screen again, so you see that really all different names were stored in just one array.

Arrays can be used just like simple variables, but they can help you to avoid writing the same code several times.


Timo 2019-03-17 15:09

I added a chapter about loops.


XenonLab 2019-03-25 00:07 (Edited)

Great job Timo! exactly what I wanted! a small START UP manual to be translated also in my language (Italian), obviously with your consent.

Thanks to the official manual and this tutorial you are writing, I can create my complete manual, with progressive examples and conversion of my old sources into C-64 BASIC and QBASIC for DOS.

Now I have all the necessary information for the LowRes NX BASIC interpreter.

Really useful and well written, easy for absolute beginners.

Thank you very much Timo.


Timo 2019-05-30 08:23

I added a first chapter about arrays (DIM). Another is planned for two-dimensional arrays.


Tinycloud778 2019-05-30 16:41

Great explanation I like it


DJMoffinz 2019-10-29 08:40

This is very helpful, thanks!


tactics_pvp 2020-02-03 04:27 (Edited)

AWESOME write up. I was able to follow along with no problems.

Described as if explaining it to a five year - which that five year old is me. Thank you.


Troopbuildz 2020-09-11 14:32

Nice!


DarkNorthDao 2020-10-05 10:59

Just wondering, how would one be able to save a game? Like I want to do some sort of rpg.


Timo 2020-10-05 16:28 (Edited)

To save a game state, there is no simple command. You have to use the command POKE to write your values to a special area of the memory. It simulates a cartridge with some battery powered RAM, like some Game Boy games had.

Please check the manual chapter "ADVANCED TOPICS > Hardware Reference > Persistent RAM" and the manual for the commands POKE and PEEK.

LowRes NX always tries to be authentic to old hardware, so some things seem a bit complicated, but this is how it worked then.


brooc210 2021-05-16 17:25 (Edited)

Cool!


Anywhere Games 2021-06-08 04:57

Sup! I’m new working on a game called Luigi’s Clean It! So that Dim command that was last could work for multiple Highscores like some Aracade games do?


Ty 2021-08-30 03:05

Hey


Best Broz 2023-12-13 21:24

I'm reading this and trying their codes out


Best Broz 2023-12-13 21:52

I Love the way you explain the this


Best Broz 2023-12-13 21:52

I Love the way you explain the this


Best Broz 2023-12-13 22:08

😎😎


Log in to reply.