To illustrate the points in this tutorial, we'll be using what's known as "direct mode". This means
that the commands that we type in aren't stored in a program; they're immediately executed as we type
them in. For instance, try typing PRINT"HELLO"
without a line number, and hit "Return".
The word "HELLO" is printed immediately to the screen. Now type NEW
(if there's a program
currently in memory), then 10 PRINT"HELLO"
and press "Return". The PRINT
command does not run until you give the RUN
command, and you can type RUN
as
many times as you wish, because the command is stored in the current program.
Creating Variables
To create a variable, simply assign a value to a 1 or 2 character variable name:
A=10
The variable A
now contains the number 10. By default, numbers are stored as floating-point,
which means that you don't have to stick with whole numbers. You can also create variables that only
store integers (whole numbers). This requires adding a "%" character after the variable name:
X%=50
Additionally, a variable can store a string of characters, normally just called a string. Unlike writing numbers, you have to explicitly tell the computer not to interpret the contents of a string by putting quotation marks around the string you want to store. These quotation marks are not stored as part of the string; they're only placeholders. The modifier for string variables is "$":
NM$="WHITE FLAME"
Displaying Variables' Contents
The PRINT
command, in addition to printing messages, can display the contents of any
variable. Typing
PRINT A
will show that the variable A
actually does contain the number 10. Try not to confuse
this with a command like PRINT "A"
, which will print out the letter A. The PRINT
command is also not limited to one variable that it can print. You can separate variables by a comma,
which will format the output into columns, or separate them with semicolons, which will print them
next to each other. Try these out:
PRINT 1,20,300,4,50,600,7,80,900 PRINT "A =";A;"ISN'T THAT NICE?"When printing numbers, a leading space is left to make room for a negative sign, if the number is less than zero. Also, a trailing space is left so that numbers printed one after each other don't butt together. Strings have no extra padding around them, so make sure you put spaces between words if you are printing two of them next to each other:
A$="WHITE" B$="FLAME" PRINT A$;B$ PRINT A$;" ";B$Intro to Operators and Functions
The value that you assign to a variable does not have to be just a single number or character string. BASIC supports a full range of mathematical operators: +, -, * (multiplication), / (division), and (exponentation). Parentheses are also supported for more complex equations. The following functions are also available: ABS, ATN, COS, EXP, INT, LOG, RND, SGN, SIN, SQR, and TAN. Boolean logic is supported with the AND and OR operators. Click on each link to view a description of the function. We'll get into the specifics of each function in a later tutorial.
Most functions get a number passed to them, which can be either an explicit number (SQR(5)
),
a variable (INT(X)
), or another function (SQR(ABS(Y))
). Functions are used to
calculate a result, and you must do something with this result, like PRINTing it or assigning it to a
variable, to make a complete, understandable line of BASIC code:
PRINT SQR(4*4+5*5) A=(B+1)/(C+1)All of these functions can use both floating-point and integer variables, but remember that when you assign a number to an integer variable (like
X%
), only the whole number portion is stored:
X%=7/3 READY. PRINT X% 2 READY.There are also functions that work on strings. The following accept a string, and also return a string: LEFT$, MID$, and RIGHT$. ASC, LEN, and VAL accept strings and return numbers, while CHR$ and STR$ take numbers and return a string.
Only one operator, +, can be used on strings. It takes two strings and creat