Action: This is the statement that gives BASIC most of its "intelligence," the ability to evaluate conditions and take different actions de- pending on the outcome.
The word IF is followed by an expression, which can include variables, strings, numbers, comparisons, and logical operators. The word THEN appears on the same line and is followed by either a line number or one or more BASIC statements. When the expression is false, everything after the word THEN on that line is ignored, and execution continues with the next line number in the program. A true result makes the program either branch to the line number after the word THEN or execute whatever other BASIC statements are found on that line.
EXAMPLE of IF...GOTO...Statement:
1 2 3
100 INPUT "TYPE A NUMBER"; N 110 IF N 0" 210 GOTO 100
This program prints out the square root of any positive number. The IF statement here is used to validate the result of the INPUT. When the result of N " ; ]
Action: This is a statement that lets the person RUNning the program "feed" information into the computer. When executed, this statement PRINTs a question mark (?) on the screen, and positions the cursor 1 space to the right of the question mark. Now the computer waits, cursor blinking, for the operator to type in the answer and press the key.
The word INPUT may be followed by any text contained in quote marks (""). This text is PRINTed on the screen, followed by the question mark.
After the text comes a semicolon (;) and the name of one or more variables separated by commas. This variable is where the computer stores the information that the operator types. The variable can be any legal variable name, and you can have several different variable names, each for a different input.
EXAMPLES of INPUT Statement:
1 2 3
100 INPUT A 110 INPUT B, C, D 120 INPUT "PROMPT"; E
When this program RUNs, the question mark appears to prompt the operator that the Commodore 64 is expecting an input for line 100. Any number typed in goes into A, for later use in the program. If the answer typed was not a number, the ?REDO FROM START message appears, which means that a string was received when a number was expected.
If the operator just hits without typing anything, the vari- able's value doesn't change.
Now the next question mark, for line 110, appears. If we type only one number and hit the , Commodore 64 will now display 2 question marks (??), which means that more input is required. You can just type as many inputs as you need separated by commas, which prevents the double question mark from appearing. If you type more data than the INPUT statement requested, the ?EXTRA IGNORED message appears, which means that the extra items you typed were not put into any variables.
Line 120 displays the word PROMPT before the question mark appears. The semicolon is required between the prompt and any list of variables.
The INPUT statement can never be used outside a program. The Commodore 64 needs space for a buffer for the INPUT variables, the same space that is used for commands.
INPUT#
TYPE: I/O Statement FORMAT: INPUT# ,
Action: This is usually the fastest and easiest way to retrieve data stored in a file on disk or tape. The data is in the form of whole vari- ables of up to 80 characters in length, as opposed to the one-at-a-time method of GET#. First, the file must have been OPENed, then INPUT# can fill the variables.
The INPUT# command assumes a variable is finished when it reads a RETURN code (CHR$ (13)), a comma (,), semicolon (;), or colon (:). Quote marks can be used to enclose these characters when writing if they are needed (see PRINT# statement).
If the variable type used is numeric, and non-numeric characters are received, a BAD DATA error results. INPUT# can read strings up to 80 characters long, beyond which a STRING TOO LONG error results.
When used with device #3 (the screen), this statement will read an entire logical line and move the cursor down to the next line.
EXAMPLES of INPUT# Statement:
1 2
10 INPUT#1,A 20 INPUT#2,A$,B$
INT
TYPE: Integer Function FORMAT: INT ()
Action: Returns the integer value of the expression. If the expression is positive, the fractional part is left off. If the expression is negative, any fraction causes the next lower integer to be returned.
EXAMPLES of INT Function:
1 2 3 4
120 PRINT INT(99.4343), INT(-12.34)
99 -13
LEFT$
TYPE: String Function FORMAT: LEFT$ (, )
Action: Returns a string comprised of the leftmost char- acters of the . The integer argument value must be in the range 0 to 255. If the integer is greater than the length of the string, the entire string will be returned. If an value of zero is used, then a null string (of zero length) is returned.
Action: The LET statement can be used to assign a value to a variable. But the word LET is optional and therefore most advanced programmers leave LET out because it's always understood and wastes valuable memory. The equal sign (=) alone is sufficient when assigning the value of an expression to a variable name.
EXAMPLES of LET Statement:
1 2 3 4
10 LET D= 12 (This is the same as D = 12) 20 LET E$ = "ABC" 30 F$ = "WORDS" 40 SUM$= E$ + F$ (SUM$ would equal ABCWORDS)
LIST
TYPE: Command FORMAT: LIST [[]-[]]
Action: The LIST command allows you to look at lines of the BASIC program currently in the memory of your Commodore 64. This lets you use your computer's powerful screen editor, to edit programs which you've LISTed both quickly and easily.
The LIST system command displays all or part of the program that is currently in memory on the default output device. The LIST will normally be directed to the screen and the CMD statement can be used to switch output to an external device such as a printer or a disk. The LIST command can appear in a program, but BASIC always returns to the system READY message after a LIST is executed.
When you bring the program LIST onto the screen, the "scrolling" of the display from the bottom of the screen to the top can be slowed by holding down the ConTRoL key. LIST is aborted by typing the key.
If no line-numbers are given the entire program is listed. If only the first line number is specified, and followed by a hyphen (-), that line and all higher-numbered lines are listed. If only the last line number is specified, and it is preceded by a hyphen, then all lines from the beginning of the program through that line are listed. If both numbers are specified, the entire range, including the line-numbers LISTed, is displayed.
EXAMPLES of LIST Command:
LIST (Lists the program currently in memory.)
LIST 500 (Lists line 500 only.)
LIST 150- (Lists all lines from 150 to the end.)
LIST -1000 (Lists all lines from the lowest through 1000.)
LIST 150-1000 (Lists lines 150 through 1000, inclusive.)
1 2 3
10 PRINT "THIS IS LINE 10" 20 LIST (LIST used in Program Mode) 30 PRINT "THIS IS LINE 30"
LOAD
TYPE: Command FORMAT: LOAD[""][,][,]
Action: The LOAD statement reads the contents of a program file from tape or disk into memory. That way you can use the information LOADed or change the information in some way. The device number is optional, but when it is left out the computer will automatically default to 1, the cassette unit. The disk unit is normally device number 8. The LOAD closes all open files and, if it is used in direct mode, it performs a CLR (clear) before reading the program. If LOAD is executed from within a program, the program is RUN. This means that you can use LOAD to "chain" several programs together. None of the variables are cleared during a chain operation.
If you are using file-name pattern matching, the first file which matches the pattern is loaded. The asterisk in quotes by itself ("*") causes the first file-name in the disk directory to be loaded. if the filename used does not exist or if it is not a program file, the BASIC error message ?FILE NOT FOUND occurs.
When LOADing programs from tape, the can be left out, and the next program file on the tape will be read. The Commodore 64 will blank the screen to the border color after the PLAY key is pressed. When the program is found, the screen clears to the background color and the "FOUND" message is displayed. When the key, key, key, or is pressed, the file will be loaded. Programs will LOAD starting at memory location 2048 unless a secondary of 1 is used. If you use the secondary address of 1 this will cause the program to LOAD to the memory location from which it was saved.
EXAMPLES of LOAD Command:
LOAD (Reads the next program on tape) LOAD A$ (Uses the name in A$ to search) LOAD"*",8 (LOADs first program from disk) LOAD"",1,1 (Looks for the first program on tape, and LOADs it into the same part of memory that it came from)
LOAD"STAR TREK" (LOAD a file from tape) PRESS PLAY ON TAPE FOUND STAR TREK LOADING READY.
LOAD"FUN",8 (LOAD a file from disk) SEARCHING FOR FUN LOADING READY.
LOAD"GAME ONE",8,1 (LOAD a file to the specific SEARCHING FOR GAME ONE memory location from which the LOADING program was saved on the disk) READY.
LOG
TYPE: Floating-Point Function FORMAT: LOG()
Action: Returns the natural logarithm (log to the base of e) of the argument. If the value of the argument is zero or negative the BASIC error message ?ILLEGAL QUANTITY will occur.
EXAMPLES of LOG Function:
1 2 3 4 5
25 PRINT LOG(45/7) 1.86075234
10 NUM=LOG(ARG)/LOG(10) (Calculates the LOG of ARG to the base 10)
MID$
TYPE: String Function FORMAT: MID$(,[,])
Action: The MID$ function returns a sub-string which is taken from within a larger argument. The starting position of the sub- string is defined by the argument and the length of the sub-string by the argument. Both of the numeric arguments can have values ranging from 0 to 255.
If the value is greater than the length of the , or if the value is zero, then MID$ gives a null string value. If the argument is left out, then the computer will assume that a length of the rest of the string is to be used. And if the source string has fewer characters than , from the starting position to the end of the string argument, then the whole rest of the string is used.
Action: The NEW command is used to delete the program currently in memory and clear all variables. Before typing in a new program, NEW should be used in direct mode to clear memory. NEW can also be used in a program, but you should be aware of the fact that it will erase everything that has gone before and is still in the computer's memory. This can be particularly troublesome when you're trying to debug your program.
BE CAREFUL: Not clearing out an old program before typing a new one can result in a confusing mix of the two programs.
EXAMPLES of NEW Command:
1 2
NEW (Clears the program and all variables) 10 NEW (Performs a NEW operation and STOPs the program.)
NEXT
TYPE: Statement FORMAT: NEXT[][,]...
Action: The NEXT statement is used with FOR to establish the end of a FOR...NEXT loop. The NEXT need not be physically the last statement in the loop, but it is always the last statement executed in a loop. The is the loop index's variable name used with FOR to start the loop. A single NEXT can stop several nested loops when it is followed by each FOR's variable name(s). To do this each name must appear in the order of inner-most nested loop first, to outer-most nested loop last. When using a single NEXT to increment and stop several variable names, each variable name must be separated by commas. Loops can be nested to 9 levels. If the counter variable(s) are omitted, the counter associated with the FOR of the current level (of the nested loops) is incremented.
When the NEXT is reached, the counter value is incremented by 1 or by an optional STEP value. It is then tested against an end-value to see if it's time to stop the loop. A loop will be stopped when a NEXT is found which has its counter value greater than the end-value.
EXAMPLES of NEXT Statement:
1 2 3 4 5 6 7 8 9 10 11 12 13
10 FOR J=1 TO 5: FOR K=10 TO 20: FOR N=5 TO -5 STEP - 1 20 NEXT N,K,J (Stopping Nested Loops)
10 FOR L=1 TO 100 20 FOR M=1 TO 10 30 NEXT M 400 NEXT L (Note how the loops do NOT cross each other)
10 FOR A=1 TO 10 20 FOR B=1 TO 20 30 NEXT 40 NEXT (Notice that no variable names are needed)
NOT
TYPE: Logical Operator FORMAT: NOT
Action: The NOT logical operator "complements" the value of each bit in its single operand, producing an integer "twos-complement" result. In other words, the NOT is really saying, "if it isn't.” When working with a floating-point number, the operands are converted to integers and any fractions are lost. The NOT operator can also be used in a comparison to reverse the true/false value which was the result of a relationship test and therefore it will reverse the meaning of the comparison. In the first example below, if the "twos-complement" of "AA" is equal to "BB" and if "BB" is NOT equal to "CC" then the expression is true.
EXAMPLES of NOT Operator:
1 2 3 4 5
10 IF NOT AA = BB AND NOT(BB = CC) THEN...
NN% = NOT 96: PRINT NN% -97
NOTE: TO find the value of NOT use the expression X=(-(X+1)). (The two's complement of any integer is the bit complement plus one.)
ON
TYPE: Statement FORMAT: ON GOTO / GOSUB [,]...
Action: The ON statement is used to GOTO one of several given line- numbers, depending upon the value of a variable. The value of the variables can range from zero through the number of lines given. if the value is a non-integer, the fractional portion is left off. For example, if the variable value is 3, ON will GOTO the third line-number in the list.
If the value of the variable is negative, the BASIC error message ?ILLEGAL QUANTITY occurs. If the number is zero, or greater than the number of items in the list, the program just "ignores" the statement and continues with the statement following the ON statement.
ON is really an underused variant of the IF...THEN...statement. Instead of using a whole lot of IF statements each of which sends the program to 1 specific line, 1 ON statement can replace a list of IF statements. When you look at the first example you should notice that the 1 ON statement replaces 4 IF...THEN... statements.
EXAMPLES of ON Statement:
1 2 3 4 5
ON -(A=7)-2*(A=3)-3*(A7)GOTO 400,900,1000,100 ON X GOTO 100,130,180,220 ON X+3 GOSUB 9000,20,9000 100 ON NUM GOTO 150,300,320,390 500 ON SUM/2 + 1 GOSUB 50,80,20
OPEN
TYPE: I/O Statement FORMAT: OPEN ,[][,] [," [,] [,]"]
Action: This statement OPENs a channel for input and/or output to a peripheral device. However, you may NOT need all those parts for every OPEN statement. Some OPEN statements require only 2 codes:
1) LOGICAL FILE NUMBER 2) DEVICE NUMBER
The is the logical file number, which relates the OPEN, CLOSE, CMD, GET#, INPUT#, and PRINT# statements to each other and associates them with the file-name and the piece of equipment being used. The logical file number can range from 1 to 255 and you can assign it any number you want in that range.
NOTE: File numbers over 128 were really designed for other uses so it's good practice to use only numbers below 127 for file numbers.
Each peripheral device (printer, disk drive, cassette) in the system has its own number which it answers to. The number is used with OPEN to specify on which device the data file exists. Peripherals like cassette decks, disk drives or printers also answer to several secondary addresses. Think of these as codes which tell each device what operation to perform. The device logical file number is used with every GET#, INPUT#, and PRINT#.
If the number is left out the computer will automatically assume that you want your information to be sent to and received from the Datassette, which is device number 1. The file-name can also be left out, but later on in your program, you can NOT call the file by name if you have not already given it one. When you are storing files on cassette tape, the computer will assume that the secondary is zero (0) if you omit the secondary address (a READ operation).
A secondary address value of one (1) OPENs cassette tape files for writing. A secondary address value of two (2) causes an end-of-tape marker to be written when the file is later closed. The end-of-tape marker prevents accidentally reading past the end of data which results in the BASIC error message ?DEVICE NOT PRESENT.
For disk files, the secondary addresses 2 thru 14 are available for data-files, but other numbers have special meanings in DOS commands. You must use a secondary address when using your disk drive(s). (See your disk drive manual for DOS command details.)
The is a string of 1-16 characters and is optional for cassette or printer files. If the file is left out the type of file will automatically default to the Program file unless the is given.
Sequential files are OPENed for reading =R unless you specify that files should be OPENed for writing =W is specified. A file can be used to OPEN an existing Relative file. Use REL for with Relative files. Relative and Sequential files are for disk only.
If you try to access a file before it is OPENed the BASIC error message ?FILE NOT OPEN will occur. If you try to OPEN a file for reading which does not exist the BASIC error message ?FILE NOT FOUND will occur. If a file is OPENed to disk for writing and the file-name already exists, the DOS error message FILE EXISTS occurs. There is no check of this type available for tape files, so be sure that the tape is properly positioned or you might accidentally write over some data that had previously been SAVED. If a file is OPENed that is already OPEN, the BASIC error message FILE OPEN occurs. (See Printer Manual for further details.)
EXAMPLES of OPEN Statements:
1 2 3 4 5 6 7 8 9 10 11 12
10 OPEN 2,8,4,"DISK-OUTPUT,SEQ,W" (Opens sequential file on disk) 10 OPEN 1,1,2,"TAPE-WRITE" (Write End-of-File on Close) 10 OPEN 50,0 (Keyboard input) 10 OPEN 12,3 (Screen output) 10 OPEN 130,4 (Printer output) 10 OPEN 1,1,0,"NAME" (Read from cassette) 10 OPEN 1,1,1,"NAME" (Write to cassette) 10 OPEN 1,2,0,CHR$(10) (Open channel to RS-232 device) 10 OPEN 1,4,0,"STRING" (Send upper case/graphics to the printer) 10 OPEN 1,4,7,"STRING" (Send upper/lower case to the printer) 10 OPEN 1,5,7,"STRING" (Send upper/lower case to printer with device # 5) 10 OPEN 1,8,15,"COMMAND" (Send a command to disk)
OR
TYPE: Logical Operator FORMAT: OR
Action: Just as the relational operators can be used to make decisions regarding program flow, logical operators can connect two or more relations and return a true or false value which can then be used in a decision. When used in calculations, the logical OR gives you a bit result of I if the corresponding bit of either or both operands is 1. This will produce an integer as a result depending on the values of the operands. When used in comparisons the logical OR operator is also used to link two expressions into a single compound expression. If either of the expressions are true, the combined expression value is true (-1). In the first example below if AA is equal to BB OR if XX is 20, the expression is true.
Logical operators work by converting their operands to 16-bit, signed, two's complement integers in the range of -32768 to +32767. If the operands are not in the range an error message results. Each bit of the result is determined by the corresponding bits in the two operands.
EXAMPLES of OR Operator:
1 2 3 4
100 IF (AA=BB) OR (XX=20) THEN...
230 KK%=64 OR 32: PRINT KK%
(You typed this with a bit value of 000000 for 64 and 100000 for 32) (The computer responded with bit value 1100000. (96.))