GFA SIG ~~~~~~~ Good news for all you GFA programmers. Hutch has bought himself another ST and continues to program in GFA basic. As I have yet not received the next part of Hutch's excellent tutorial series, I have parts 1 & 2 of a 10 part GFA tutorial by Tom Hayslett. The full series of Tom's tutorial is available from Cathy Tuck (ACE PD Officer). Ed ] GFA BASIC Class 1 BASIC What is it? BASIC stands for Beginners All purpose Symbolic Instruction Code. BASIC consists of commands (specific words) that when entered through a BASIC interpreter form a 'Statement'. A statement is simply an instruction to the computer to perform a specific task. All computers understand only 1 language directly, called machine language. Machine language consists of series of numbers (1's and 0's). Assembly language is sometimes referred to as machine language. Any programming language other than Assembly or machine language is known as an interpreted language, in other words, the computer translates the commands you enter into machine language which it can understand. This translation is what sometimes causes other languages to be slower than Assembly language. Why learn another language then? Why not learn Assembly and save the time of this translation? Simple, Assembly or machine language is cryptic! The commands are not easily understood and every different computer chip uses a different set of instructions. Assembly is by far the fastest and most efficient language, also the hardest to learn (in my opinion anyway). BASIC on any computer uses an interpreter for you to enter your code. The interpreter then translates those commands into machine language. The relative ease of learning and using BASIC is why we're here. Lets proceed... Through the course of this instruction the only thing that's required is GFA Basic itself. After you learn to write your own programs you may want to consider purchasing the GFA Compiler. It allows you to convert your basic programs into regular .PRG files that you can run from the desktop without having to load GFA Basic first. Also, the GFA Companion is helpful in constructing GEM menus, alert boxes, text boxes and 'pop-up' menus. The companion also includes a very good disk tutorial for GFA beginners. Items I strongly urge you to get immediately are: DIOX95.ARC - A shareware program that constructs dialog boxes for you and actually writes the GFA source code to merge into your own programs. GFATIPS.ARC - A series of GFA tutorials with example programs included written by John Holder (author of GFA Companion) Misc GFA programs - you can print them out and use them to study other programmers techniques. The 3 items mentioned above are available on the STarBase BBS (904- 581-2866, 1200/2400 baud, 24 hours daily) if you have a modem or from the STar library if you contact Hobart Neighbors. All the files include documentation which I suggest you print out as much as possible as it makes a good source for future reference. Several of the ST magazines have ongoing articles pertaining to GFA Basic, some very good. Other suggested reading sources are: GFA Basic Training - Reboot Camp - An excellent tutorial written specifically for the GFA novice. This will teach you a lot of the basics if you're a true beginning programmer. GFA Basic Book - Written by the author of GFA Basic himself. This book is for intermediate to advanced level programmers. A lot of good info here although it gets quite complex at times. Save this for when you've truly mastered the main commands and functions of the language. GFA Basic Quick Reference Guide by Abacus - Another reference type of book that includes additional examples of some of the commands. Very similar to the GFA book that comes with the language. On to the good stuff... (***NOTE*** the familiarization with the editor and keyboard commands referenced here were covered in our User Group lessons, if you're reading this from the tutorial, please study your GFA Owners Manual and learn to use the editor, it works!) Initially we'll cover familiarization with the editor in GFA. You'll learn how to use both the mouse and the keyboard to perform all the needed functions. We'll then progress to some of the most basic commands (and the most frequently used commands) to thoroughly familiarize you with them. Three different filename extensions will be used, .BAS (or .GFA),.BAK and .LST. The .BAS extender indicates that the program is a basic program that requires the GFA Basic interpreter (or the Run Only version) to run the program. Almost all the Basic interpreters use the .BAS extension so to identify the GFA only programs, some programmers began using .GFA as an extension to differentiate between the different interpreters. You CAN modify your GFA Basic program to list and load programs with a .GFA extension instead of the .BAS extension however, this should be left to those with some experience modifying programs in this fashion. The second time you save a file, GFA automatically renames any previous version of your program to .BAK to provide you with a backup. The .LST extension indicates a GFA program saved in ASCII format. To load these type of programs, you need to use the Merge feature within the interpreter. The advantage to saving your files in ASCII format is that they can be viewed/printed from the desktop. The disadvantage is that they take longer to save/load. Subroutines that you intend to use in different programs should be saved in this format as they can then be merged into any of your programs. One thing to remember when using GFA is that you may only enter 1 command per line. This tends to make your listings (programs) a little longer but this is also where GFA gains a lot of its speed. The interpreter will check each line you enter after you press . If there's an error, it will tell you. You may enter your commands in either upper or lowercase letters and GFA will automatically correct them for you. For ease of illustration, I'll show all GFA commands in uppercase letters. Some of the commands we'll learn to use during this first lesson will include: PRINT (or ?) - simply tells the computer to print a statement on the screen. EXAMPLE: PRINT "Hello there!" - notice the text we want printed is enclosed in quotation marks. The computer will print anything between the quotation marks up to a maximum of 255 characters. Instead of typing PRINT you may use the question mark and GFA will change it for you to PRINT. This is simply a shortcut. EXAMPLE: ? "Hello there!" - notice you still need the quote marks. PRINT AT(X,Y) - tells GFA to print a statement at a specific location with 'X' being the column and 'Y' being the row. EXAMPLE: PRINT AT(10,10);"Hello there!" - the quotes are still there and the only difference is this statement will print Hello there! beginning at the 10th column and the 10th row. REM (or ' or !) - tells GFA to ignore the statement that follows as it is only a comment for your use. Uses would be to insert comments in your program such as REM This changes colors. If you ever need to find where you change the colors in your program, it's easier to look for the comment you left yourself explaining what you did. REM or ' may be used at the beginning of a line only and indicates that GFA should ignore the entire line. The ' symbol is a shortcut. When you want to enter a comment on the same line as your statement, use the ! character. The ! tells GFA that the text following it is simply a comment for your use. EXAMPLE (EX1): REM Program to print Hello there! (GFA ignores this) ' By Tom Hayslett (GFA ignores this line also) PRINT "Hello there!" !Actual program PAUSE X - Tells GFA to wait a specified period of time before continuing. 'X' can be any whole number. 50=1 second so if your command is PAUSE 100, you're telling GFA to wait 2 seconds and then continue. EXAMPLE (EX2): REM Program to pause PAUSE 50 !Waits 1 second PAUSE 300 !Waits 6 seconds CLS - Tells GFA to clear the screen. This will help keep your program looking more professional and it's very fast. It doesn't need anything else used with it to work. EXAMPLE: CLS KEY=INP(2) - This statement tells GFA to wait for the user to press any key. This can be very useful when you want to display information to whomever is using your program and you want your program to continue after they've read the information and pressed any key. EXAMPLE: KEY=INP(2) The 2 in parentheses indicates we're waiting for any input from the keyboard. GFA uses this command to wait for input from other sources also but we'll save them for another lesson. EDIT - Tells GFA to return you to the editor. Again, no additional commands are needed. Now, lets use what we've learned to write a short and simple program. See if you can follow exactly what we're doing in the following program (EX3). REM Our first program in GFA PRINT AT(35,11);"Hello there!" !Prints at column 35, row 11 PRINT AT(30,22);"Press any key to continue..." KEY=INP(2) !Wait for a key to be pressed CLS !Clears the screen PRINT AT(35,11);"Thank you!" PAUSE 250 !Pauses for 5 seconds CLS PRINT "Returning to the editor..." PAUSE 150 EDIT !Returns us to the GFA editor That's it for the introduction and some of the simpler yet most commonly used commands. Hopefully I'll be able to keep writing these files to help you beginners progress. Don't forget though that after you do become somewhat proficient at the language yourself, I expect to learn some tricks from you! Tom Hayslett STar Users Group GFA Basic Class 2 I'm not going to review what we learned last time since you have the documentation from class 1. We do however, need to go into a little explanation here first... When working in the editor, the 'NEW' command will erase the current program you have in memory and allow you to start again with a fresh editor. Care should be exercised when entering the NEW command, be sure you've saved any program you want to keep BEFORE entering NEW. Variables are used extensively in any BASIC language we use. A variable is simply a symbol (usually a letter or word or number) that we use in a program when that variable needs to change to many different values (or characters). For example, I know you've all seen or heard statements like this one pertaining to programming 'LET X=5' (for some reason, 'X' seems to be popular). All this means is that we've assigned the value of 5 to a variable called 'X'. If we had a short program like this (EX21): LET X=5 PRINT X Guess what would be printed? Right, the number 5. Okay, there are several different types of variables we may use to help BASIC understand what type of information we want it to save. All variables MUST start with a letter and be ONLY 1 word long. You should NOT use a BASIC command as a variable (even though it IS possible). Another example (EX22): LET Our_number=5 PRINT Our_number This statement is functionally identical to the first example. Notice that when I used the variable 'Our_number', there's an underscore connecting the 2 words. This is to keep our variable 1 'word' long (remember the rules?). Now for some of the different TYPES of variables we may use. A letter or word alone indicates 'normal' or 'real' variables. They can be numbers only and are accurate to 11 digits. EXAMPLE: LET X=234.15723 assigns that numeric value to the variable 'X'. A variable with a suffix of '%' indicates an 'integer' variable, in other words, whole numbers between 2, 147,483,647 and -2,147,483,648. The advantage of 'integer' variables is that they are much faster as BASIC doesn't have to compute the numbers out to 11 places (including decimal places). EXAMPLE: LET X%=5 assigns the number 5 to our 'integer' variable 'X%'. Another type of variable is the 'string' variable. These are indicated by the suffix '$' and can contain ANY characters less than 255 in length. The value assigned to string variables must be enclosed in quotation marks. EXAMPLE: LET X$="Hello there!". This assigns the 'value' 'Hello there!' to the string variable 'X$'. You may change the value of a variable within a program by assigning it a new value and the use of 'LET' is optional. EXAMPLE (EX23): REM Short variable routine LET X=1 !I could have left 'LET' out Y=2 !I DID leave 'LET' out Total%=3 !Notice the '%'? 'Total%' is only another variable NAM$="Tom Hayslett" !You may have spaces or ANYTHING in there LONG_NAM$="Thomas W. Hayslett, W?" !Any characters (up to 255) ' !A 'REM' line to separate parts of our program PRINT X PRINT Y PRINT Total% PRINT NAM$ ? LONG_NAM$ Our screen would look like this: 1 2 3 Tom Hayslett (Notice it does NOT print the quotes!) Thomas W. Hayslett, W? Simple huh? Now lets see how we can use variables for a good purpose. You may perform mathematical functions on numeric variables just as if they were numbers. Math functions in BASIC include '+' or 'ADD' for addition, '-' or 'SUB' for subtraction, '/' or 'DIV' for division and '*' or 'MUL' for multiplication. There are also '=' for equal to, '<>' for not equal to, '<' for less than, '>' for greater than, '<= or =<' for equal to or less than and '>= or =>' for equal to or greater than. EXAMPLE (EX24): X%=10 Y%=5 Total%=X%+Y% ? Total% This would print the integer variable 'Total%' on our screen which has the value of 'X%' and 'Y%' added together. It would print 15. You may also use some of the mathematical symbols to combine string variables. EXAMPLE (EX25): NAM$="Tom" Last_nam$="Hayslett" ? NAM$+Last_nam$ You must enclose your string variables in quotes and guess what our example would print? It would print TomHayslett. Oops! To correct this, we would have to change our string variable 'NAM$' to "Tom ", the space would also print making our screen show Tom Hayslett. Now, to make what we've learned useful, our numbers and strings must come from somewhere so where do we get them? One common source is from the person using our program. How? We use the 'INPUT' command. It's use is simple, EXAMPLE: INPUT X%. This pauses and waits for the user to enter a number. If they enter a character (A,b,q,etc.), a bell will sound and they can try again. The 'INPUT' command will also print a question mark for you if you use a semicolon with it like this: INPUT;X% You may also include instructions with the input command if you use it like this: INPUT "Instructions";X% 'Instructions' may be any string of text you want to precede the question mark. You may also request input of a character string by simply requesting a string variable. EXAMPLE: INPUT "Please enter your name";NAM$ Our screen would look like this: Please enter your name? _ (The underscore indicates where our flashing cursor would be) Okay, now on to another example (EX26). REM Example program using input, variables and math CLS !Clear the screen ? !Print a blank line INPUT "Please enter your name and press RETURN ",NAM$ !See below ' This asks the user to enter their name ? Print another blank line INPUT "Now, please enter a number ",X% ? INPUT "A second number also please ",Y% ? ? "Thank you "NAM$", please touch any key and I'll print the" ? "total of the 2 numbers you entered..." Key=INP(2) !Wait for any key to be pressed Total%=X%+Y% PRINT AT(39,12);Total% !Prints total of the 2 numbers PAUSE 150 !Pause for 3 seconds CLS PRINT AT(36,12);"Goodbye "NAM$", returning to the editor" PAUSE 200 EDIT Whew! If you follow the program line by line it's easy to see exactly what it's doing in each step. You may also combine your INPUT statements and ask for more than one item at a time as follows, INPUT "Name and age: ",NAM$,AGE%. Notice we used a comma instead of a semi colon (same as in the first few lines of the example program above)? The comma along with the semicolon allows us to define how we want the screen to look when we request information. We could also use it like this: INPUT "Name and age? ",NAM$,AGE%. We may include the ? if we desire. We need to learn a few other commands now to make what we've already learned more meaningful. We're going to learn about several types of loops. A loop is simply a routine in a program that continues whatever process it's doing until a pre determined condition is met. Lets start with the simplest form first, the IF THEN type of loop. An 'IF...THEN' really isn't a true loop per say but it's often used as one. IF sets up a condition that if true will process a certain statement. Every IF must be ended with an 'ENDIF'. A simple form is as follows (EX27): IF NAM$="Tom" THEN PRINT "Hello Tom" ENDIF If the condition is true (string variable 'NAM$' = Tom), the following command is executed. If our string variable 'NAM$' does NOT = Tom, the complete loop would be bypassed. What if we wanted it to do something else if that condition wasn't met? Here's where the 'ELSE' command comes into play. It means if the preceding statement wasn't true then do this. For example (EX28): IF NAM$="Tom" THEN PRINT "Hello Tom" ELSE PRINT "Hello somebody else" ENDIF In this case, if our string variable = Tom it would print 'Hello Tom', if it didn't equal 'Tom' it would print 'Hello somebody else" then it would end the 'if' (leave the loop). These 'IF...THEN...ELSE...ENDIF' commands can be nested, that is you may have them inside another 'IF...ENDIF' loop. The editor in GFA will help you keep track of how many loops you are deep as it controls the indenting of the commands. Each 'IF' must be closed with an 'ENDIF'. Another example (EX29): IF NAM$="Tom" THEN PRINT "Hello Tom" ELSE IF NAM$="Bob" THEN PRINT "Hello Bob" ENDIF ENDIF Notice we have 2 'ENDIF' commands because we have 2 'IF's. A numeric example would be as follows (EX210): INPUT "Please enter a number between 1 and 10",NUM% IF NUM%< 5 THEN PRINT "Your number is less than 5" ELSE IF NUM%=>5 AND NUM%<=10 THEN PRINT "Your number is larger than 4 but less than 11" ELSE PRINT "You cheated! Your number was too big!" ENDIF ENDIF This asks for a number to be entered that's between 1 and 10 (NUM%). If the number is less than 5 it prints 'Your number was less than 5'. If the number is equal to or greater than 5 AND equal to or less than 10 it prints 'Your number is larger than 4 but less than 11'. If the user enters a number larger than 10, it goes to the 'ELSE' which prints 'You cheated! Your number was too big!'. Then we have our 2 'ENDIF' commands to end the 'IF's. Another type of a loop is a 'DO...LOOP'. You tell it to do something continuously until a pre determined condition is met. You exit a 'DO...LOOP' with the 'EXIT...IF' command. Sometimes when writing a program, you may accidentally write loops without an exit. To stop the program and return to the editor, press the CONTROL, ALTERNATE and SHIFT keys simultaneously. This is called a 'BREAK'. lets look at an example of a 'DO...LOOP' (EX211). DO !Starts our loop CLS !Clear the screen INPUT "Please enter a number between 1 and 10 ",NUM% !Gets NUM% IF NUM%=3 THEN !If the number is 3 PRINT "You guessed my lucky number!" !Then print this PAUSE 300 !Pause for 6 seconds ELSE !if it isn't 3, then do this PRINT "Wrong. Please try again..." PRINT "Press any key to continue..." KEY=INP(2) !Wait for any key to be pressed ENDIF IF NUM%=>11 THEN !If you enter a number we don't want then PRINT "You didn't follow instructions." PAUSE 300 ENDIF EXIT IF NUM%=>11 !This exits our 'DO...LOOP' when NUM%=>11 LOOP Another type of looping can be done with the 'FOR...NEXT' command. It usually starts a counter and processes the commands between the FOR and the NEXT as long as the counter is still valid. For example: FOR NUM%= 1 to 10 states that our integer variable (NUM%) will equal 1, then 2, then 3, etc. up to 10. When the program encounters the NEXT command it returns to the FOR to process the commands between FOR and NEXT again. For example (EX212): FOR NUM%=1 to 10 PRINT PRINT NUM% !This will print our variable NEXT NUM% !This sends our program back to the first line Each time through, NUM% will be printed so your screen will look like this 1 2 3 ETC. Another type of loop is the 'REPEAT...UNTIL' loop. REPEAT starts the loop and it continues until the condition set in the UNTIL command is true. For example (EX213): REPEAT !Starts the loop CLS INPUT "Please enter a number between 1 and 10 ",NUM% !Gets NUM% IF NUM%=5 THEN !If the number input=5 then PRINT "Good guess!" PAUSE 300 ELSE PRINT "Wrong! Press any key to try again..." KEY=INP(2) ENDIF UNTIL NUM%=5 !This exits if the number was 5 Another command essential to programming is the 'GOSUB' command. This directs your program to a particular routine that you may want to perform any number of times. The 'GOSUB' command can be abbreviated '@' and the location you want your program to go to must begin with 'PROCEDURE'. To return from the 'PROCEDURE' to your program, simply end the PROCEDURE with 'RETURN'. That sends you back to the line after the GOSUB command. This is best demonstrated with an example (EX214): DO CLS PRINT AT(10,10); !Print a blank line to move 10,10 INPUT "Please enter a number (1 - 25) ",NUM% IF NUM%=>1 AND NUM%=<25 THEN GOSUB Check_number !Execute PROCEDURE named Check_number ENDIF EXIT IF NUM%="0" !Exits loop if we enter 0 LOOP EDIT !Return to the editor after exiting the loop ' PROCEDURE Check_number CLS PRINT AT(30,12); PRINT "You entered "NUM%" ..." PAUSE 200 RETURN !Go back to our loop to get another number One more type of loop to learn and then we're finished with this lesson (Whew!). This is the 'WHILE...WEND' type of loop. WHILE sets up a condition and if it's true it processes all the commands between WHILE and WEND (WEND stands for WHILE END) until the WHILE condition is met. The major difference between the 'WHILE...WEND' loop and the other types is that it's possible for the 'WHILE...WEND' to never be processed if the WHILE condition isn't true. With the others, they always process their loops at least once and look for an EXIT. An example follows (EX215): FOR NUM%=1 to 15 !Set up our counter CLS PRINT AT(25,12);"Our number is now "NUM%"..." PAUSE 100 WHILE NUM%=10 !Do this ONLY while NUM%=10 PRINT AT(25,14);"Now in the 'WHILE...WEND' loop..." PAUSE 300 EXIT IF NUM%=10 WEND !End the while loop NEXT NUM% !Return to our counter Notice that the ONLY time our WHILE...WEND loop is executed is when our variable is equal to 10. The other times, it never executes the loop. Enter all these commands and play with them. You'll learn a lot just by experimenting with them. Also, read about these commands in your GFA Basic owners manual, their examples should help serve to clarify what you've learned here. Tom Hayslett STar Users Group