8-Bit Software Online Conversion

AN INSIGHT INTO PROGRAMMING by Peter Davy This article, which refers particularly to BBC BASIC, first appeared in the Summer 1988 issue of "Microwaveband", the journal of the now defunct organisation CABE - Computers and Adult Basic Education. The computer uses letters to stand for numbers. For example the letter A could represent 25. Type at the keyboard A=25 and press the RETURN key. Pressing the RETURN key is what you do to enter what you have just typed in. Now type PRINT A and press RETURN. The number 25 will appear on the screen showing that our previous instruction has been accepted by the computer. In computer jargon, A is known as a numeric variable and the act of entering A=25 has assigned a value of 25 to the numeric variable A. Numeric variables don't have to be restricted to one letter. Groups of letters can be used - for example TEMPERATURE=22, XBC=-40 and INTEREST=14.25 would all be understood by the computer. Now type TWO TIMES THREE and press RETURN. The word "Mistake" appears on the screen. The computer has only a limited vocabulary and doesn't understand "TWO TIMES THREE". If you would like the computer to give the answer, type PRINT 2*3 and press RETURN when the answer 6 will appear on the screen. For humans an X means multiplication but the computer needs an asterisk. Remember our variable A, which we set at 25? Type A=A+20 and press RETURN. Newcomers to computers are often shocked by a statement such as this. They think of A=A+20 as being an absurdity. But to a computer A=A+20 merely means let the value of A be 20 more than its present value. Type PRINT A and press RETURN. The number 45 appears on the screen. Now type PRINT B and press RETURN. A message "No such variable" appears on the screen. This is the computer telling you that it cannot print B as no value has been assigned to it. If you progress further with programming you will come to appreciate this feature of the BBC computer. In similar circumstances many computers will print the value of any un-assigned numeric variables as zero. How do we make the computer handle words? We use what are called string variables. These are just the same as numeric variables except they have a dollar sign at the end. Type A$="MILK" and press RETURN, then type PRINT A$ and press RETURN. The word MILK appears on the screen showing that the computer has accepted the instruction to assign the word MILK to the string variable A$. Type B$="PUDDING" and press RETURN. Type PRINT A$+B$ and press RETURN. The word MILKPUDDING appears on the screen. If you want a space between MILK and PUDDING then type PRINT A$+" "+B$ and press RETURN. The posh word for adding strings together is "concatenation". We all know many thousands of words. The computer knows only about 100. Three of its words are IF, THEN and ELSE. Type IF A$="MILK" THEN PRINT"MILK IS THE WORD" ELSE PRINT"MILK IS NOT THE WORD" and press RETURN. A message MILK IS THE WORD appears on the screen. Now type IF B$="MILK" THEN PRINT"MILK IS THE WORD" ELSE PRINT"MILK IS NOT THE WORD" and press RETURN. This time a message MILK IS NOT THE WORD appears on the screen. This is a simple example of the computer carrying out a test and taking action according to the result. So far we have been using the computer in what is called the command mode. We have typed in an instruction and the computer has carried it out. A computer program consists of a list of instructions which the computer carries out one after the other. The computer recognises an instruction as part of a program by the fact that it starts with a number. Type 10 CLS:PRINT:PRINT and press RETURN. The computer does not carry out these instructions but just accepts them as a program line. Type 20 INPUT"What is your name",NAME$ and press RETURN. Type 30 PRINT"Hello ";NAME$;"!" and press RETURN. Type 40 PRINT:PRINT and press RETURN Type 50 GOTO 20 and press RETURN. You have completed a short program. To make it work all you have to do is to type RUN and press RETURN. The computer first carries out the instructions in line 10. CLS means clear the screen. The word PRINT on its own means print a blank line on the screen. The two PRINT statements in line 10 ensure that when we do get some printing it will be fully in view and not lost off the top of the screen. Line 20 causes the question: What is your name? to appear on the screen and then the computer waits for you to type in your name and press RETURN. Whatever you type in will be stored in NAME$. Line 30 causes the message: Hello Robert! to be printed on the screen (if Robert is what you typed in!) Line 40 produces two blank lines and line 50 tells the computer to go back to line 20 which again asks for a name to be typed in. We have created what is known as an infinite loop. The GOTO instrution in line 50 is one to beware of. Too liberal a use of GOTO leads to what are called spaghetti programs, which, although they may work, are very difficult to follow. Good programmers hardly ever use GOTO. Programming can easily be learned from books as long as you have a computer at hand to practise on. Two books for beginners which I found useful are: "BBC BASIC for Beginners" by D. Smith (pub. by Melbourne House) and "The BBC Microcomputer for Beginners" by S. Dunn and V. Morgan (pub. by Prentice Hall). The most difficult thing I found to learn is the art of de-bugging - finding and putting right mistakes which cause your program either to do something which is not intended or to crash altogether. As time goes on you make fewer such mistakes and you get better at finding those you do make. Happy programming!