direct modeprogram mode
[continue/next section][MAIN/Introduction][table of contents]

Getting Started in Basic - continued



3.4.1 What a Program Is
3.4.2 Line Numbers
3.4.3 Viewing your Program - The LIST Command
3.4.4 A Simple Loop - The GOTO Command
3.4.5 Clearing the Computer's Memory - The NEW Command
3.4.6 Using Color in a Program


3.5.1 Erasing a Line from a Program
3.5.2 Duplicating a Line
3.5.3 Replacing a Line
3.5.4 Changing a Line


3.6.1 Addition and Subtraction
3.6.2 Multiplication and Division
3.6.3 Exponentiation
3.6.4 Order of Operations
3.6.5 Using Parentheses to Define the Order of Operations


3.7.1 Constants
3.7.2 Variables
3.7.3 Strings


3.9.1 Formatting a Disk - The HEADER Command
3.9.2 SAVEing on Disk
3.9.3 SAVEing on Cassette
3.9.4 LOADing from Disk
3.9.5 LOADing from Cassette Tape
3.9.6 Other Disk-Related Commands Verifying a Program Displaying Your Disk Directory


So far most of the commands we have discussed have been performed in DIRECT mode. That is, the command was executed as soon as the {return} key was pressed. However, most BASIC commands and functions can also be used in programs.

3.4.1 What a Program Is

A program is just a set of numbered BASIC instructions that tell your computer what you want it to do. These numbered instructions are referred to as statements or lines.

3.4.2 Line Numbers

The lines of a progam are numbered so that the computer knows in what order yo want them executed or RUN. The computer executes the program lines in numerical order, unless the program instructs otherwise. You can use any whole number from 0 to 63999 for a line number. Never use a comma in a line number.

Many of the commands you have learned to use in DIRECT mode can easily be made into program statements. For example, type this:

10 ?"COMMODORE 128" {return}

Notice the computer did not display COMMODORE 128 when you pressed {return}, as it would do if you were using the PRINT command in DIRECT mode. This is because the number, 10, that comes before the PRINT symbol (?) tells the computer that you are entering a BASIC program. The computer just stores the numbered statement and waits for the next input from you.

Now type RUN and press {return}. The computer prints the words COMMODORE 128. This not the same as using the PRINT command in DIRECT mode. What has happened here is that YOU HAVE WRITTEN AND RUN YOUR FIRST BASIC PROGRAM as small as it may seem. The program is still in the computer's memory, so you can run it as many times as you want.

3.4.3 Viewing your Program - The LIST Command

Your one-line program is still in the C128 memory. Now clear the screen by pressing the {shift} and {clr/home} keys together. The screen is empty. At this point you may want to see the program listing to be sure it is still in memory. The BASIC language is equipped with a command that lets you do just this - the LIST command.

Type LIST and press {return}. The Commodore 128 responds with:


Anytime you want to see all the lines in your program, type LIST. This is especially helpful if you make changes, because you can check to be sure the new lines have been registered in the computer's memory. In response to the command, the computer displays the changed version of the line, lines, or program. Here are the rules for using the LIST command:

  • To see line n only, type LIST n and press {return}.
  • To see from line n to the end of the program, type LIST n- and press {return}.
  • To see the lines from the beginning of the program to line n, type LIST -n and press {return}.
  • To see from line n1 to line n2 inclusive, type LIST n1-n2 and press {return}.

3.4.4 A Simple Loop - The GOTO Command

The line numbers in a program have another purpose besides putting your commands in the proper order for the computer. The serve as a reference for the computer in case you want to execute the command in that line repetitively in your program. You use the GOTO command to tell the computer to go to a line and execute the command(s) in it. Now type:

20 GOTO 10

When you press {return} after typing line 20, you add it to your program in the computer's memory.

