The Basics of Programming

What is BASIC

BASIC stands for Beginners All purpose Symbolic Instruction Code. This means that the language is designed for beginners in programming which can be used for different types of applications and uses simple to understand instructions. Instructions can be as simple as INPUT, PRINT, LET, The Amiga has several versions of Basic including AmigaBasic, ABasic, HisSoft Basic, Ace Base, AMOS, Blitz Basic, AmiBlitz3 and others.

Memory

For computers to process information and to store information, it needs to allocate and access this information in memory. So, in programming, we need to use variables which are just named areas of memory which the program can store information such as numbers or text. We use the word variable because the contents of them can change.

a) Variables

A variable name can be from one character long to many characters and may be alpha numeric and can only start with a letter. For example, count, name, total, age, address, balance, TotalUsers, Number501 etc. Variables, in some languages have to be defined first before that can be used. This helps with determining what the variable can contain and to allocate a certain amount to memory to store the information. For example, BASIC you can use the DEF and DIM statements. The DEF statements are optional in BASIC.

Variable types: Integer is a whole number e.g. 1, 2, 45, 193, a float is a decimal number e.g. 3.1, 4.1234, 100.9, 0.6666, a long is a very long number e.g. 12372905, a char is a single character e.g. "M" and text is a string of characters e.g. "LONDON".

e.g.

DEFINT number ' Define an integer number 
DEFLNG longnumber ' Define a long-integer number
DEFSNG number ' Define a single length number DEFSTR code$ ' Define a string variable called code DEFDBL longvalue ' Define a double length floating point variable

Note: The text after the apostrophe (') called a comment or remark, which is used to document the code. You may also have remarks on their own lines, as long as it starts with the REM instruction.
Note that string variables, which stores letters, digits and other symbols always end with the dollar sign ($) to indicate that its a string.
To set values for variables, you would use the assignment or LET statement which is basically in the format: [LET] variable = expression where variable is the destination and expression is a value or a a formula or some other thing that can return a value. The LET part is optional e.g.

number = 129

name$ = "Simon Walker" ' Note: characters and string values are always surrounded by quotes 
count = 1

code$ = "A"

To make programs more useful, you can also do arithmetic such as adding, subtracting, multiply, divide and so on. A list of arithmetic symbols is below:

Symbol Description
+ Add
- Subtract
* Multiply
/ Divide
^ Power to

For example to add two numbers together and divide it by another number:

number = (120 + 18) / 2

Note that the 100 + 58 is enclosed in brackets. In programming, multiple and divide are calculated before any adds or subtracts. If no brackets were provided, then the result would be 100 + 29 = 129 instead of 158 / 2 = 79 (with the brackets).
You can also use variables in your expressions, as follows:

number = (150 + number2) / number3

So if the number2 variable contains 60 and the variable number3 contains 10 then the result would be (150 + 60) / 10 = 21

b) Arrays

Variables are okay to store single values, but if you wanted to store 10s or 100s of values, then having 10s or 100s of variables e.g. number1, number2, number3 to number100 would be too clumbersum and wasteful. Therefore, you can use an array or a collection of variables together which share the same name. An array has a index, which allows you to access any of the individual values using for format: arrayname (index). The index can be a number or another variable, this makes arrays ideal for use with loops and specifically counted loops. You will still need to declare arrays first using the DIM statement, like variables and specify the maximum number of values in the parenthesis '(n)'. E.g.

DIM numbers(100)
 

For example, if you want to store all the numbers from 1 to 100 into the array called numbers, you can use a for loop.

FOR count=1 TO 100    
   
  numbers(count) = count   
NEXT count

The for statement reads like this: set a variable called count to value 1, check that the condition 'count<=100' is true before running the instructions between the FOR and NEXT statements. WHEN it reaches the NEXT statement it will increment the value of count by 1 and epeat the loop until the count has reached 100.

So to access the 56th item in array numbers, you can do this:
mynumber = numbers(56)

You can also initialize an array with constants using the READ and DATA statements:

DIM figures(10)
FOR count=1 TO 10
    
  READ figures(count) NEXT count DATA    45, 67, 123, 59, 12, 4, 23, 89, 66, 90

Here we have an array called figures that can store ten numbers. The FOR NEXT loop will count from 1 to 10 and use the READ statement to read the next value from the DATA statement and store the value in the the figures array using the count as an index.

For strings, we can have simple arrays. So we specify a value in parenthesis, which is a number of strings . So, in this example we have 4 country strings.

DIM countries$(4)
countries(1) = "France"
countries(2) = "Germany"
countries(3) = "Poland"
countries(4) = "Italy"

You may want bigger arrays, you can do this by adding more dimensions. For example, you want a 8 by 8 table (say, for a chess game), then you can add another index value to the DIM statement like this:
DIM chessboard(8,8).

Then you can set or get a item on the board using the index values:

ROW = 4
COL = 3
chessboard(ROW, COL) = WHITE

Next Page