

                          [42m INTRODUCTORY PROGRAMMING [0m

                                     by

                             [3m  David Perkovic [0m


   Ed: Check out Tutes_&_CLI this issue for a hint on how to run AmigaBasic
  on Workbench 2. This is the beginning of a series on Basic, so if you like
  it, please let the author know.


[32m   <> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <>
[0m


   Hi there and welcome to the introductory programming column.  I hope this
  column will become a regular part of MegaDisc.  The aim of this column is
  to provide the basics of programming to the users out there who would like
  to learn but do not know where to begin.  I will first start with Basic and
  then move on to C depending upon your response, so write in and tell me
  what you would like.


[33m   About Basic
[0m
   BASIC (Beginners All purpose Instruction Code) was developed in 1964 by
  John Kemeny and Thomas Kurtz at the Dartmouth College in America.  The
  intention of Kemeny and Kurtz was to create a computer language that is
  easy to learn, use and teach.  Due to its simplistic nature Basic has
  become very widespread and is available on most home computers.

   Basic is an interpreted language meaning that each time the program is run
  each instruction is converted to machine language and then executed.  Due
  to this, Basic programs run fairly slowly as each time the program is run
  the conversion to machine code has to take place.

   Most other computer languages are compiled rather than interpreted.
  Compiled languages first convert their particular code to a separate
  machine language program and then each time the program is run the
  converted program is executed.


  [33m Why Basic?
[0m
   The reason why I chose Basic is that nearly every Amiga owner already has
  a copy of Basic on their Extras disk (if you have a version of Workbench
  greater than 2.04 you won't, as I believe Commodore exchanged Basic it for
  the Arexx language). This means hat you don't have to fork out a couple of
  hundred of dollars on a interpreter or compiler as you would have to for
  another language.

   The speed of Basic will not worry us as the programs you will be writing
  are reasonably small and thus will be executed at a reasonable speed.

   Basic has been criticised due to its nature allowing the use of badly
  structured code (i.e. code that is hard to understand and modify). In
  this column I will try to encourage people to write structured code that is
  easy to understand and not introduce statements that can lead to badly
  written programs.


  [33m A First Course in Basic
[0m
   As this is an introductory programming tutorial I will presume that you
  already know about Workbench, particularly how to open and load programs,
  if not have a read of the manuals that came with your Amiga.

   AmigaBasic is located on your Workbench extras disk so first load up
  Workbench and then place your extras disk in your drive and load the
  AmigaBasic interpreter.

   Once AmigaBasic has loaded you should see two windows, one labelled
  "Basic" the other "List".  These two windows behave like ordinary Workbench
  windows, as windows are selected by clicking anywhere with in the desired
  window.

   As mentioned above Basic is an interpreted language and the Basic window
  is the interpreter.  Whatever you type in the Basic window is interpreted
  and then if the command is valid, executed.  Whereas the List window is
  where a set of instructions (called a program) is temporarily stored.  From
  the List window the program can then be executed, stored on disk, edited
  etc.  In todays column I will be concentrating on the basic window, next
  time I will introduce the list window and show you how to create, load and
  save programs.

   The simplest basic command that produces anything useful by itself is the
  print statement.  The print statement allows the user to display text on
  the screen.  To use the print statement first select the basic window by
  clicking your mouse button anywhere within the basic window.  To display
  the words 'Hello world' the following command is entered:

[32m       print "Hello world"
[0m
   after pressing return the computer will respond by displaying 'Hello
  world'. The print statement displays on the screen whatever you type
  between the quotes(").

   Play around with the print statement and see what happens when you place
  different text within the quotes.

   If the above print statement is typed without the quotes (eg print Hello
  world) the compiler will assume that Hello and world are variables.
  Variables are a combination of characters that point to a place in memory
  where a certain value is stored.  Since we have not defined "Hello" and
  "world" to be variables the computer will respond with two zeros when the
  command is entered.

   To define a variable the following is entered into the basic window:

[32m   a = 1
[32m   b = 2
[0m
   The above statements told the computer to set the variable 'a' to equal 1
  and the variable 'b' to equal 2.  If now we instruct the interpreter to
  display 'a' and 'b' by typing

    [32m   print a b
[0m
   the computer will respond with 1 and 2.

   To declare a string variable we have to include the dollar sign($) as the
  last character in the variable name, for example:

    [32m   a$ = "this is a string variable".
[0m
   In the above command we have declared a$ to be the string 'this is a
  string variable' so now if we type in:

    [32m   print a$
[0m
   the string that was declared to be a$(i.e 'this is a string variable' will
  be displayed.

   As all string variables end with a dollar sign and all number integers
  don't an error will occur if we try to declare a number as a string or a
  string variable as a number.  Note that you can declare a number as a
  string variable if you include the number inside quotes as anything inside
  quotes is taken to be a string eg:

    [32m   a$ = "123".
[0m

   The print statement will also allow mathematical computations, for example
  to calculate 1 plus 3 the following command is entered:

  [32m print 1 + 3
[0m
   Once return has been pressed the computer will respond with the answer of
  1 + 3 which is 4.  If you would like the actual computation that was
  carried out to be displayed you should type in the following:

[32m   print "1 + 3 =" 1 + 3
[0m
   If you typed the above command in correctly the computer will respond
  with:

 [32m  1 + 3 = 4
[0m
   Experiment around with other mathematical computations.  The symbols for
  addition, subtraction, multiplication and division are +, -, * and /
  respectively.

   In an attempt to make life easier for Basic programmers the designers have
  allowed programmers to use the question mark in place of the print
  statement eg.

  [32m print "hello world"
[0m
   is equivalent to

 [32m  ? "hello world"
[0m
   I have mentioned this shortcut mainly as a time saving device for people
  typing in programs with many print statements.  My advice though, is not to
  use it as it is not a standard basic command and it decreases the
  readability of the program as a whole.


   Now that you have learnt how to program the computer to output text to the
  screen I will show you how to program the computer to accept input from the
  keyboard.  To demonstrate the input command type the following command:

 [32m   input test$
[0m
   After you have typed in the command and pressed return the interpreter
  will display a question mark and pause until the user has entered some
  text.  The interpreter will not continue until the user has pressed return.

   Once the above input command has been successfully executed what ever you
  typed in, the computer has stored in the variable test$.  If we now print
  out the contents of test$ by typing the following:

 [32m  print test$
[0m
   the computer will display what you had just typed in.

   Well that's it for today.  To keep you busy till next time I suggest you
  experiment around with the print and input commands and have a read of the
  Basic manual.  Remember the best way to learn a programming language is to
  play around with it yourself and learn from your mistakes.  In the next
  edition I will explain how to write, save, load and edit your first basic
  program and show you how your Amiga can make decisions.



 [32m  <> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <>
[0m

