Home PageDownloadBeginner's ZoneAmateur's ZoneExpert's ZoneTips PageProgramming LinksE-Mail Me
GFA Beginners | Getting Started with the GFA Enviroment | Beginners to Windows

The Basics of BASIC
by Anthony Hayward

If you wanted someone to change a lightbulb, and they didn't know how to do it, you might write them a list of instructions, or "algorithm", to do it:

1. Turn off the power
2. Unscrew the old lightbulb
3. Screw the new bulb in
4. Turn the power on
5. If it doesn't work, get another lightbulb and go back to 1, otherwise finish, and go and have a cup of tea.

Programming computers is much the same, except they have no common sense at all. They do exactly what you tell them - if you forgot to tell them to turn the power off, they'll change the bulb with the power still on, and as the flesh melts from their fingers they'll shout "error! exception!" untill you "turn them off".

BASIC stands for Beginner's All-Purpose Symbolic Instruction Code. It was originally designed to be an easy way of getting computers to do what you want (although the "beginner's" bit is misleading - it can be just as powerful as other languages). Basically (ha!) you write a list of instructions to the computer which it then follows in order.

A simple GFA Basic program might look like this:

OPENW #1
INPUT "What's your name?",a$
PRINT "Hello "+a$
INPUT "Press return",b$
CLOSEW #1

This is a list of orders to the computer. The first one says "open a window for our program to work in". Then we have INPUT. This displays the message (the text between the quotes) on the screen and lets the user type something in. This is then stored in a$, which is a "variable" - it refers to part of the computer's memory which the program will use to keep track of what's going on. The $ on the end is nothing to do with America - it indicates that a$ is a "string" variable, ie. it contains a sequence of words, letters, numbers and punctuation marks. We could have called it anything we like, as long as it had a $ on the end - nm$, what_has_been_entered$ and f99bx3jui2$ would all have done. The PRINT line then displays something else on the screen - a nice hello message. This is made up of two strings stuck together - "Hello " and the name typed in, which is stored in a$. To stick strings together you use +. The last two lines wait for the user to press return to make sure they've had time to see the hello message, and closes the window.

This program is boring because it just runs in a straight line. Programs get interesting and tricky when they have structures and loops in them, like this one:

OPENW #1
INPUT "Enter a whole number more than 0:",n
total=0
FOR i=1 to n
  total=total+i
NEXT i
PRINT "The sum of the first "+STR$(n)+" numbers is "+STR$(total)
INPUT "Press return",b$
CLOSEW #1

How does this program work? Instead of a$ in the INPUT statement, there's n, which is a numeric variable. Just like a$, it points to a place inside the computer where things the program is interested in are stored. This time though, it's a number and not a word. Then we set up another variable, total, which will hold the total of our sum. total=0 stores the value 0 in total, so we start with a total of 0. The next bit is called a for-next loop. This strangely-named construction is used to repeat something a certain number of times. The first time, i=1. The second time, i=2, and so on. So if we typed in 5 for n, and wrote out what the program is doing, we'd get:

total=total+1
total=total+2
total=total+3
total=total+4
total=total+5

Now how does it make sense to say total=total+1? Surely total can't be equal to itself plus one. The thing is, this statement, total=total+1 is an assignment - the computer works out what total+1 is, then puts this into the bit of memory referred to by total. So the effect is to increase total by 1.

So at the end of the for-next loop, total contains the number 1+2+3+4+...+(n-1)+n. It only remains to display this answer on the screen. We use print again, but this time instead of a string variable we have a numeric variable. The way we turn this into a string is to put STR$() around it - STR$() is a "function" that turns a number into a string which represents the number - for example STR$(5) is "5". There are lots of other functions available, for strings, numbers, and all sorts of things - for example, PRINT STR$(SQR(25)) will print 5 - the square root of 25. Look in the help file for a list of the many functions you can use.

The other really important thing you need to know is the IF statement. IF lets your program make decisions, according to the values of the variables. For example:

INPUT "type in a number: ",num
IF num<5
  PRINT "the number is less than 5"
ELSE
  PRINT "the number is not less than 5"
ENDIF

