SECTION 33. Getting Started in Basic3.1 BASIC PROGRAMMING LANGUAGEThe BASIC programming language is a special language that lets you communicate with your Commodore 128. Using BASIC is one means by which you instruct your computer what to do. BASIC has its own vocabulary (made up of commands, statements and functions) and its own rules of structure (called syntax). You can use the BASIC vocabulary and syntax to create a set of instructions called a program, which your computer can then perform or "run". Using BASIC, you can communicate with your Commodore 128 in two ways: within a program, or directly (outside a program). 3.1.1 Direct ModeYour Commodore 128 is ready to accept BASIC commands in direct mode as soon as you turn on the computer. In the direct mode, you type commands on the keyboard and enter them into the computer by pressing the {return} key. Most BASIC commands in your Commodore 128 can be used in direct mode as well as in a program. 3.1.2 Program ModeIn program mode you enter a set of instructions that perform a specific task. Each instruction is contained in a sequential program line. A statement in a program may be as long as 160 characters; this is equivalent to four full screen lines in 40-column format, and two full screen lines in 80-column format. Once you have typed a program, you can use it immediately by typing the RUN command and pressing the {return} key. You can also store the program on disk or tape by using the DSAVE (or SAVE) command. Then you recall it from the disk or tape by using the DLOAD (or LOAD) command. This command copies the program from the disk or tape and places that program in the Commodore 128's memory. You can then use or "execute" the program again by entering the RUN command. All these commands are explained later in this section. Most of the time you will be using you computer with programs, including programs you yourself write, and commercially available software packages. The only time you operate in direct mode is when you are manipulating or editing your programs with commands such as LIST, LOAD, SAVE and RUN. As a rule, the difference between direct mode and operation within a program is that direct mode commands have no line numbers. 3.2 USING THE KEYBOARDShown below is the keyboard of the Commodore 128 Personal Computer. 3.2.0.1 C128 Keyboard Lay-OutNote: Outlined key areas can be used in C64 Mode. Using BASIC is essentially the same in both C64 and C128 modes. Most of the keys, and many of the commands you will learn, can be used to program BASIC in either mode. The keys that are shaded in the diagram above can be used in C64 mode. In C128 mode you can use all of the keys on the keyboard. 3.2.1 Keyboard Character SetsThe Commodore 128 keyboard offers two different sets of characters:
In 80-column format, both character sets are available simultaneously. This gives you a total of 512 different characters that you can display on the screen. In 40-column format you can use only one character set at a time. When you turn on the Commodore 128 in 40-column format, the keyboard normally is using the uppercase/graphic character set. This means that everything you type is in capital letters. To switch back and forth between the two character sets, press the {shift} key and the {C=} key (the COMMODORE key) at the same time. To practice using the two character sets turn on your computer and press several letters or graphic characters. Then press the {shift} key and the {C=}(Commodore) key. Notice how the screen changes to upper- and lowercase characters. Press {shift} and {C=} again to return to the uppercase and graphic character set. 3.2.2 Using the Command KeysCOMMAND keys are keys that send messages to the computer. Some command keys (such as {return}) are used by themselves. Other keys such as {shift}, {ctrl}, {C=} and {restore}) are used with other keys. The use of each of the command keys is explained below. The keys used in C128 mode are described in Section 5. 3.2.2.1 ReturnWhen you press the {return} key, what you have typed is sent to the Commodore 128 computer's memory. Pressing the {return} key also moves the cursor (the small flashing rectangle that marks where the next character you type will appear) to the next line. At times you may misspell a command or type in something the computer does not understand. Then, when you press the {return} key, you probably will get a message like SYNTAX ERROR on the screen. This is called an "Error Message". Appendix A lists the error messages and tells how to correct the errors. 3.2.2.2 ShiftThere are two {shift} keys on the bottom row of the keyboard. One key is the one on the left and the other on the right, just as on a standard typewriter keyboard. The {shift} key can be used in three ways:
3.2.2.3 Shift LockWhen you press this key down, it locks into place. Then, whatever you type will either be a capital letter, or the top character of a double-character key. To release the lock, press down on the {shift lock} key again. 3.2.2.4 Moving the cursorIn C128 mode, you can move the cursor by using either the four arrow keys located just above the top right of the main keyboard, or the two keys labeled {crsr}, at the right of the bottom row of the main keyboard. 3.2.2.4.1 Using the four Arrow Cursor keysIn C128 mode, the cursor can be moved in any direction simply by using the arrow key in the top row that points in the direction you want to move the cursor. (These keys cannot be used in C64 mode). 3.2.2.4.2 Using the CRSR keysIn both C128 and C64 mode, you can use the two keys on the right side of the bottom row of the main keyboard to move the cursor:
You don't have to keep tapping a cursor key to move more than one space. Just hold the key down and the cursor continues to move, release it when it reaches the position you want. Notice that when the cursor reaches the right side of the screen, it "wraps", or starts again at the beginning of the next row. When moving left, the cursor will move along until it reaches the edge of the screen, then it will jump up to the end of the preceding line. You should try to become very familiar with the cursor keys, because moving the cursor makes your programming much easier. With a little practice you will find that you can move the cursor almost without thinking about it. 3.2.2.5 Inst/DelThis is a dual purpose key. INST stands for INSerT, and DEL for DELete. 3.2.2.5.1 Inserting CharactersYou must use the {shift} key with the {inst/del} key when you want to insert characters in a line. Suppose you left some characters out of a line like this: WHILE WERE OUT_ To insert the missing characters, first use the cursor keys to move the cursor back to the error. like this: WHILE_WERE OUT Then, while you hold down the {shift} key, press the {inst/del} key until you have enough space to add the missing characters: WHILE_ WERE OUT Notice that {inst} doesn't move the cursor; it just adds space between the cursor and the character to its right. To make the correction type the missing {space}, {y}, {o} and {u} like this: WHILE YOU_WERE OUT 3.2.2.5.2 Deleting charactersWhen you press the {del} key, the cursor move one space to the left and erases the character that is there and moves any characters to the right of the cursor one position to the left. This means that when you want to delete something, you move the cursor just to the right of the character you want to DELete. Suppose you have made a mistake in typing, like this: PRINT "ERROER"_ You wanted to type the word ERROR, not ERROER. To delete the incorrect E that precedes the final R, position the cursor on the final R. When you press the {del} key, the R automatically moves over one space to the left. You now have the correct wording like this: PRINT "ERROR" 3.2.2.5.3 Using INSerT and DELete togetherYou can use the INSerT and DELete functions together to fix incorrect characters. First, move the cursor one space after the incorrect characters and press the {inst/del} key by itself to delete the incorrect characters. Next, press the {shift} key and the {inst/del} key together to add any necessary space. 3.2.2.6 ControlThe {ctrl} key is used with other keys to do special task called control functions. To perform a control function, hold down the {ctrl} key while you press some other key. A full list of control sequences is given in ASCII, CHR$ and ESC codes. Control functions are often used in prepackaged software such as a word processing system. One control function that is used often is setting the character and cursor color. To select a color, hold down the {ctrl} key while you press a number key ({1} throught {8}), on the top row of the main keyboard. There are eight more colors available to you; these can be selected with the {C=} key, as explained later. 3.2.2.7 Run/StopThis is a dual function key. Under certain conditions you can use the RUN function of this key by pressing the {shift} and {run/stop} key together. It is also possible to use the STOP function of the key to halt a program or a printout by pressing this key while the program is running. However, in most prepackaged programs, the STOP function of the {run/stop} key is intentionally disabled (made unusable). This is done to prevent the user from trying to stop a program that is running before it reaches its normal end point. If the user were able to stop the program, valuable data could be lost. 3.2.2.8 RestoreThe {restore} key is used with the {run/stop} key to return the computer to its standard condition. Most prepackaged programs disable the {restore} key for the same reason the disable the STOP function of the {run/stop} key: to prevent losing valuable data. 3.2.2.9 CLR/HomeCLR stands for CLeaR. HOME refers to the upper left corner of the screen, which is called the HOME position. If you press this by itself the cursor returns to the HOME position. When you use the {shift} key with the {clr/home} key, the screen CLeaRs and the cursor returns to the HOME position. 3.2.2.10 Commodore keyThe {C=} key (known as the {commodore} key) has a number of functions, including the following ones:
3.2.3 Function KeysThe four keys above the numeric keypad (marked F1, F3, F5 and F7 on the top and F2, F4, F6 and F8 on the front) are called function keys. In both C128 and C64 modes, you can program the function keys. (See the KEY command descriptions in Section 5, paragraph 5.9.2, of Chapter II and in Section 17, paragraph 17.53, Chapter V, BASIC 7.0 Encyclopaedia.) These keys are often used by prepackaged software to allow you to perform a task with a single keystroke. 3.2.4 Displaying Graphic CharactersTo display the graphic symbol on the right front of a key, hold down the {shift} key while you press the key that has the graphic character you want to print. You can display the right side graphic character only when the keyboard is in the uppercase/graphics character set (one normal character set usually available at power-up). To display the graphic character on the left front face of a key, hold down the {C=} key while you press the key that has the graphic character you want. You can display the left graphic character while the keyboard is in either character set. 3.2.5 Rules for Typing BASIC Language ProgramsYou can type and use BASIC language programs even without knowing BASIC. You must type carefully, however, because a typing error may cause the computer to reject your information. The following guidelines will help minimize errors when typing or copying a program listing.
Follow these guidelines when you type the examples and programs shown in this section. 3.3 GETTING STARTED - THE PRINT COMMANDThe PRINT command tells the computer to display information on the screen. You can print both numbers and text (letters), but there are special rules for each case, described in the following paragraphs. 3.3.1 Printing NumbersTo print numbers, use the PRINT command followed by the number(s) you want to print. Try typing this on you Commodore 128: PRINT 5 Then press the {return} key. Notice the number 5 is now displayed on the screen. Now type this and press {return}: PRINT 5,6 In this PRINT command, the comma tells the Commodore 128 that you want to print more than one number. When the computer finds commas in a string of numbers in a PRINT statement, the output is displayed to the nearest tenth column. If you don't want all the extra spaces, use a semicolon (;) in your PRINT statement instead of a comma. The semicolon tells the computer to print the numbers next to each other. A number when printed has either a space or a minus sign preceding it and a skip character after it. Type these examples and see what happens: PRINT 5;6 {return} PRINT 100;-200;300;-400;500 {return} 3.3.2 Using the Question Mark to Abbreviate the PRINT CommandYou can use a quotation mark (?) as an abbreviation for the PRINT command. Many of the examples in this section use the ? symbol in place of the word PRINT. In fact, most of the BASIC commands can be abbreviated. The abbreviations for BASIC commands can be found in Appendix K of this Guide. 3.3.3 Printing TextNow that you know how to print numbers, it's time to learn how to print text. It's actually very simple. Any words or characters you want to display are typed on the screen, with a quote symbol at each end of the string of characters. String is the BASIC name for any set of characters surrounded by quotes. The quote character is obtained by pressing "SHIFT" and the number {2} key on top of the main keyboard (not the {2} in the numeric keypad). Try these examples: ? "COMMODORE 128" {return} ? "4*5" {return} Notice that when you press {return}, the computer displays the character within the quotes on the screen. Also note that the second example did not calculate 4*5 since it was treated as a string and not a mathematical calculation. If you want to calculate the result of 4*5, use the following command: ? 4*5 {return} You can PRINT any string you want by using the PRINT command and surrounding the printed characters with quotes. You can combine text and calculations in a single PRINT command like this: ? "4*5 = "4*5 {return} See how the computer PRINTs the characters in quotes, makes the calculation and PRINTs the result. It doesn't matter whether the text or calculation comes first. In fact, you can use both several times in one PRINT command. Type the following statement: ? 4*(2+3)" is the same as "4*5 {return} Notice that even spaces inside the quotation marks are printed on the screen. Type: ? " OVER HERE" {return} 3.3.4 Printing in Different ColorsThe Commodore 128 is capable of displaying 16 different colors on the screen. You can change colors easily. All you do is hold down the {ctrl} key and press a numbered key between one and eight on the top row of the main keyboard. Notice that the cursor changes color according to the numbered key you pressed. All the succeeding characters are displayed in the color you selected. Hold down the {C=} key and press a numbered key between one and eight, and eight additional colors are displayed on the screen. Table 3-1 list the colors available in C128 mode, for both 40-column and 80-column screen formats. The tables also show the key sequence (CONTROL key plus number key, or {C=} key plus number key) used to specify a given color. Table 3-1. Available Colors in 40- and 80-Column Formats.CONTROL + Color {C=} + Color 1 Black 1 Orange 2 White 2 Brown 3 Red 3 Light Red 4 Cyan 4 Dark Grey 5 Purple 5 Middle Grey 6 Green 6 Light Green 7 Blue 7 Light Blue 8 Yellow 8 Light GreyColors in 40-Column Format CONTROL + Color {C=} + Color 1 Black 1 Dark Purple 2 White 2 Brown 3 Dark Red 3 Light Red 4 Light Cyan 4 Dark Cyan 5 Light Purple 5 Middle Grey 6 Dark Green 6 Light Green 7 Dark Blue 7 Light Blue 8 Light Yellow 8 Light GreyColors in 80-Column Format 3.3.5 Using the Cursor Keys Inside Quotes with the PRINT CommandWhen you type the cursor keys inside quotation marks, graphic characters are shown on the screen to represent the keys. These characters will NOT be printed on the screen when you press {return}. Try typing a question mark ({?}), open quotes ({shift}ed {2} key); then press either of the down cursor keys 10 times, enter the words "DOWN HERE", and close the quotes. The line should look like this: ?"QQQQQQQQQQDOWN HERE" Now press {return}. The Commodore 128 prints 10 blank lines, and on the eleventh line, it prints "DOWN HERE". As this example shows, you can tell the computer to print anywhere on your screen by using the cursor control keys inside quotation marks. |
page URL: www.bigfoot.com/~c128page/c128sg/sect-03a.htm
contact: c128page@bigfoot.com