Hello, and welcome to the fourth part of the Runtime Basic
                     
Tutorial series:
                     
                     
                     
               FIRST STEPS IN BASIC
                     
                     
                     
     Following our discussion of the way the ST handles mathematical
                     
operation in last month's column, this month Philip Johns is going to help
                     
expand your knowledge into the way basic keeps track of operations
                     
using named variables and constants.
                     
                     
                     
     Variables are one of the  most powerful functions any version of
                     
BASIC possesses. If BASIC could only work with absolute values,
                     
numbers like 1, 8, -16 and so on, most programs would be extremely
                     
difficult (if not impossible) to write. To avoid problems, BASIC allows
                     
you to use something called a "variable".
                     
     These variables are similar to the letters used in algebra to
                     
represent unknown factors, but they work in a slightly different way, so
                     
if you hated algebra at school, don't panic.
                     
     A variable must have a name by which BASIC can identify it. Any
                     
combination of letters and numbers can be used to make up the name,
                     
although there are some restrictions. They are:
                     
                     
                     
     1. No variable can begin with a number.
                     
                     
                     
     ("1VARIABLE" is not permitted as it begins with a number).
                     
                     
                     
     2. The first eight characters must be unique.
                     
                     
                     
     ("VARIABLE1" and "VARIABLE2" both have the same eight letters at
                     
the beginning, so BASIC won't be able to tell the difference).
                     
                     
                     
                     
                     
               WHAT'S IN A NAME..?
                     
                     
                     
     Quite a lot, really. Most variable represent something meaningful,
                     
so its a good idea if the name reflects what they mean. This will make
                     
your program easier to read, and when you try to change the way the
                     
program works you have some reminders as to what each variable does.
                     
                     
                     
     Lets take a simple example to see how variables work. I have five
                     
oranges and six lemons and need to know how much fruit I have
                     
altogether. If you think in terms of variables, the problem will look
                     
something like this:
                     
                     
                     
          oranges = 5
                     
          lemons = 6
                     
          fruit = oranges + lemons
                     
          PRINT fruit
                     
                     
                     
     This can be abbreviated down to a much shorter, but less
                     
understandable form like this:
                     
                     
                     
          o = 5
                     
          l = 6
                     
          f = o + l
                     
          Print f
                     
                     
                     
     From this example, you can see how a variable can be almost
                     
anything. The equals sign ( = ) is used here as an assignment operator, to
                     
give a variable the value that comes after the sign.
                     
     When working with variables, it is always good practice to
                     
"declare" all the variables you intend to use at the beginning of the
                     
program. Variables are often declared by setting them all to zero. This
                     
has a two fold advantage. If something goes wrong while your program is
                     
running, you can check the state of all the variables to see which ones
                     
have been used by your program.
                     
     Declaring variables also helps to stop you getting confused, and
                     
perhaps using the same variable for two different pieces of information.
                     
For instance, say you declare a variable called Cost at the beginning as
                     
COST = 5. Sometime later in writing the program, you forget that COST
                     
has already been used and put in a line COST = 10. This will cause
                     
problems as COST may not always be what you think it is. If you declare
                     
all the variables at the beginning, you can check to see what names have
                     
already been used.
                     
     For the same reason, VARIABLE NAMES SHOULD NEVER BE REUSED.
                     
Once a variable has been used, even if you think you've finished with it,
                     
don't be tempted to reuse the name as you may get very confused about
                     
what value the variable is supposed to hold.
                     
                     
                     
                     
                     
                     
                     
               PROGRAM NUMBER ONE
                     
                     
                     
     With the idea of the PRINT statement, mathematical rules and
                     
variables firmly in mind, its time for us to try a simple program to bring
                     
these ideas together. First, we shall introduce another BASIC keyword.
                     
                     
                     
     The INPUT statement is used when BASIC needs to get some form
                     
of input from the user. It take the form of:
                     
                     
                     
     INPUT "text, text, text",variable
                     
                     
                     
     After the INPUT keyword, any text enclosed in quotation marks
                     
( " and " ) will be printed to the screen, just like a PRINT statement. Text
                     
is optional in an INPUT statement, but it helps to let the user known
                     
what they are supposed to be doing when the machine seems to stop. The
                     
variable at the end of the statement is used to hold the user input. Like
                     
all variables, it must be unique and not begin with a number. Now, to see
                     
how the INPUT statement works, let's try this:
                     
                     
                     
     10 INPUT "How many oranges ",oranges
                     
     20 INPUT "How many lemons ",lemons
                     
     30 fruit = oranges + lemons
                     
     40 PRINT "Total = ";fruit
                     
                     
                     
     (NOTE: The line numbers used here are optional in most versions of
                     
BASIC. They are used here to make typing the listing easier. Check your
                     
user manual to see if your BASIC needs line numbers)
                     
                     
                     
     Before you run this program, it is a good idea to select SAVE from
                     
the file menu and save the program. Remember to give it a unique name
                     
(PROG1 might be good) so you can load it and use it again. When the
                     
program is safely stored on disk, RUN it and see what happens. Follow the
                     
on screen prompts to see the result.
                     
     Even a program as simple and straightforward as this can be
                     
shortened. For example, line 40 can be changed to:
                     
                     
                     
     40 PRINT "Total = ";oranges + lemons
                     
                     
                     
     This change to the program means that line 30 has become
                     
redundant, as the addition part of the sum is now included in the final
                     
line.
                     
     Remember to resave the program each time you make an alteration
                     
to it. Try and use different names each time (PROG1A, PROG1B, etc. ) so
                     
you can keep track of the changes you make, and go back to a previous
                     
version if something should go wrong.
                     
                     
                     
     Moving on from there, suppose I can buy my fruit at a fixed price,
                     
the same no matter what the fruit is. We can expand our program so it
                     
will calculate the cost of the fruit for us as well.
                     
     As the price is fixed, it can be defined as a constant at the start
                     
of the program. BASIC treats a constant in the same way as a variable,
                     
but it's up to the programmer to ensure that the value remains the same.
                     
Here's the program, amended to include costs:
                     
                     
                     
     5 cost = 0.15
                     
     10 INPUT "How many oranges ",oranges
                     
     20 INPUT "How many lemons ",lemons
                     
     30 PRINT "Total = ";oranges + lemons
                     
     40 PRINT "Price = ";(oranges + lemons)*cost
                     
                     
                     
     You may want to try re-numbering the program lines so they read
                     
from 10 to 50, rather than having the odd line 5 at the beginning.
                     
                     
                     
                     
                     
               BEFORE NEXT TIME...
                     
                     
                     
     Here are a couple of things to try on your own before the next
                     
issue of Runtime.
                     
                     
                     
     1. Try changing the cost program so it asks you what the cost of
                     
fruit is at the start. This will involve changing COST from a constant to
                     
a variable.
                     
                     
                     
     2. Change the program so oranges and lemons cost different
                     
amounts (HINT: try using two constants called ORANGE_COST and
                     
LEMON_COST and performing the multiplication separately then adding
                     
the results to get the final total).
                     
                     

Takaisin

(C) Marko, Suomen Atari-sivut / ArkiSTo 2003