Commodore BASIC 7.0 offers improvements over BASIC 2.0 (Commodore 64) and is quite close to BASIC 4.0. For those who are familiar with BASIC 2.0, there are additional commands available that should make programming easier. Certain projects, such as programming sprites and sound, are handled much easier. Upgrading from v. 2 to v. 7 is quite nice. If you're just beginning, don't worry, it will make sense. Be sure to follow the examples. Experiement with different things. This is the best way to learn.
First is a '10' followed by 'SCNCLR'. This is the line the computer starts with (lowest number first). It tells the computer to clear the screen. The second line begins with '20'. Then the variable 'A' is assigned a 1. If you have any experience with algebra, this is similar. You tell the computer that the variable 'A' is equal to '1'. As long as you don't change it, reset, or turn off the computer, it will always have this value. Next we have line '30' and give 'B' the value of '2'. (Another way to look at this is; variable 'B' holds number '2')
Next we have our PRINT statement with a bunch of things following it. Remember that in a PRINT statement, text is placed in quotes. Notice also there is a space after 'EQUALS' followed by the closing quote.
Now, after the closing quote, there is a semi-colon followed by 'A+B'. If you recall the first example of adding 1+1. It adds 'A' (which is '1') to 'B' (which is '2') and prints it after the text 'A PLUS B EQUALS'. One thing to point out here. When tacking a number to the end of a PRINT statement using the semi-colon (;), the computer leaves a space for the sign. If A+B were to equal a negative number, then the computer would use this space to print the negative sign in front of the number (the computer doesn't print a + sign for positive, all numbers are assumed to be positive). If we were to tack on another piece of text using a string variable, we would put a space after 'EQUALS' or before the first letter in the text to be added. More details can be found in the Advanced Programming Section, or in the Programmers Reference Manual.
Finally, line '50' ends the program. It isn't required to have this statement at the end, but it is good programming etiquette to do so. You can also insert the 'END' statement in-between line numbers to force the computer to stop (handy tool for 'debugging').
The 'LIST' command is typically used in Direct Mode. After running the previous program, you won't be able to see the line numbers and commands. When you are at the prompt (flashing cursor), type in LIST and you will see the program. When your programs get larger, this command will be essential to finding what you are looking for. Here are some examples of how LIST works:
LISTLIST will start with the first line number and scroll through the whole program to the last line. LIST 40 will list only line 40. LIST 300-410 will list line numbers 300 through and including 410 only.
There is another type of variable which you can use to tell the computer to store and print. It is called a "String Variable" (remember our example earlier of printing 'ONE' to the screen?). It is usually a letter followed by the dollar sign (you can use words too):
A$, B$, COMMODORE$, NAME$, etc.The PRINT statement is not the only way to get letters on the screen. There are what we call "Character codes" that we can use to print letters, numbers, and other characters to the screen. To do this, we use the CHR$( ) function. Use this with the PRINT statement. Enter in the following example...
? CHR$(65) <RETURN>The computer responds by printing the letter 'A'. You can experiment with different numbers to see what happens. Also, if you would like to see a listing of what the numbers are, you can refer to the Commodore 128 Programmers Reference Manual p. 660-661, or access this list in the Programming Tools section.
If you have followed everything up to now, you have learned there are two ways (modes) to enter commands: Direct and Program. Typically, you'll use Program Mode more often. This way you can create sophisticated programs and be able to save your work to tape or disk. Also, you have learned about the PRINT, SCNCLR, and the END statements; you have been introduced to String variables (they end with a $), and numerical variables (as you become more adept at programming, these will become indespensible!).
REMThe next statement to learn is the REM statement. This is short for REMark and is used to add comments to a program. This can be real handy when you have entered a lot of code with sophisticated functions. Use REM to remind yourself (and inform others) how your code works. Here is an example:
10 REM
******************************
20 REM **
**
30 REM ** COMMENT PROGRAM BY:
**
40 REM **
COMMODOREMAN **
50 REM **
**
60 REM ** (C) 2009
**
70 REM **
**
80 REM ******************************
90 REM
100 SCNCLR
110 REM CLEAR SCREEN
120 INPUT A
130 REM GET A NUMBER FROM THE USER
140 INPUT B
150 REM GET ANOTHER NUMBER FROM THE USER
160 C=A+B
170 REM ADD A AND B AND PLACE IN C
180 PRINT C
190 REM PRINT THE RESULTS STORED IN VARIABLE
C TO THE SCREEN
200 INPUT"ADD TWO OTHER NUMBERS (Y/N)";A$
210 REM ASK IF THE USER WANTS TO ADD TWO NEW
NUMBERS
220 IF A$="Y" THEN 100
230 REM IF THE USER ENTERS A 'Y', THEN START
BACK AT LINE 100
240 IF A$="N" THEN 280
250 REM IF NOT, END THE PROGRAM
260 GOTO 220
270 REM IF ANYTHING ELSE IS ENTERED, GO BACK
TO LINE 220
280 REM THE NEXT LINE ENDS THE PROGRAM
290 END
Anything entered after REM is ignored by the computer, so you can do whatever you want. Notice the cute little heading using the stars? You could use other characters too--whatever looks best for you. REM statements can be very important too. Use them in your program often so you don't forget what you are doing. It is also nice to let other programmers know what you are doing too. One thing about programming is that there is typically more than one way to do things. Its up to the programmer to do it the way best for him/her (advanced programming may require the most efficient way--but we won't go into that here).
In large programs, REM statements might actually get in the way, so if you are planning an ambitious program--be aware that this may be a way to save some space. For now, we'll just stick to using them (they are so handy for learning!).
DSAVEIf you typed in the last example, we will use it to learn just how to save it to a disk (If you don't have it, retype it for this example). Be sure to have your disk drive connected, powered on, and have a disk inserted that you can save to.
Type in the following...You might be saying to yourself, "Why would I want to save a small program?". Sure, saving this little program may not important, but as your programs become larger and more sophisticated, it will be a time-saver!
As you write programs, there are times where you will want to save your current work. If you added more to this program and wanted to save it, you would have to use a different name or delete the old one from the disk. It's easier for now to just use the next higher number (1...2...3...4 etc) at the end of the file name. So the next time we save this program, the name would be 'TEST2'. You can always use your handy disk utility program to delete the old file(s) later.
To recall the program, type in the following:In this first part, a lot has actually been covered. There are basically two modes, Direct and Program. In direct mode, commands can be entered which are processed immediately after pressing <RETURN>. Program mode uses line numbers to assemble a program which can be saved to a tape or disk drive, or executed with the RUN command.
Math, printing characters on the screen, comments, and variables were also demonstrated using the following:Remember the comment program example? (see the description of the REM statement if you've forgotten) Specifically, we're after the INPUT statement. When used in a program, this statement allows us to get information from the user. We can specify what type of information in the programming code. We can then use this information to process and output the result we want (if it doesn't, we have to scrutinize the code to find out why). Here is an example. Type in the following program:
10 REM CLEAR THE SCREENThis is a simple program to get information and then use it for printing a personalized greeting. When we want to get data like names, we need to use what is called a 'String Variable'. Data for a string variable can be Alphanumeric-letters mixed with numbers and other characters. Another example would be if we wanted to get some sort of part number (instead of a name), that could be a combination of mixed letters and numbers.
As we see above, line 50 is the INPUT statement and says "get information from user -- data type is String (that's what the dollar sign means at the end of 'NA') and store it to String Variable 'NA$'. If we wanted only numbers, we would omit the question mark after 'NA?'. Here we used PRINT to prompt the user and INPUT to get the information. We could also use the INPUT statement this way...
INPUT"PLEASE ENTER YOUR FIRST NAME...";NA$
This will essentially do the same thing. So choose which way is best for you. Also, notice the semi-colon before NA$. This is required and lets the computer know that you want the input data to be stored in NA$.
Data TypesNow would be a good time to learn more about Data Types. When using variables to store data, we have three types: String, Integer, and Floating-point integer. We use string variables for data such as getting a name in the above example, or part numbers (many use letters-so we have to use string variables for this type), or other like data (there is another BASIC statement called 'DATA' that we can use to store permanent data in a program - more on this later). If you want to get numbers (e.g. age, mathematical numbers, number of Commodore computers you own, etc...) then use either the Integer or Floating-Point Integer variable. You may be asking yourself "What's the difference?". Integer variables take less memory to store. So if you don't need to use a decimal point, it is more efficient to just use the Integer variable.
Here is another program demonstrating the differences:This little program shows that a floating-point variable can store values with a decimal point. Use this if you will need this type of precision (e.g. math). Saving a floating-point value to an integer variable cuts everything from the decimal point to the right off and leaves only the whole number to be stored in the integer variable. If you make a huge program, it could make all the difference to use integer variables where you can as they take less storage space.