Beginner Level: PRINT, +, -, *, /
Welcome to our NextBASIC programming guide. If you've read the manual that comes withthe ZX Spectrum NEXT, you may already be familiar with many of the commands - It's a great reference guide, but perhaps aimed at people who are not strangers to programming. I'll be introducing some complicated topics at an early stage if I believe they're relevant, but do my best to explain them in an easy to understand way.
Terminology: Commands are what we give the computer to tell it to do something.
I've created these tutorials, as I found that there was much that needed further explanation, or I sometimes didn't quite grasp the concept because the explanation was too complicated or brief.
Let's get started then! There's a lot to learn, and I hope my tutorials make it interesting and fun.
Let's get the computer to do something in BASIC, and I'm sure I'm starting where may others doing tutorials also have. Let's display some writing on the screen.
In the basic Editor, type in
PRINT "Hello World"
and hit the Enter key.
Congratulations, you've written a computer program. Well, an extremely simple one that uses a single command: PRINT. The text in quotes is what you're telling the command to display on the screen.
It gets better, not only can we print text, we can print numbers...
PRINT 67
will display 67 on screen, and we can start using some mathematical operators:
+ addition
- subtraction
* Multiplication
/ Division
Terminology: Operators are the actions in a calculation (add, subtract, multiply, divide) whereas operands are the values that are calculated with the operator. I don't expect anyone to remember this, but's it's something that you'll probably come across in programming at a later stage.
So lets put something to the test, lets use the computer as a computer and let it compute something: a simple calculator
PRINT 5+7
displays the number 12 on the screen. Play around with some simple maths, doing subtraction, division and multiplication, i.e. 11-4, 7*5, or 65/5
It's probably a good time to intruduce brackets if you want to group parts of the sum together so everything doesn't get jumbled up...
PRINT (5*7) - (72/5)
which gives 28.8 or -65 if you typed it without the brackets.
And just to get ahead of ourselves, lets mix numbers and strings together. We can type:
PRINT "There are 12 to a dozen"
or we can type
PRINT "There are ";12;" to a dozen"
Where we print test, followed by a number value followed by more text. Everything inside the quotes is treated as text. A couple of things to note with this, don't forget to leave a space inside the quotes between the text and the number, otherwise the output will read
There are12to a dozen
The semicolons either side of the number tell the PRINT command to join what follows, so it's joining a number to text in this case, and some more text at the end.
So, congratulations, you're now a computer programmer, you've written a program, and let your computer run it giving a result. You're heading in the right direction.
Beginner Level: REM, LET, Strings
We've started out well, printing some writing and numbers up on the screen, and doing some simple sums. But computers are expected to do so much more, and a large part of that is down to variables - Assigning a value to a letter, like in simple algebra. This lets us play around with the value before we use the result for a command.
We'll start off with a simple program that adds numbers. This time, we'll record the program into the RAM so it stays there for a while, and you can reference and edit it at will.
Terminology: RAM (Random Access Memory) is the computer's temporary memory that holds a written or loaded program. When you switch off the computer, the contents of the RAM are lost. You'll need to save a copy of your work before switching off the computer so you can load it in again when you turn the computer back on, this will be covered a little later on as our example programs become a little more meaningful.
You can use whatever program line numbers you like, but there's a unwritten convention where you order numbers in 10's, and sections in 100's or 1000's. The gaps let you add in extra code without having to renumber all the lines.
Let's start off with a REM to remind you what the program is about, or what the section of the program does. They are not read by the computer, purely there as little notes for yourself or anyone trying to make sense of your program.
10 REM assign variables
20 LET a=1
30 LET b=2
40 REM print the values of the variables out
50 PRINT "a=";a
60 PRINT "and b=";b
Type RUN to get it all going, and you'll see on screen:
a=1
and b=2
The program has created a and b, assigned values to them, and then displayed those values in the PRINT statements. Rememberm everything inside the quotes is treated as text.
Add a semicolon ; to the end of line 40, so it reads
50 PRINT "a=";a;
run the program again, and instead of the second PRINT statement of line 60 going to a new line, it stays on the same line, added on. You'll need to tweak line 60 now, adding in a space at the beginning immediately after the first quote.
60 PRINT " and b=";b
and we have perfection!
Let's get our program to calculate something now.
Add this line:
60 PRINT "a+b=";a+b
and run the program again, you'll see the result of this on a new line.
So, we've assigned numbers to variables, we can also assign text to special variables called strings, and to do that, we have a variable name, followed by a dollar sign for "string".
Add into the program:
35 Let n$="Bert"
45 PRINT n$;" tells me that ";
You can see why we don't number the program lines 1, 2, 3, 4, etc. The gaps in numbering has allowed us to add in more code, and we still have room to add in more if necessary. Eventually, there may come a stage where we will need to renumber the lines of code, we'll deal with that a little later.
So, we've written a simple program that's calculated something and displayed writing and numbers onto the screen.