It's pretty clear what all this does apart maybe from num<5 - this compares the value of num and the number 5. If num is less than 5 (the pointy arrow < means "less than") then all the commands between "IF" and "ELSE" are followed ("executed"), otherwise all the commands between "ELSE" and "ENDIF" are executed. Other comparisons you can do are equals (=), more than (>), less than or equal to (<=), more than or equal to (>=).

Variants on "IF" are: WHILE...WEND.

n=4
WHILE n<99
  PRINT str$(n)
  n=n+1
WEND

This will do the commands inside the WHILE...WEND until n stops being less than 99. You can also use REPEAT...UNTIL:

REPEAT
  INPUT "Please type in 'wibble': ",a$
UNTIL a$="wibble"

The program will keep asking the question until the user types in "wibble".

Another big thing you need to know about the basics of programming, and that's arrays. Imagine you wanted to store all your emplyees' names and their ages in a program. You'd need lots of string variables:

nm1$="Jim"
age1=23
nm2$="Bob"
age2=28
nm3$="Marmaduke"
age3=71

...

INPUT "Whose age do you want to know?",a$
IF a$=nm1$
  PRINT STR$(age1)
ENDIF
IF a$=nm2$
  PRINT STR$(age2)
ENDIF
IF a$=nm3$
  PRINT STR$(age3)
ENDIF
...

This is obviously a bit silly. What you want are arrays - a big list of variables with the same variable name. To set up the arrays, use DIM:

DIM nm$(30),age(30)

This sets up storage space for 30 names. Now you can just read nm$(4) to get the name of your 4th employee. The age-finding routine above becomes just

FOR k=1 to 30
  IF a$=nm$(k)
    PRINT STR$(age(k))
  ENDIF
NEXT k

You can have arrays of more than one dimension. Imagine you wanted to store a 340-page book in memory. you might set up an array like this: DIM word$(20,25,340). Then word$(3,8,201) will give the 3rd word on the 8th line on the 201st page of the book. And if you wanted to store 5 books in this way, you'd just use DIM word$(20,25,340,5). (Note - this will be pushing your computer's memory a bit!).

Procedures and Functions

Procedures and functions are used to structure programming, if it wasn't for them there would be no way to do a repetitive task (repetitive tasks are what computers do best) without writing out the same code over and over again. It's very hard for more seasoned programmers to explain procedures and functions to beginners because they are taken for granted, they are what programming is about. I'm going to use an analogy to try to explain them. You're in Las Vegas, using the one armed bandits. You are getting very weary of putting your money in, pulling the handle, and seeing whether you win or not. So you get a robot called Tim to do it. Whenever you call his name he puts the money in, pulls the handle and you can see what the result is. This is the basis of procedures. In GFA Basic procedures look like this:

PROCEDURE Tim
  ' DO STUFF
RETURN

Now to do what is in the procedure (call it) you simply put:

Tim

This will do everything in the procedure tim before returning to the line after you called it. Procedures and functions can be passed parameters. Parameters are a list of numbers which the procedure or function then uses to affect what it does. So it's like you calling Tim like this, "Tim, do it 2 times." This makes Tim do his stuff 2 times before stopping and waiting for you. Here's how that works in GFA Basic:

Tim(2)
PROCEDURE Tim(n&)
  ' Now the value in n& is the value that you've called tim with
RETURN

This allows procedues to do different things depending on what paramaters you call them with, this is very useful.

A function is just a procedure which returns something, in our analogy it is like Tim the robot putting in the money, pulling the handle and saying "WIN" if you won or "NO WIN" if you lost. If you've heard of subroutines before then don't worry that's just another name for procedures. Look at some examples, almost every one will have procedures and/or functions in them. If you see a procedure being called with a @ before it then don't worry, it's no different, @ is short for GOSUB and neither are necessary in GFA Basic (functions always have to be called with @ or FN before them though).

I hope this tutorial has given you an insight into BASIC programming. I always find I learn more when I have an idea of a program I want to write. Try to think of a simple program that you will be able to use to do something time consuming for you, when you have a good idea just try your best to write it. Then keep going back to the program and making it better, this way you will learn better programming techniques. But remember don't run before you can walk, or you may stumble into a brick wall. :-)

GFA Beginners | Getting Started with the GFA Enviroment | Beginners to Windows
Home PageDownloadBeginner's ZoneAmateur's ZoneExpert's ZoneTips PageProgramming LinksE-Mail Me