BASIC TUTORIAL   PARTS 3 & 5 As there was an unfortunate omission on MEGADISC23 I have decided to include the text for Chapter Three along with Chapter Five. Also in this drawer is "EducatingSally" - this is a program which is used in conjunction with Tutorial #5, and includes all references from part #3 of the Tutorial - note that the less finished version of "EducatingSally" which Part #3 refers to was in MD23. [Ed: Mea culpa!]  ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ##  BASIC TUTORIAL   PART THREE by  Frank Wilkinson In this chapter we are going to look at the outline of the Program we are going to construct. As well as learning to apply the commands we have already learnt we will also learn some new ones. The first thing any programmer must do is sit down and work out just what it is that he want his program to do. So! The very first thing we will want is a SETTING UP SECTION, usually called the INITIALIZATION. Here we have to decide how big our screen will be and what our COLOR scheme will be and allow for the colours we want to use. Then we will need to INITIALIZE any windows that we are going to use. Here also we can initialize any arrays we may want to use. After that we will need to set up a TITLE SCREEN. We learnt how to make a title screen with our Colors program last chapter (#2). This will do very nicely with a few modifications. As our program is going to be a multi-purpose Educational program we could possibly have a second screen briefly describing its purpose. Then we will need some means of making a choice. Do we want to learn addition or multiplication or spelling or maybe some geography? ( This program will be very flexible. Once you have learnt how to construct the program. You will be able to patch in your own MODULES so that you can tailor the program to teach anything from ASTRONOMY to ZOOLOGY. When we have made our choice then we have to have the means to get where we want to go. ABOUT LABELS I have used 'LABELS', But I haven't explained them yet. So before we go any further let's take a look at 'LABELS'. AMIGABasic doesn't have line numbers like most other BASICS instead it uses 'LABELS'. A LABEL can be any string you like! Letters or letters and numbers or if you like just numbers. These are some examples. Begin: Init: Addition: Addition2: 100 : RestAWhile: ReadAFile: WriteAFile: 10000000000 : They should be descriptive and they can even contain BASIC KEYWORDS. If a LABEL is a number you must always put a space after the number before the colon. All LABELS are recognised by AMIGABasic when they end with the colon. AMIGABasic stores the names of all the LABELS and where they are. So that it knows where to go when you 'GOTO LABELNAME'. If our labelname is a subroutine it must always end with a RETURN STATEMENT. (More about SUBROUTINES later.) We use LABELS to write our routines, such as Spelling, Addition. We will learn how to make these routines as we build up the program. We will also make use of SUBROUTINES, The beauty of a SUBROUTINE is that it can be called from any part of our program. We can then use the SUBROUTINE and when we have finished with it we can RETURN to the place we came from. This enables us to save DISK SPACE, REDUCES THE SIZE OF OUR PROGRAM, but best of all we only have to write the routine once. I have started a LIBRARY OF SUB-ROUTINES for you. In our CHOICE MENU we should always have the ability to end the program cleanly so our last Item will always be "Finished with this Program". We can think about other CHOICE menus which we can use in our Modules. We may want to have the choice of: 1. Multiply two single digit numbers 2. Multiply two double digit numbers. 3. Multiply Double digit by Triple digit number. So you see you can have a CHOICE within a CHOICE.  INITIALIZATION  Now that we know how our Program is going to be organized we can set about our First task - 'INITIALIZATION'. In the INITIALIZATION you can put a few REM STATEMENTS. These can describe the Program and give details of who wrote it. Feel free to put your own name in here because when you adapt it for your own needs. You will have written it yourself. It is very hard to define Copyright. Here we are using Microsoft BASIC for the Amiga. Microsoft own the copyright to AMIGABasic. I have to use Microsoft's KEYWORDS and PARAMETERS or the program will not work. So in a way every program in Microsoft Basic is copyrighted by Microsoft. So whatever the pros and cons are of copyright you can rest assured that this TUTORIAL is for you to USE. I want you to use it. That's what it is all about.  A FEW COMMANDS  Before we go to the Program there are some commands we have to learn. 1......GOTO 2......GOSUB 3......IF.....THEN 4......IF.....THEN.......ELSE 5......RANDOM numbers more advanced 6......LINE 7......RIGHT$,LEFT$ and MID$. Not many but very important. 1. The GOTO command tells Basic the next thing we want to do is elsewhere in the program and we want to go there. Now if you remember when we were talking about labels I said a label was used with the GOTO and GOSUB commands like this:- GOTO Labelname. The program immediately goes to the label Labelname and continues from there. In other words we can skip about in our program by using the GOTO command. This can be very DANGEROUS if you aren't careful. With too many GOTO's following each other you can soon get lost. GOSUBs are a better way to move about your program because when you are finished they always RETURN you to the line after you called the GOSUB. It is not always practical to use a GOSUB command. In these cases you will just have to use a GOTO. In our program you will see several GOTOs and they are essential in this context. When we are finished with the part of the program we have gone to. I have always sent the program back to the choice screen. 2. GOSUB. A GOSUB SUBROUTINE is different from a SUB...END SUB. A SUB....END SUB is called and used just like a command. But a GOSUB is a whole new ball game. It can be very long and it can call other GOSUBs and they will always return you to the Statement after you called them. They can be used any where in your program, but if we are to make sense out of our program and make it possible for others to read we will keep them altogether in a little section of their own just before the SUB...END SUBS at the end of the program. The GOSUB uses the same kind of syntax as the GOTO like this:- GOSUB Labelname The GOSUB then goes to the LABEL Labelname, does whatever it is designed for and when it reaches the RETURN command at the end of the SUBROUTINE it goes back to the statement after the line it was called from. In our program you will see these GOSUBs and they look like this:- IF answer1=number1+number2 THEN GOSUB Correct :GOTO RestAWhile1 IF answer1<>number1+number2 THEN GOSUB Wrong :GOTO Try1 Here we have two IF...THEN statements which check to see whether the answer to a question is true or false. If it is true then the GOSUB sends the program to the SUBROUTINE 'Correct'. It performs the tasks in the subroutine and when it reaches the RETURN statement at the end of the subroutine the program RETURNs to the next statement which says GOTO RestAWhile. Which is an INKEY$ statement which makes the computer wait for a key to be pressed. If it is false (i.e. we got the answer wrong) then the program goes to the SUBROUTINE 'Wrong' does what it has to, then RETURNs to the GOTO Try1 label which allows us to have another try at getting the answer right. By putting multiple statements on one line, if the IF statement fails it will drop through to the next line and completely ignore anything on the line after the failed statement. It will only read the statements after the THEN if the first statement is true. 3. IF......THEN This is the simplest form of the IF....THEN statement and it only does one thing. IF (this statement is true) THEN (do this) IF (this statement is not true) THEN (do nothing and go to the next line) In our choice section you will see several IF......THENs like this:- IF n<1 or n>7 THEN GOTO Choice IF n=1 THEN GOTO Addition ........... ........... IF n=7 THEN GOTO Cleanup GOTO Choice What this does is make some decisions for us. The first line says IF the number we type in is less than "1" or greater than "7" then go back to Choice and have another try. The second line says IF the number we have typed in is equal to "1" THEN off you GOTO Addition and do whatever has to be done. The last line says IF the number we have typed in is equal to "7" THEN we have finished with this program. GOTO Cleanup and close down. 4. IF......THEN......ELSE This is a little bit more advanced than the 'IF.....THEN' statement. It can decide between 2 alternative statements. IF statement one is true THEN do statement two. ELSE IF statement one is false do statement three. It sounds a little complicated but it works like this. In our imaginary program we could have a variable which is set by a RANDOM NUMBER GENERATOR and IF the random number is less than 5 say. We want the program to print "HEADS". And IF it isn't less than 5 THEN we want to print "TAILS". Here's how we would do it:- A=INT(RND*10) IF A<5 THEN PRINT "HEADS" ELSE PRINT "TAILS" 5. RANDOM numbers again. Whilst we are talking about RANDOM NUMBERS. I have used a slight variation of the RND command in our program this time and you will find it very useful if you need specific numbers between a range of numbers. Say you want to generate numbers between 50 and 100 and nothing else. You would use a command like this: Number=Starting number + ((Ending number-Starting number)*RND) Which in our case would be:- Number=50 + ((100-50)*RND) Using this form of the RND command we will only get random numbers between 50 and 100. I have used this method in the arithmetic section of our program. 6....LINE There are several forms of the LINE command and the simplest one will just draw a LINE:- START TO END COLOR LINE (xposition,yposition) - (xposition,yposition),red Try to imagine your screen as a sheet of graph paper. Like you used at school. 'x' position 0 5 10 15 20 25 30 0------------------------------------------------------------------- |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| Y |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| p |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| o 5|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| s |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| i |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| t |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| i |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| o 10|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| n |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| ------------------------------------------------------------------- The numbers across the top are the 'x' co-ordinates (position) of the Pixel you want to start or finish at. The numbers down the side are the 'y' co-ordinates (position) of the Pixel you want to start or finish at. A PIXEL is a Picture Element and in MED-Res you have 640 Pixels across the screen and 256 down the screen. If you are drawing in a WINDOW remember the borders, they reduce the number of Pixels you have to draw on. To about 631 across and 241 down, depending of course on the size of your window. So if your LINE statement says:- LINE (5,5)-(40,5),red It means start at the '5th' Pixel across and the '5th' Pixel down. Draw a line to the '40th' Pixel across and the '5th' Pixel down. And I want the color of the line to be red. Remember you can only use the color red if you have set up your palette like we did in our colors program and then set our variable to red. Otherwise you will have to remember which palette number is red and use that number. The next variation of the LINE command draws an empty Box or Rectangle. LINE (5,5)-(40,40),red,b In this case the (5,5) is the TOP LEFTHAND corner of the BOX. and (40,40) is the BOTTOM RIGHTHAND corner of the BOX and this will give you something like this:- (5,5) ....|----------------------------------|.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....| |.......... ....|----------------------------------|.......... (40,40) THE empty BOX will be drawn in red. Another variation is by using 'bf' instead of 'b' like this:- LINE (5,5)-(40,40),red,bf This statement will do exactly the same as the previous one with one major difference. Instead of an empty BOX you will get a beautiful all in living color red Box. i.e it fills the empty BOX with the red color. Hence 'bf' = BOXFILL There is another LINE command and that is:- LINE (5,5)-(40,5) Draws a straight line Line -(40,40) This one starts where the first one finished and Draws a straight line to '40,40 like this:- 5,5 40,5 -------------------------------------------| | | | | | | | | | 40,40 We use the 'bf' LINE command in our "Educating Sally" program. When you have run the program, look at the listing. You will find it in every section of the program that I have written so far. In the next chapter. You will find that I have converted it to a SUBROUTINE along with several others. Hopefully this will give you a practical demo of how and why we use SUBROUTINES. 7.....RIGHT$ This is one of a group of THREE commands which handle STRINGS. i.e "This is a STRING" Any group of CHARACTERS enclosed in " " (Double Quotes) is a STRING. Nam$="Educating Sally" If we say in our Program 'PRINT Nam$' the computer will PRINT Educating Sally. "Educating Sally" has 15 characters. Go on count them. Don't forget the space between "Educating" and "Sally" is also a character just as are 8,7,*,_,&,>,<. PRINT RIGHT$(Nam$,5) The Command RIGHT$ tells the computer to PRINT the last 5 characters of the string Nam$. The last 5 characters are S,a,l,l,y so the computer will PRINT Sally Conversely (I looked it up in the dictionary) the command LEFT$ tells the computer to start at the left or start of the string. So if we say:- PRINT LEFT$(Nam$,9) the computer will PRINT:- Educating. Now comes the tricky bit. I always have to think about this one. MID$ This tells the computer we want something from the MIDdle of the string. But where in the middle. Here we have an extra PARAMETER. PRINT MID$(Nam$,4,3) | " extra parameter" This says get the string called Nam$ which we know is "Educating Sally" start at the 4th characters and PRINT 3 characters. ( 456 ) This gives us:- cat. To see if you have got this by the short and curlies, see if you can unravel this one without the aid of the computer: Ani$="liontigerstorkorkygorillamarmadillo" PRINT MID$(ani$,10,5);" ";MID$(ani$,19,7);" ";RIGHT$(ani$,9);" "; PRINT MID$(ani$,10,5);" ";MID$(ani$,23,5);" and ";MID$(ani$,14,5); PRINT " the ";MID$(Nam$,4,3). Now on with the Program. In our INITIALIZATION we will use the Colors program and so the choice of Screen,Window and colours will be as is. That is a Medium Resolution Screen with 8 Colours and we will use the biggest Window we can for our TITLE screen. Later we may wish to change the size and colours of our window. When we do I will explain what I have done and how you can customize your own screens and windows. After we have set up our Screen, Window and Colours we can the go about designing our TITLE SCREEN. First lets give it a label. The obvious one is:- TitleScreen: I will not write it twice just load the program 'EDUCATING SALLY' and read the REM STATEMENTS. I have Initialized the SCREEN and first window. I have also created WINDOW 3 in each section. If you look carefully you will see that it is the same window in each section but with a different name. We can do this as long as we remember to close this window when we are finished using it. And we do that with this command:- WINDOW CLOSE 3. I have implemented all the Addition choices. Which are:- 1.....Add Single digit numbers. 2.....Add Double digit numbers. 3.....Add Three digit numbers. 4.....Add mixed digit numbers. All the other choices will say:- Not Implemented yet Press any key to continue. Study the listing carefully and you will see that to implement the Subtraction, and Multiplication is only a matter of a few minor alterations and if you wish make a copy of the listing and see if you can do it yourself. The Division will be a little harder as you have to take care of the bits that are left over (i.e. The remainder ) in a Division like this 333 / 10 answer=33.33333333333333333333333333333 for ever. or 33 with 3 over. Think about it for a while because next time we are going to start on the Spelling and Geography. I will leave the other Arithmetic till last. But to give you some idea of what to do... Change the '+' in the PRINT USING "####";number1;:PRINT "+" to a '-' or a '*' sign. Then change the LOCATE 8,10:PRINT "Add up the lefthand column " to LOCATE 8,10:PRINT "Subtract the lefthand column " etc., etc. You won't want these lines LOCATE 8,10:PRINT "Type in the carry number " INPUT " ";answer3 LOCATE 6,10:IF answer3=0 THEN PRINT " " ELSE PRINT STR$(answer3) LOCATE 8,1:PRINT " Multiply is a bit harder but it should test your new skills.  ************************************************************************  Basic Tutorial   Chapter Five  by  Frank Wilkinson In this chapter we are going to put the finishing touches to our first real program. There is not a lot that is new in this part. The only command that is completely new is the VAL() command. This command is used to convert a STRING into numerical value, as we discovered in an earlier chapter. If we convert a number to a STRING we can more easily manipulate the individual parts of the number. If we have a numeric variable 'A' containing the number 586441 we can convert the number to a STRING variable with the comand:- A$=STR$(A) and now we have a STRING of ASCII Characters and not a number. You will remember from previous chapters that the commands to extract individual parts of the STRING are:- LEFT$(A$,?) MID$(A$,?,?) RIGHT$(A$,?) Where the '?' represents a number or a position in the STRING. If you are not sure about the use of these command go back to chapter Three and read it again. At the same time you can find the commands in your Basic Manual and study them there. As I pointed out earlier I am glad the tax man doesn't use STRINGS to calculate how much tax we have to pay, because when we add STRINGS we just join the STRINGS together. a=123 b=456 a$=STR$(a) a$=" 123" b$=STR$(b) b$=" 456" If we add these STRINGS we get:- X$+Y$= 123 456 **************************************************************** 'Example 1 a=123 b=456 a$=STR$(a) b$=STR$(b) x$=a$+b$ PRINT "a="a,"a$="a$,"Length a$="LEN(a$) PRINT "b="b,"b$="b$,"Length b$="LEN(b$) PRINT "x$="x$,"Length x$="LEN(x$) PRINT "val(x$)="VAL(x$),"length VAL(x$)="LEN(x$)-1 **************************************************************** If you cut out Example 1 in a text editor or if you can't manage that yet just carefully type it in to basic and you will get an output like this:- a= 123 a$= 123 Length a$= 4 b= 456 b$= 456 Length b$= 4 x$= 123 456 Length x$= 8 val(x$)= 123456 Length VAL(x$)= 7 Now before we have anyone ringing up from Germany or elsewhere in the middle of the night and saying: "In Chapter 5 you have made an error. There is no such command as LEN(VAL(x$))." I have cheated just a little, to try and make something clearer. If you look at the output CAREFULLY you will see:- a= 123 Not what I told Basic, which was "a=123" What does that extra space mean? Well Integer Numbers (there are many more types of numbers: floating point, real, complex, imaginary) can be regarded for our purpose to be of 2 types. 1. Positive 2. Negative In BASIC the "+" sign is not displayed but the number has a space in front of it. On the other hand a negative number is displayed with a "-" in front of it like so:- a= 123 b=-123 So each number is actually 1 character longer than the number of digits it contains. When these numbers are converted to STRINGS then the space or "-" sign are included. So that when we add the two small STRINGS " 123" AND " 456" We get a larger string containing all 8 characters " 123 456" and our 2 numbers of 3 digits each become one large string of 8 characters. This part also applies to negative numbers. Adding 2 negative STRINGS "-123" + "-456" will give "-123-456" But please be very careful as the VAL() command is not able to recognise "-" signs or for that matter "+" signs. What it can do, and indeed does do, is to ignore SPACES. Unless the "+" or "-" is the first character of a STRING. So how do we convert the STRINGS back to NUMBERS? We use the command:- Number = VAL(a$) or Number2 = VAL(b$) or in our case VAL(x$) Now the VAL() command will convert any STRING containing digits into a number. But, and I emphasize this, as soon as it meets a character which is not a digit it STOPS. The only character it will step over is a SPACE. There is one exception, if the FIRST character is a "+" or a "-" sign, VAL() will recognise it. STRING RESULT " 123 456" 123456 " 123-456" 123 "-123-456" -123 " 123abcd" 123 " 456 ab3" 456 "-abc2345" 0 "-0345678" -345 "-034.567" -34.567 Zero " 0" is always regarded as a positive number unless it is followed by another number. The "." decimal point is regarded as a digit by VAL(). RANDOM NUMBER GENERATORS As a final point on the generation of RANDOM NUMBERS I have included here 2 RANDOM NUMBER GENERATORS. They will both generate RANDOM NUMBERS between 1 and any number you select. You can cut them out with a text editor or type them in. They will both display the highest number generated. If you have a couple of days to spare you can make the loop = to 5000000 but I don't recommend it. It wont make any difference.  ********************************************************************* Example 2 RANDOMIZE TIMER x=0 FOR num =1 TO 10000 number = RND PRINT number;" ",number*19;" ",number*19+.4,(number*19+.4)+1,INT(number*19+.4)+1 IF number>x THEN x = number NEXT PRINT "The highest number generated was ";x;"whose integar = ";INT(x*19+.4)+1 *************************************************************************** INPUT "type in High number (say 60 or 40) ";y INPUT "Type in Number of times to Run (say 100 or 1000) ";z RANDOMIZE TIMER x=0 FOR num =1 TO z number = RND PRINT number;" ",number*y;" ",INT(number*y)+1 IF number>x THEN x = number NEXT PRINT "The highest number generated was ";x;"whose integar result = ";INT(x*y)+1  ***************************************************************************  For all those people out there who like to play with random numbers The pseudo random numbers generated by a computer can range from .0000001 to .9999999 in THEORY. Bigger computers than the Amiga can generate numbers with more DECIMAL places but it doesn't matter one iota. The number never reaches 0 and never reaches 1 so as long as your life doesn't depend on it go for broke. Now that our Maths are finished I thought you might like to learn about another kind of RANDOM. In the next chapters we are going to deal with SEQUENTIAL and RANDOM files. How they work and how to use them. We will design a DATABASE that will allow you to create a FILE. Enter DATA into it, SEARCH it for an entry, ALTER an entry and Print out your DATA. After that we will design a PAINT PROGRAM, it won't be quite as good as DPAINT but you will be surprised at what it can do. and if you just want to design CHRISTMAS CARDS in 32 colours this is the one for you. As the next issue of Megadisc will be out before Xmas I have decided to give you a Christmas Present. The Next Chapter is a full fledged DataBase and is complete in every detail. It has the following functions:- 1. It will create and maintain any number of files to use within the DataBase. 2. Each File can have up to 10 fields in it. 3. As is, each file can hold 100 records with up to 10 Fields, but this can easily be changed to 1000 or 10000, it all depends on how much disk space you wish to allocate to each file. If you only have 25 Names and addresses in your christmas card list then it is pointless using a database with 10000 entries. But if you have a stamp collection with say 4000 stamps and want them all in one file then a file with only 100 records would be useless. 4. It has a Search facility on any field with in the record. 5. It can print out the entire contents of your file either in list form or as Stored. 6. It can print out a single entry in either List form or as stored. Each of these methods will print a header showing the file name and the date you printed it. 7. There is also a facility to print an Envelope with your return address in the Top Left Hand corner of the Envelope and the name and address of the addressee in the correct place. This facility is only useful for addressbook type files but it's there if you want it. 8. It comes with a full tutorial as Chapter 6 of the Basic Tutorial: How it is constucted. How and why each command is used. And to help you get started there are two demo files to show you how it can be tailored to suit your needs. 9. It uses all the commands and subroutines we have learnt so far plus a few more, and of course there is the usual Tutorial. Till next time HAPPY COMPUTING. Frank Wilkinson 16 Melbourne Place Alberton SA 5014. Phone 08 47 1871  ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ##