Timo 2019-03-01 11:08 (Edited)
I'm still working on this tutorial and I will add more chapters soon. I would like to get feedback especially from users with little experience. Is it well explained?
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.
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:
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.
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:
"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:
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.
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"
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.
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
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.