SECTION 55. Some BASIC Commands and Keyboard Operations Unique to C1285.1 INTRODUCTIONThis section introduces you to some powerful BASIC commands and statements that you probably have not seen before, even if you are an experienced BASIC programmer. If you are familiar with programming in BASIC, you have probably encountered many situations in which you could have used these commands and statements. This section explains the concepts behind each command and gives examples of how to use each command in a program. (A complete list and an explanation of these commands and statements may be found in Chapter V, BASIC 7.0 Encyclopaedia.) This section also describes how to use the special keys that are available to you in C128 mode. 5.2 ADVANCED LOOPING5.2.1 The DO/LOOP StatementThe DO/LOOP statement provides more sophisticated ways to create a loop than do the GOTO, GOSUB or FOR/NEXT statements. The DO/LOOP statement combination brings to the BASIC language a very powerful and versatile technique normally only available in structured programming languages. We discuss just a few possible uses of DO/LOOP in this explanation. If you want to create an infinite loop, you start with a DO statement, then enter the line or lines that specify the action you want the computer to perform. Then end with a LOOP statement like this: 100 DO 110 PRINT "REPETITION" 120 LOOP Press the {run/stop} key to stop the program. The directions following the DO statement are carried out until the program reaches the LOOP statement (line 120); control is then transferred back to the DO statement (line 100). Thus any statements in between DO and LOOP are performed indefinitely. 5.2.1.1 UNTILAnother useful technique is to combine the DO/LOOP with the UNTIL statement. The UNTIL statement sets up a condition that directs the loop. The loop will run continually unless the condition for UNTIL happens. 100 DO:INPUT "DO YOU LIKE YOUR COMPUTER";A$ 110 LOOP UNTIL A$="YES" 120 PRINT "THANK YOU" The DO/LOOP statement is often used to repeat an entire routine indefinitely in the body of a program, as in the following: 10 PRINT "PROGRAM CONTINUES UNTIL YOU TYPE 'QUIT'" 20 DO UNTIL A$="QUIT" 30 INPUT "DEGREES FAHRENHEIT";F 40 C=(5/9)*(F-32) 50 PRINT F;"DEGREES FAHRENHEIT EQUALS ";C;"DEGREES CELCIUS" 60 INPUT "AGAIN OR QUIT";A$ 70 LOOP 80 END Another use of DO/LOOP is as a counter, where the UNTIL statement is used to specify a certain number of repetitions. 10 N=2*2 20 PRINT"TWO DOUBLED EQUALS";N 30 DO UNTIL X=25 40 X=X+1 50 N=N*2 60 PRINT"DOUBLED";X+1;"TIMES...";N 70 LOOP 80 END Notice that if you leave the counter statement out (the UNTIL X=25 part in line 30), the number is doubled indefinitely until an OVERFLOW error occurs. 5.2.1.2 WHILEThe WHILE statement works in a similar way to UNTIL, but the loop is repeated only while the condition is in effect, such as in the reworking of the last brief program: 100 DO:INPUT "DO YOU LIKE YOUR COMPUTER";A$ 110 LOOP WHILE A<>"YES" 120 PRINT "THANK YOU" 5.2.1.3 EXITAn EXIT statement can be placed within the body of a DO/LOOP. When the EXIT statement is encountered, the program jumps to the next statement following the LOOP statement. 5.2.2 The ELSE Clause with IF-THENThe ELSE clause provides a way to tell the computer how to respond if the condition of the IF-THEN statement is false. Rather than continuing to the next program line, the computer will execute the command or branch to the program line mentioned in the ELSE clause. For example, if you wanted the computer to print the square of a number, you could use the ELSE clause like this: 10 INPUT "TYPE A NUMBER TO BE SQUARED";N 20 IF N<100 THEN PRINT N*N:ELSE 40 30 END 40 ?"NUMBER MUST BE < 100":GOTO 10 Notice that you must use a colon (:) between the IF-THEN statement and the ELSE clause. 5.2.3 The BEGIN/BEND Sequence with IF-THENBASIC 7.0 allows you to take the IF-THEN condition one step further. The BEGIN/BEND sequence permits you to include a number of program lines to be executed if the IF condition is true, rather than one simple action or GOTO. The command is constructed like this: IF condition THEN BEGIN: (program lines): BEND:ELSE Be sure to place a colon (:) between BEGIN and any instruction to the computer, and again between the last command in the sequence and the word BEND. BEGIN/BEND can be used without an ELSE clause, or can be used following the ELSE clause when only a single command follows THEN and multiple commands follow the ELSE clause (of course BEGIN/BEND can also be used both after THEN and ELSE). Try this program: 10 INPUT A 20 IF A<100 THEN BEGIN: ?"YOUR NUMBER WAS"A 30 SLEEP 2:REM DELAY 40 FOR X=1 TO A 50 ?"THIS IS AN EXAMPLE OF BEGIN/BEND" 60 NEXT X 70 ?"THAT'S ENOUGH":BEND:ELSE ?"TOO MANY" 80 END This program asks for a number from the user. IF the number is less than 100, the statement between the keywords BEGIN and BEND are performed, along with any statements on the same line as BEND (except for ELSE). The message "YOUR NUMBER WAS" n appears on the screen. Line 30 is a delay used to keep the message on the screen long enough so it can be read easily. Then a FOR/NEXT loop is used to display a message the number of times specified by the user. If the number is greater than or equal to 100, the part after the THEN condition is skipped, and part after the ELSE condition (printing "TOO MANY") is carried out. The ELSE keyword must be on the same line as BEND. 5.2.4 The SLEEP CommandNote the use of the SLEEP command in line 30 of the program just discussed. SLEEP provides an easier, more accurate way of inserting and timing a delay in program operation. The format of the SLEEP command is: SLEEP nwhere n indicates the number of seconds (rounded down to a whole number of seconds), in the range of 0 to 65535, that you want the program to delay. In the command shown in line 30, the 2 specifies a delay of two seconds. 5.3 FORMATTING OUTPUT5.3.1 The PRINT USING CommandSuppose you were writing a sales program that calculated a dollar amount. Total sales divided by number of salespeople equals average sales. But performing this calculation might result in dollar amounts with four or five decimal places! You can format the result the computer prints so that only two decimal places are displayed. The command which performs this function is PRINT USING. PRINT USING lets you create a format for your output, using spaces, commas, decimal points and dollar signs. Hash marks (the {#} sign) are used to represent spaces or characters in the displayed result. For example: PRINT USING "#$######.##";Atells the computer that when A is printed, it should be in the form given, with up to six places to the left of the decimal point, and two places to the right. The hash mark in front of the dollar sign indicates that the {$} should float, that is, it should always be placed next to the left-most number in the format. If you want a comma to appear before the last three dollar places (as in $1,000.00), include the comma in the PRINT USING statement. Remember you can format output with spaces, commas, decimal points, and dollar signs. There are several other special characters for PRINT USING, see paragraph 17.68 of the BASIC 7.0 Encyclopaedia for more information. 5.3.2 The PUDEF CommandIf you want formatted output representing something other than dollars and cents, use the PUDEF (Print Using DEFine) command. You can replace any of four format characters with any character on the keyboard. The PUDEF command has four positions, but you do not have to redefine all four. The command looks like this: PUDEF " ,.$" 1234 Here:
If you wrote a program that converted a dollar amount to English pounds, you could format the output with these commands: 10 PUDEF " ,.{pound}" 20 PRINT USING "#$####.##";X 5.4 SAMPLE PROGRAMThis program calculates interest and loan payments, using some of the commands and statements you just learned. It sets a minimum value for the loan using the ELSE clause with an IF-THEN statement, and sets up a dollar and cents format with PRINT USING. 10 INPUT "LOAN AMOUNT IN DOLLARS";A 20 IF A<100 THEN 70:ELSE P=.15 30 I=A*P 40 ?"TOTAL PAYMENT EQUALS"; 50 PRINT USING "#$#####.##";A+1 60 GOTO 80 70 ?"LOANS UNDER $100 ARE NOT AVAILABLE" 80 END 5.5 INPUTTING DATA WITH THE GETKEY COMMANDYou have learned to use the INPUT and GET commands to enter DATA during a program. Another way for you to enter data while a program is being RUN is with the GETKEY statement. The GETKEY statement accepts only one key at a time. GETKEY is usually followed by a string variable (A$ for example). Any key that is pressed is assigned to that string variable. GETKEY is useful because it allows you to enter data one character at a time without having to press the RETURN key after each character. The GETKEY statement may only be used in a program. Here is an example of using GETKEY in a program: 1000 PRINT "PLEASE CHOOSE A, B, C, D, E OR F" 1010 GETKEY K$ 1020 PRINT A$;" WAS THE KEY YOU PRESSED." The computer waits until a single key is pressed; when the key is pressed, the character is assigned to variable A$, and printed out in line 1020. The following program features the GETKEY in more complex and useful fashions: for answering multiple-choice question and also asking if the question should be repeated. If the answer given is incorrect, the user has the option to try again by pressing the {y} key (line 80). The key pressed for the multiple choice answer is assigned to variable A$ while the "TRY AGAIN" answer is assigned to B$, through the GETKEY statements in line 60 and 90. IF/THEN statements are used for loops in the program to get the proper computer reaction to the different keyboard inputs. 10 PRINT "WHO WROTE 'THE RAVEN'?" 20 PRINT "A. EDGAR ELLEN POE" 30 PRINT "B. EDGAR ALLEN POE" 40 PRINT "C. IGOR ALLEN POE" 50 PRINT "D. ROB RAVEN" 60 GETKEY A$ 70 IF A$="B" THEN 150 80 PRINT "WRONG. TRY AGAIN? (Y OR N)" 90 GETKEY B$ 100 IF B$="Y" THEN PRINT "A,B,C OR D": GOTO 60 110 IF B$="N" THEN 140 120 PRINT "TYPE EITHER Y OR N - TRY AGAIN" 130 GOTO 90 140 PRINT "THE CORRECT ANSWER IS B." 145 GOTO 160 150 PRINT "CORRECT!" 160 END GETKEY is very similar to GET, except GETKEY will wait for a key to be pressed. 5.6 PROGRAMMING AIDSIn earlier sections you learned how to make changes in your programs, and correct typing mistakes with {inst/del}. BASIC also provides other commands and functions which help you locate actual programming errors, and commands which you can use to make programming sessions flow more smoothly. 5.6.1 Entering Programs5.6.1.1 AUTOC128 BASIC provides an auto-numbering process. You determine the increment for the line numbers. Say you want to number your program in the usual manner, by tens. Before you begin to program, while in DIRECT mode, type: AUTO 10 {return} The computer automatically numbers your programs by tens. After you enter a line and press the {return} key, the next line number appears, and the cursor is in the correct place for you to type the next statement. You can choose to have the computer number the commands with any increment; you might choose 5 or even 50. Just place the number after the word AUTO and press {return}. To turn off the auto-numbering feature, type AUTO with no increment, and press {return}. 5.6.1.2 RENUMBERIf you write a program and later add statements to it, sometimes the line numbering can be awkward. Using the RENUMBER command you can change the line numbers to an even increment for part or all of your program. The RENUMBER command has several optional parameters, as listed below in brackets: RENUMBER [new starting line] [,[increment] [,old starting line]] The new starting line is what the first program line wil be numbered after the RENUMBER command is used. If you do not specify, the default is 10. The increment is the spacing between line numbers, and it also defaults to 10. The old starting line number is the line number where the renumbering is to begin. This feature allows you to renumber a portion of your program, rather than all of it. It defaults to the first line of the program. For example: RENUMBER 40,,80tells the computer to renumber the program starting at line 80, in increments of 10. Line 80 becomes line 40. Notice that this command, like AUTO, can only be executed in DIRECT mode. 5.6.1.3 DELETEYou know how to delete program lines by typing the line number and pressing the {return} key. This can be tedious if you want to erase an entire portion of your program. The DELETE command can save you time because you can specify a range of program lines to erase all at once. For example: DELETE 10-50will erase line 10, 50, and any in between. The use of DELETE is similar to that of LIST, in that you can specify a range of lines up to a given line, or following it, or a single line only, as in these examples: DELETE -120erases all lines up to and including 120 DELETE 120-erases line 120 and any line after it DELETE 120erases line 120 only 5.6.2 Identifying Problems in Your ProgramsWhen a program does not work the way you expected, an error message usually occurs. Sometimes the messages are vague, however, and you still do not understand the problem. The Commodore 128 computer has several ways of helping you locate the problem. 5.6.2.1 HELPThe Commodore 128 provides a HELP command that specifies the line in which a problem has occurred. To actuate the HELP command, just press the special {help} key on the row of keys located above the main keyboard. Type the following statement. It contains an intentional error, so type it just as is: 10?3;4:5;6 When you RUN this one-line program, the computer prints 3 and 4 as expected, but then responds ?SYNTAX ERROR IN 10. Suppose you cannot see the error (a colon instead of a semicolon between 4 and 5). You press the {help} key. (You can also type HELP and press {return}.) The computer displays the line again, but the 5;6 is highlighted to show the error is in that part of the line: 10?3;4:5;6 5.6.2.2 Error Trapping - The TRAP CommandUsually, if an error occurs in a program, the program "crashes" (stops running). At that point, you can press the {help} key to track down the error. However, you can use the BASIC 7.0 TRAP command to include an error-trapping capability within your program. The TRAP command advises you to locate and correct an error, then resume program operation. Usually, the error trapping function is set in the first line of a program: 5 TRAP 100tells the computer that if an error occurs to go to a certain line (in this case, line 100). Line 100 appears at the end of the program, and sets up a contingency. Neither line is executed UNLESS there is an error. When an error occurs, the line with the TRAP statement is enacted, and control is directed to another part of the program. You can uses these statements to catch anticipated errors in entering data, resume execution, or return to text mode from graphics mode, to name just a few options. If you run the last DO/LOOP example (with doubled numbers) without an UNTIL statement, you can get an OVERFLOW error and the program crashes. You can prevent that from happening by adding two lines, one at the beginning and one at the end. For example, you might add these two lines: 5 TRAP 100 100 IF N<1 THEN END Even though N has been much greater than one for the entire program, the statement is not considered until there is an error. When the number "overflows" (is greater than the computer can accept), the TRAP statement goes into effect. Since N is greater than one, the program is directed to END (rather than crashing). Here is an example in which trapping is used to prevent a zero from being input for division. 10 TRAP 1000 100 INPUT "I CAN DIVIDE BY ANY NUMBER, GIVE ME A NUMBER TO DIVIDE";D 110 INPUT "WHAT SHOULD I DIVIDE IT BY";B 120 A=D/B 130 PRINT D;"DIVIDED BY ";B;"EQUALS ";A 140 END 1000 IF B=0 THEN PRINT"EVEN I CAN'T DO THAT" 1100 INPUT "PICK A DIFFERENT NUMBER";B:RESUME 120 Notice the RESUME in line 1100. This tells the computer to return to the line mentioned (in this case, 120) and continue. Depending on the error that was trapped, resuming executing may or may not be possible. For additional information on error trapping, see the error functions ERR$ (paragraph 18.8), EL and ER (paragraph 19.1), described in Chapter V, BASIC 7.0 Encyclopaedia. 5.6.2.3 Program Tracing - The TRON and TROFF CommandsWhen a problem in a program occurs, or you do not get the result you expect, it can be useful to methodically work through the program and do exactly what the computer would do. This process is called tracing. Draw variable boxes and update the values according to the program statements. Perform calculations and print results following each instruction. (All done by hand, using the program listing as a guideline.) This kind of tracing may show you, for example, that you have used a GOTO with an incorect line number, or calculated a result but never stored it in a variable. Many program errors can be located by pretending to be the computer, and following only one instruction at a time. Your C128 can perform a type of trace using the special commands TRON and TROFF (short for TRace ON and TRace OFF). When the program is run, with TRace ON, the computer prints the line numbers in the order they are executed. In this way, you may be able to see why your program is not giving the results you expected. Type any short program we have used so far, or use one of your own design. To activate trace mode, type TRON in DIRECT mode. When you run the program, notice how line numbers appear in brackets before any results are displayed. Try to follow the line numbers and see how many steps the computer needed to arrive at a certain point. TRON will be more interesting if you pick a program with many branches, such as GOTO, GOSUB and IF-THEN-line number. Type TROFF to turn trace mode off before continuing. You do not have to trace an entire program. You can place TRON within a program as a line prior to the program section causing problems. Put the word TROFF as a program line after the troublesome section. When you run the program, only the lines between TRON and TROFF will be bracketed in the results. 5.7 WINDOWINGWindows are a specific area on the screen that you define as your workspace. Everything you type (lines you type, listings of programs, etc.) after setting a window, appears within the window's bounderies, not affecting the screen outside the window area. The Commodore 128 provides two methods of creating windows: the WINDOW command and {esc} key functions. 5.7.1 Using the WINDOW Command to Create a WindowThe Commodore 128 BASIC 7.0 language features a command that allows you to create and manipulate windows: the WINDOW command. The command format is: WINDOW top-left column, top-left row, bottom-right column, bottom-right row [,clear option] The first two numbers after WINDOW specify the column and row number of where you want the top left corner of the window to be; the next two numbers are the coordinates for the bottom right corner. Remember that the screen format (40- or 80-columns) dictates the acceptable range of these coordinates. You can also include a clear option with this command. If you add 1 onto the end of the command, the window screen area is cleared, as in this example: WINDOW 10,10,20,20,1 Here's a sample program that creates four windows on the screen, in either 40- or 80-column format. 10 SCNCLR :REM CLEAR SCREEN 20 WINDOW 0,0,39,24 :REM SET WINDOW TO FULL SCREEN 30 COLOR 0,13:COLOR 4,13 :REM SET 40 SCREEN TO MED. GREY 40 A$="ABCDEFGHIJKLMNOPQRST" 50 COLOR 5,5 :REM SELECT PURPLE TEXT 60 FOR I=1 TO 25 :REM FILL SCREEN WITH CHARACTERS 70 PRINT A$;A$:NEXT I 80 WINDOW 1,1,7,20 :REM DEFINE WINDOW 1 90 COLOR 5,3 :REM SELECT RED TEXT 100 PRINT CHR$(18);A$; :REM PRINT A$ IN REVERSE RED TEXT 110 WINDOW 15,15,39,20,1 :REM DEFINE SECOND WINDOW 120 COLOR 5,7 :REM SELECT BLUE TEXT 130 FOR I=1 TO 6:PRINT A$;: NEXT :REM FILL WINDOW 2 WITH CHARACTERS 140 WINDOW 30,1,39,22,1 :REM DEFINE THIRD WINDOW 150 COLOR 5,8:LIST :REM LIST IN YELLOW TEXT 160 WINDOW 5,5,33,18,1 :REM DEFINE FOURTH WINDOW 170 COLOR 5,2 :REM SELECT WHITE TEXT 180 PRINT A$:LIST :REM PRINT A$ AND LIST IN WHITE TEXT 190 END 5.7.2 Using the ESC Key to Create a WindowTo set a window with the {esc} (Escape) key, follow these steps:
You can manipulate the window and the text inside using the {esc} key. Screen editing functions, such as inserting and deleting text, scrolling, and changing the size of the window, can be performed by pressing {esc} followed by another key. To use a specific function, press {esc} and release it. Then press any of the following keys listed for the desired function:
Experiment with the {esc}ape key functions. You will probably find certain functions more useful than others. Note that you can use the usual {inst/del} key to perform text editing inside a window as well. When a window is set up, all screen output is continued to the "box" you have defined. If you want to clear the window area, press {shift} and {clr/home} together. To cancel the window, press the {clr/home} twice. The window is then restored to its maximum size and the cursor is placed in the top left corner of the screen. If you subsequently want to clear the screen, press {shift} and {clr/home} together. Windows are particularly useful in writing, listing and running programs, because they allow you to work in one area of the screen, while the rest of the screen stays as it is. 5.8 2 MHZ OPERATION5.8.1 The FAST and SLOW CommandsThe 2 Mhz operation mode allows you to run non-graphic programs in 80-column format at twice the normal speed. You can switch normal and fast operation by using the FAST and SLOW commands. The FAST command places the Commodore 128 in 2 Mhz mode. The format of this command is: FAST The SLOW command returns the Commodore 128 to 1 Mhz mode. The format of this command is: SLOW 5.9 KEYS UNIQUE TO C128 MODE5.9.1 Function KeysThe four keys on the Commodore 128 keyboard on the right side above the numeric keypad are special function keys that let you save time by performing repetitive tasks with the stroke of just one key. The first key reads F1/F2, the second F3/F4, the third F5/F6 and the last F7/F8. You can use functions 1, 3, 5 and 7 by pressing the key itself. To use the functions 2, 4, 6 and 8, press {shift} and the function key. Here are the standard functions for each key: F1 F2 F3 F4 GRAPHIC DLOAD" DIRECTORY SCNCLR F5 F6 F7 F8 DSAVE" RUN LIST MONITOR Here is what each function involves:
5.9.2 Redefining Function KeysYou can redefine or program any of these keys to perform a function that suits your needs. Redefining is easy, using the KEY command. You can redefine the keys from BASIC programs, or change them at any time in DIRECT mode. A situtation where you might want to redefine a function key is when you use a command frequently, and want to save time instead of repeatedly typing in the command. The new definitions are erased when you turn off the computer. You can redefine any of the function keys ans as many times as you want. If you want to reprogram the F7 function key to return you to text mode from high resolution or multicolor graphics modes, for example, you would use the key command in this fashion: KEY 7,"GRAPHIC 0" + CHR$(13) CHR$(13) is the ASCII code character for {return}. So when you press the {f7} key after redefining the key, what happens is the command "GRAPHIC 0" is automatically typed out and entered into the computer with {return}. Entire commands or series of commands may be assigned to a function key. 5.9.3 Other Keys Used in C128 Mode Only5.9.3.1 HELPAs noted previously, when you make an error in a program, your computer displays an error message to tell you what you did wrong. These error messages are further explained in Appendix A of this manual. You can get more assistance with errors by using the {help} key. After an error message, press the {help} key to locate the exact point where the error occurred. When you press {help}, the line with the error is highlighted on the screen in reverse video (in 40-column) or underlined (in 80-column). For example: ?SYNTAX ERROR IN LINE 10Your computer displays this. HELPYou pressed the {help} key. 10 PRONT "COMMODORE COMPUTERS"The line with the mistake is highlighted in reverse if in 40-column output or underlined in 80-column output. 5.9.3.2 NO SCROLLPress this key down to stop the text from scrolling when the cursor reaches the bottom of the screen. This key turns off scrolling until you press a key (any character key will do). 5.9.3.3 CAPS LOCKThis key lets you type in all capital letters without using the {shift} key. The {caps lock} key locks in when you press it, and must be pressed again to be released. {caps lock} only effects the lettered keys. 5.9.3.4 40/80 DISPLAYThe 40/80 key selects the main (default) screen format: either 40- or 80-column. The selected screen displays all the messages and output at power-up, or when the {reset} button, or the key combination {run/stop restore} is pressed. The 40/80 key may be used to set the display format only before turning on or resetting the computer. You cannot change modes with this key after the computer is turned on, unless you use the {reset} button at the right side of the computer, or the key combination {run/stop restore}. Section 8 provides an explanation of 40/80 column modes. 5.9.3.5 ALTThe {alt} allows programs to assign a special meaning to a given key or set of keys. Unless the specific application program redefines it, holding down the {alt} key and any other key has no effect. 5.9.3.6 TABThis key works like the TAB key on a typewriter. It may be used to set or clear tab stops on the screen and to move the cursor to the columns where the tabs are set. 5.9.3.7 LINE FEEDPressing this key advances the cursor to the next line, similar to the {crsr down} key. *************************************************************************** FOR MORE INFORMATIONThis section covers only some of the concepts, keys and commands that make the Commodore 128 a special machine. You can find further explanation of the BASIC language in the BASIC 7.0 Encyclopaedia in Chapter V. |
page URL: www.bigfoot.com/~c128page/c128sg/sect-05.htm
contact: c128page@bigfoot.com