Notice that we numbered the first line 10 and the second line 20. It is very helpful to number program lines in increments of 10 (that is 10, 20, 30, 40, etc.) in case you want to go back and add lines in between. You can number such added lines by fives (15, 25...), or ones (1, 2...) - in fact, by any whole number - to keep the lines in proper order. (See the RENUMBER and AUTO commands in the BASIC 7.0 Encyclopaedia, paragraphs 17.74 and 17.2, respectively.)

Type RUN and press {return}, and watch the words COMMODORE 128 move down your screen. To stop the message from printing on the screen, press the {run/stop} key on the left side of the main keyboard.

The two lines that you have typed make up a simple program that repeats itself endlessly, because the second line keeps referring the computer back to the first line. The program will continue indefinitely unless you stop it or turn off the computer.

Now type LIST {return} The screen should say:

20 GOTO 10

Your program is still in memory. You can RUN it again if you want to. This is an important difference between PROGRAM mode and DIRECT mode. Once a command is executed in DIRECT mode, it is no longer in the computer's memory. Notice that even though you used the ? symbol for the PRINT statement, your computer has converted it into the full command. This happens when you LIST any command you have abbreviated in a program.

3.4.5 Clearing the Computer's Memory - The NEW Command

Anytime you want to start all over again or erase a BASIC program in the computer's memory, just type NEW and press {return}. This command clears out the computer's BASIC memory, the area where programs are stored.

3.4.6 Using Color in a Program

To select color within a program, you must include the color selection information within a PRINT statement. For example, clear your computer's memory by typing NEW and pressing {return}, the type the following, being sure to leave space between each letter:

10 PRINT " R A I N B O W" {return}

Now type line 10 again, but this time hold down the {ctrl} key and press the (numeral) {1} key directly after entering the first set of quote marks. Release the {ctrl} key and type the {r}. Now hold down the {ctrl} again and press the {2} key. Release the {ctrl} key and type the {a}. Next hold down the {ctrl} again and press the {3} key. Continue this process until you have typed all the letters in the word RAINBOW and selected a color between each letter. Press the {shift} and the {2} keys to type a set of the closing quotation marks and press the {return} key. Now type RUN and press the {return} key. The computer displays the word RAINBOW with each letter in a different color. Now type LIST and press the {return} key. Notice the graphic characters that appear in the PRINT statement in line 10. These characters tells the computer what color you want for each printed letter. Note that these graphic characters do not appear when the Commodore 128 PRINTs the word RAINBOW in different colors.

The color selection characters, known as control characters, in the PRINT statement in line 10 tell the Commodore 128 to change colors. The computer then prints the characters that follow in the new color until another color selection character is encountered. While characters enclosed in quotation marks are usually PRINTed exactly as they appear, control characters are only displayed within a program LISTing.


The following paragraphs will help you to type in your programs and make corrections and additions to them.

3.5.1 Erasing a Line from a Program

Use the LIST command to display the program you typed previously. Now type 10 and press {return}. You just erased line 10 from the program. LIST your program and see for yourself. If the old line 10 is still on the screen, move the cursor up so that it is blinking anywhere on that line. Now, if you press {return}, line 10 is back in the computer's memory.

3.5.2 Duplicating a Line

Hold down the {shift} key and press the {clr/home} key on the upper right side of the main keyboard. This will clear your screen. Now LIST the program. Move the cursor up again so that it is blinking in the (numeral) "0" in the line numbered 10. Now type a {5} and press {return}. You have just duplicated (i.e. copied) line 10. The duplicated line is numbered 15. Type LIST and press RETURN to see the program with the duplicated line.

3.5.3 Replacing a Line

You can replace a whole line by typing in the old line number followed by the text of the new line, the pressing {return}. The old version of the line will be erased from memory and replaced by the new line as soon as you press {return}.

3.5.4 Changing a Line

Suppose you want to add something in the middle of a line. Simply move the cursor to the character or space that immediately follows the spot where you want to insert the new material. Then hold down the {shift} key and the {inst/del} key together until there is enough space to insert your new characters.

Try this example. Clear the computer's memory by typing NEW and pressing {return}. The type:

10 ? "MY 128 IS GREAT" {return}

Let's say you want to add the word COMMODORE in front of the number 128. Just move the cursor so that it is blinking on the "1" in 128. Hold down the {shift} and {inst/del} keys until you have enough room to type in COMMODORE (don't forget to leave enough room for a space after the last letter "E"). Then type in the word COMMODORE.


You can use the PRINT command to perform calculations like addition, subtraction, multiplication, division and exponentiation. You type the calculation after the PRINT command.

3.6.1 Addition and Subtraction

Try typing these examples:

PRINT 6 + 4 {return}

PRINT 50 - 20 {return}

PRINT 10 + 15 - 5 {return}

PRINT 75 - 100 {return}

PRINT 30 + 40,55 - 25 {return}

PRINT 30 + 40;55 - 25 {return}

Notice that the fourth calculation (75-100) resulted in a negative number. Also notice that you can tell the computer to make more than one calculation with a single PRINT command. You can use either a comma or semicolon in your command, depending on whether or not you want your results printed tabulated or next to each other.

3.6.2 Multiplication and Division

Find the asterisk key (*) on the right side of the main keyboard. This is the symbol that the Commodore 128 uses for multiplication. The slash (/) key, located next to the right {shift} key, is used for division.

Try these examples:

PRINT 5*3 {return}
PRINT 100/2 {return}

3.6.3 Exponentiation

Exponentiation means to raise a number to a power. The up arrow key ({up arrow}, located next to the asterisk on the main keyboard, is used for exponentiation. If you want to raise a number to a power, use the PRINT command, followed by the number, the up arrow and the power, in that order. For example, to find out what 3 squared is, type:

PRINT 3{up arrow}2 {return}

NOTE: From now on, the {up arrow} keystroke is represented by the {^} keystroke, or simply ^.

3.6.4 Order of Operations

You have seen how you can combine addition and subtraction in the same PRINT command. If you combine multiplication or division with addition or subtraction operations, you may not get the result you expect. For example, type:

PRINT 4 + 6/2 {return}

If you assumed you were dividing 10 by 2, you were probably surprised when the computer responded with the answer 7. The reason you got this answer is that multiplication and division operations are performed by the computer before addition or subtraction. Multiplication and division are said to take precedence over addition and subtraction. It doesn't matter in what order you type the operation. In computing, the order in which mathematical operations are performed is known as the order of operations.

Exponentiation, or raising a number to a power, takes precedence over the other four mathematical operations. For example, if you type:

PRINT 16/4{up arrow}2 {return}
the Commodore 128 responds with 1, because it squares the 4 before it divides 16.

3.6.5 Using Parentheses to Define the Order of Operations

You can tell the Commodore 128 which mathematical operation you want performed first by enclosing that operation in parentheses in the PRINT command. For instance, in the first example above, if you want to tell the computer to add before dividing, type:

PRINT (4 + 6)/2 {return}
This gives you the desired answer, 5.

If you want the computer to divide before squaring in the second example, type:

PRINT (16/4){up arrow}2 {return}
Now you have the expected answer, 16.

If you don't use parentheses, the computer performs the calculations according to the above rules. When all operations in a calculation have equal precedence, they are performed from left to right. For example, type:

PRINT 4*5/10*6 {return}

Since the operations in this example are performed in order from left to right, the result is 12 (4*5 = 20... 20/10 = 2... 2*6 = 12). If you want to divide 4*5 by 10*6 you type:

PRINT (4*5)/(10*6) {return}
The answer is now .333333333.


3.7.1 Constants

Constants are numeric values that are permanent: that is, they do not change in value over the course of an equation or program. For example, the number 3 is a constant, as is any number. This statement illustrates how your program uses constants:

10 PRINT 3

No matter how many times you execute this line, the answer will always be 3.

3.7.2 Variables

Variables are values that can change over the course of an equation or program statement. There is a part of the computer's BASIC memory that is reserved for the characters (number, letters and symbols) you use in your program. Think of this memory as a number of storage compartments in the computer that store information about your program; this part of the computer's memory is referred to as variable storage. Type in this program:

10 X=5
20 ?X

Now RUN the program and see how the computer prints a 5 on your screen. You told the computer in line 10 that the letter X will represent the number 5 for the remaninder of the program. The letter X is called a variable, because the value of X varies depending on the value to the right of the equals sign. We call this an assignment statement because now there is a storage compartment labeled X in the computer's memory, and the number 5 has been assigned to it. The = sign tells the computer that whatever comes to the right of it will be assigned to a storage compartment (a memory location) labeled with the letter X to the left of the equals sign.

The variable name on the left side of the = sign can be either one or two letters, or one letter and one number (the letter MUST come first). The names can be longer, but the computer only looks at the first two characters. This means the names PA and PART would refer to the same storage compartment. Also, the words used for BASIC commands (LOAD, RUN, LIST, etc.) or functions (INT, ABS, SQR, etc.) cannot be used as names in your programs. Refer to the BASIC Encyclopaedia in Chapter V if you have any questions about whether a variable name is a BASIC keyword. Notice that the = in assigment statements is not the same as the mathematical symbol meaning "equals", but rather means allocate a variable (storage compartment) and assign a value to it.

In the sample program you just typed, the value of the variable X remains at 5 throughout. You can put calculations to the right of the = sign to assign the result to a variable. You can mix text with constants in a PRINT statement to identify them. Type NEW and press {return} to clear the Commodore 128's memory; then try this program:

10 A = 3*100
20 B = 3*200

Now there are two variables, labeled A and B, in the computer's memory, containing the numbers 300 and 600 respectively. If, later in the program, you want to change the value of a variable, just put another assignment statement in the program. Add these lines to the program above and RUN it again.

50 A = 900*30/10
60 B = 95 + 32 + 128
70 GOTO 30

You'll have to press the {stop} key to halt the program.

Now LIST the program and trace the steps taken by the computer. First, it assigns the value to the right of the = sign in line 10 to the letter A. It does the same thing in line 20 for the letter B. Next, it prints the messages in lines 30 and 40 that give you the values of A and B. Finally, it assigns new values to A and B in lines 50 and 60. The old values are replaced and cannot be recovered unless the computer executes lines 10 and 20 again. When the computer is sent to line 30 to begin printing the values of A and B again, it prints the new values calculated in lines 50 and 60. Lines 50 and 60 reassign the same values to A and B and line 70 sends the computer back to line 30. This is called an endless loop, because lines 30 through 70 are executed over and over again until you press the {run/stop} key to halt the program. Other methods of looping are discussed later in this and following two sections.

3.7.3 Strings

A string is a character or group of characters enclosed in quotes. These characters are stored in the computer's memory as a variable in much the same way numeric variables are stored. You can also use variable names to represent strings, just as you use them to represent numbers. When you put the dollar sign ($) after the variable name, it tells the computer that the name is for a string variable, and not a numeric variable.

Type NEW and press {return} to clear your computer's memory, then type in the program below:

20 X = 128
30 B$ = "COMPUTER"
40 Y = 1
50 ? "THE "A$;X;B$" IS NUMBER"Y

See how you can print numeric and string variables in the same statement? Try experimenting with variables in your own short programs.

You can print the value of a variable in DIRECT mode, after the program has been RUN. Type ?A$;B$;X;Y {return} after running the program above and see that those four variable values are still in the computer's memory.

If you want to clear this area of BASIC memory but still leave your program intact, use the CLR command. Just type CLR {return} and all constants, variables and strings are erased. But when you type LIST, you can see the program is still in memory. The NEW command discussed earlier erases both the program and the variables.


Here is a sample program incorporating many of the techniques and commands discussed in the section.

This program calculates the avarage of three numbers (X, Y and Z) and prints their values and their avarages on the screen. You can edit the program and change the calculations in line 10 through 30 to change the values of the variables. Line 40 adds the variables and divides them by 3 to get the average. Note the use of parentheses to tell the computer to add the numbers before it divides.

TIP: Whenever you are using more than one set of parentheses in a statement, it's a good idea to count the number of left parentheses and right parentheses to make sure they are equal.

10 X = 46
20 Y = 73
30 Z = 114
40 A = (X + Y + Z)/3
60 END


Once you have created your program, you will probably want to store it permanently so you will be able to recall and use it at some later time. To do this, you'll need either a Commodore disk drive or the Commodore 1530 (or 1531) Datassette, or similar storing device.

You will learn several commands that let you communicate between your computer and your disk drive or Datassette. These commands are structured with the use of a command word followed by several parameters. Parameters are letters, words or symbols in a command that supply specific information to the computer, such as a filename, or a numeric variable that specifies a device number. Each command may have several parameters. For example, the parameter of the disk format command include a name for the disk and an identifying number or code, plus several other parameters. Parameters are used in almost every BASIC command; some are variables which change and others are constant. These are the parameters that supply disk information to the C128 and the disk drive: Disk Handling Parameters

disk name
arbitrary 16 character identifying name you supply.
arbitrary 16 character identifying name you supply.
i.d. number
arbitrary two-character identifying code you supply.
drive number
must use 0 for a single disk drive, 0 or 1 in a dual disk drive.
device number
a preassigned number for a peripheral device. For example, the device number for a Commodore disk drive is 8.

3.9.1 Formatting a Disk - The HEADER Command

To store programs on a new (or blank) disk, you must first prepare the disk to receive data. This is called "formatting" the disk.

Make sure you turn on the disk drive before inserting any disk.

The formatting process divides the disk into sections called tracks and sectors. A table of contents, called a directory, is created. Each time you store a program on disk, the name you assign to that program will be added to the directory.

The Commodore 128 has two kinds of formatting commands. One can be used in C128 mode only, and one can be used in both C64 and C128 mode. The following describes the C128 mode formatting command only. See your disk drive manual for the disk handling in C64 mode.

The command that formats a diskette is called the HEADER command. It has a long form and a short form. To format a blank (new) disk, you MUST use the long form as follows:

HEADER "diskname", Ii.d. [,Ddrive number] [,[ON]Udevice number]

After the word HEADER, you type a name of your choice for the disk, within quotes. You can choose any name with up to 16 characters. You should choose disk names that help you identify what will be stored on the disk.

Follow the diskname with a comma and the letter "I". Now a two character i.d. Your disk i.d. does not have to be numbers; you can also choose letters. You may want to develop a consecutive coding system for your disk, such as A1, A2, B1, B2.

If you have one single disk drive, just press {return} at this point since the Commodore 128 automatically assumes the drive number is 0 and the device number is 8. You can specify these parameters if you have more than one drive or a dual drive.

The next parameter in the command selects the drive number. Press the {d} key and if you have a single disk drive, press the {zero} key followed by a {comma}. Dual drives are labeled 0 and 1. The device number parameter starts with the letter U so press the {u} key followed by the preassigned device number for a Commodore disk drive which is {8}.

Here is an example of the long form of the HEADER command:

HEADER"RECS",IA1,D0,U8 {return}

This command formats the diskette, calling it "RECS", the i.d. number "A1", on drive 0, unit 8.

The default values for disk drive (0) and device number (8) will be used if none are supplied. This is an acceptable long form of the HEADER command:

HEADER "MYDISK",I51 {return}

The HEADER command can also be used to erase all data from a used disk, so the disk can be reused as if it were a brand new disk. Be careful that you don't erase a disk that contains data you may want someday.

The quick form of the HEADER command can be used if the disk was previously formatted with the long form of the HEADER command.

The quick form clears the directory, erasing all data in the same way as the long form, but keeps the same i.d. as was previously used. Here is what the quick HEADER might look like:


3.9.2 SAVEing on Disk

In C128 mode, you can store you program on disk by using either of the following commands:

DSAVE "program name" {return}
SAVE "program name",8 {return}

Either command can be used. Remember that the character sequence DSAVE" can be displayed on the screen by pressing the function key labeled {f5}, or you can type the sequence yourself. The progam name can be any name you choose, up to 16 characters long. Be sure to enclose the program name in quotes. You cannot put two programs with the same name on the same disk. If you do, the second program will not be accepted; the disk will retain the first one. In the second example, the 8 indicates that you are saving your program on device number 8. You do not need the 8 with DSAVE, because the computer automatically assumes you are using device number 8.

3.9.3 SAVEing on Cassette

If you are using a Datassette to store your program, insert a blank tape in the recorder, rewind the tape if necessary, and type:

SAVE "program name" {return}

You must type the word SAVE, followed by the program name. The program name can be any name you choose up to 16 characters.

NOTE: The 40-column screen will go blank while the program is being SAVEd, but returns to normal when the process is completed.

Unlike disk, you can save two programs to tape under the same name. However when you load it back into the computer, the first progam sequentially on the tape will be loaded, so avoid giving programs the same name.

Once a program has been SAVEd, you can LOAD it back into the computer's memory and RUN it anytime you wish.

3.9.4 LOADing from Disk

Loading a program simply copies the contents of the program from the disk into the computer's memory. If a BASIC program was already in memory before you issued the LOAD command, it is erased.

To load your BASIC program from a disk, use either ot the following commands in C128 mode:

DLOAD "program name" {return}
LOAD "program name",8 {return}

Remember, in C128 mode you can use the {f2} function key (which you activate by pressing {shift} and {f1} together) to display the sequence DLOAD", or you can type the letters yourself. In the second example, the 8 indicates to the computer that you are loading from device number 8. Again, like DSAVE, DLOAD assumes the disk drive device number is 8. Be careful to type the program name exactly as you typed it when SAVEing the program, or the computer will respond FILE NOT FOUND.

Once the program is loaded, type RUN to execute. The Commodore 128 has a special form of the RUN command used to LOAD and RUN the program in C128 mode with one command. Type RUN, followed by the name of the program (also known as the filename) in quotes:

RUN"MYPROG" {return}

3.9.5 LOADing from Cassette Tape

To LOAD your program from cassette tape, type:

LOAD "program name" {return}

If you do not know the name of the program, you can type:

LOAD {return}
and the next program on tape will be found. While the Datassette is searching for the program the 40 column screen is blank. When the program is found, the screen displays:

To actually load the program, you then press the Commodore key, or in 128 mode press the space bar to find the next program on tape.

You can use the counter on the Datassette to identify the starting position of the programs. Then, when you want to retrieve a program, simple wind the tape forward from 000 to the program's start location, and type:

LOAD {return}

In this case you don't have to specify the program name; your program will load automatically because it is the next program on the tape.

3.9.6 Other Disk-Related Commands Verifying a Program

To verify that a program has been correctly saved, use the following command in C128 mode:

DVERIFY "program name" {return}

If the program in the computer is identical to the one on the disk, the screen display will respond with the letters "OK".

The VERIFY command also works for tape programs. You type:

VERIFY "program name" {return}

You do not enter the comma and a device number. Displaying Your Disk Directory

In C128 mode, you can see a list or directory of the programs on your disk by using the following command:

DIRECTORY {return}

This lists the contents of the directory. The easy way is to press the {f3} function key. When you press {f3}, the C128 displays the word DIRECTORY and performs the command.

For further information on SAVEing and LOADing your programs, or other disk related information refer to your Datassette or disk drive manual. Also consult the LOAD and SAVE command descriptions in the Chapter V, BASIC 7.0 Encyclopaedia.


You now know something about the BASIC language and some elementary programming concepts. The next section builds on these concepts, introducing additional commands, functions and techniques that you can use to program in BASIC.

[top of document]

page URL: