Basic basics  --  tutorials



Part 1   Part 2   Part 3   Part 4


Part 1

INTRO

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.



GETTING STARTED

Be sure to have your computer and peripherals connected. (see users guide if you need help). Go ahead and turn on your computer.

After the initial screen has been displayed, the computer prompts you by way of the flashing cursor. We can start by typing in a command (or series of commands) that tells the computer to do something, such as loading a program, printing a character, performing math, etc. If we do not start out with a number, then the computer will take whatever we type in as a direct mode command and try to execute (run) it. Our first few experiments will be direct mode commands. Numbers preceeding other text will be taken as a program (more on this later).

Ready to learn?

FIRST STEPS

O.k., In this tutorial, text to be typed on the computer will be in  Courier font. Keys to be pressed (e.g. the RETURN key) will be in bold and surrounded by carats <>.

So, where does one begin when programming? It is typical in the programming community to start by printing something simple like "HELLO" on the screen. I am going to break with tradition here and start with simple mathmatics (everyone likes math, right?). One thing to remember, you can only learn programming in any language by using it. 

Simple addition:

Type in the following...

? 1+1 <RETURN>

The computer responds by adding the two numbers together and printing a "2" on the screen.

So what does the '?' mean? This is a shortcut for the 'PRINT' command in BASIC. (You can use this shortcut to print text on the screen too)
When typing simple commands on the screen, you will see this shortcut. When we start entering programs, we will be using PRINT.

Type in the next line...

? "TEXT" <RETURN>

Entering this in would result in the computer printing the word 'TEXT' on the screen.

Notice when printing text, the text must be enclosed in quote marks. You can also print more than one word using the print statement, just remember that an entry cannot have more than 80 characters (two lines if you are in 40 column mode or one line if you are in 80 column
mode).

There's a lot more you can do with PRINT and we will be getting into it later.

Now for some more math examples;

Subtraction...

?1-1 <RETURN>

Multiplication...

?2*3<RETURN>

Division...

?4/2 <RETURN>

Powers (use the up arrow key)...

?2^2 <RETURN>

Mixed...

?4/2-3+7*.02  <RETURN>

When you enter in the previous example, division and multiplication is performed first, addition and subtraction last. If you were intending 3+7 to be done first, you would need to surround them with parenthesis, like this...

?4/2-(3+7)*.02  <RETURN>

Here is a list of the order that is performed (if the problem had multiplication and division, it would be processed in order from left to right):

1.    ( )         parenthesis
2.    ^          exponentiation
3.    *  /       multiplication and division
4.     +  -     addition and subtraction
 

PRINT

Now let's try some more examples printing text...
You can use it with variables.
Try this...

A=1  <RETURN>

The computer responds by printing 'READY.' followed by the flashing cursor.

Now type in the following:

?A  <RETURN>

The computer then prints the value of 'A' to the screen. Because we told it that 'A' equals '1', it prints out a 1.

Try this out...

A$="ONE" <RETURN>

And type...

?A$  <RETURN>

The computer responds by printing the text 'ONE' on the screen!

These examples illustrate how variables (A) and string variables (A$) work. More on this just ahead.

Before we begin the next example, now is a good time to introduce program line numbers and the difference between direct mode and program mode.

Direct Mode

Direct Mode is simply doing what we have already done using the simple addition example, or by printing "TEXT" to the screen. After you pressed the <RETURN> key, the computer immediately processed the command.

Program Mode

Program Mode is where we start with a number, add a space, and then type in what we want the computer to do. After pressing <RETURN>, the computer saves the line to memory and waits for the next command. This could be the flashing cursor, or if you are using Auto Number you will get the next line number (See the command AUTO for details on how this feature works). The examples used here will typically be incremented by 10. This makes it easy to add a line in between if we need to. Later on, we can renumber our program using the BASIC 7.0 Direct Mode command 'RENUMBER'.

Here is an example of what a program generally looks like:

10 SCNCLR
20 A=1
30 B=2
40 PRINT "A PLUS B EQUALS ";A+B
50 END


Here is a description of how this program works:

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').


Type in RUN and press <RETURN>.

The computer sends the following to the screen:

A PLUS B EQUALS 3
READY.



The LIST Command...

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:

LIST
LIST 40
LIST 300-410

LIST 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.


More on variables...

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.

First, you would need to assign text to the string variable, like this (Be sure to type NEW and press <RETURN> first)... 10 A$="HERE IS SOME TEXT"

Then you would add a line to PRINT A$...

20 PRINT A$
30 END
This little program will print 'HERE IS SOME TEXT' to the screen!

Easy, right?   Right.

Remember that when you use String variables, the computer only recognizes the first two characters. So if you try the following:

10 COMMODORE$="AWESOME"
20 COMPUTER$="C128"
...


you will get "C128" when you try to use COMMODORE$!

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!).

REM

The 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!).

DSAVE

If 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...

DSAVE"SOME TEXT01",D0,U8 <RETURN>

This will save your program to the disk in drive 8. But what if you want to save it to another drive? Type in the following:

DSAVE"SOME TEXT",D0,U9  <RETURN>

This saves the program to drive 9(notice the 'U9'). If you want to save to drive 11, simply replace the 9 in 'U9' to 11.

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:

DLOAD"SOME TEXT",D0,U9 <RETURN> RUN  <RETURN>

If you wanted to save this program to a datasette, type in the following:

DSAVE"SOME TEXT01" <RETURN> SUMMARY - PART 1

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:

? or PRINT    -- to perform a calculation or print something on the screen
REM            -- add a comment or remark in a program
A=1              -- assign the number 1 to the letter 'A' (storage)
LIST             -- List all or part of a BASIC program stored in memory.
SCNCLR      -- clear the screen
IF                 --  This is used with THEN. IF a certain condition exists, THEN do something. If you place a number after THEN, the computer
                          automatically goes to that line number.
GOTO         --  go to the line number given
END           -- this is the end of the program
DSAVE       -- save a program to a tape or disk drive
DLOAD       -- load a program from a tape or disk drive

Terms:

DIRECT MODE        - Commands entered at the prompt and executed by the computer immediately after pressing <RETURN>.

PROGRAM MODE   - The computer executes a series of instructions which were typed in by the programmer. These instructions are preceeded by a number. These instructions can be loaded from media (tape, disk, etc...) into RAM and then executed by typing in 'RUN' and pressing <RETURN>.

DEBUGGING            - This is a term used to describe the activity of finding out why a program does not work like it is supposed to - finding errors.



Part 2

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 SCREEN
    20 SCNCLR
    30 REM DISPLAY INFORMATION YOU WANT TO GET FROM THE USER
    40 PRINT"PLEASE ENTER YOUR FIRST NAME"
    50 INPUT NA$
    60 REM  THE COMPUTER WILL PRINT A QUESTION MARK AND WAIT
    70 REM  FOR THE USER TO TYPE IN THE INFORMATION
    80 REM  AND PRESS THE <RETURN> KEY. THE INFORMATION IS THEN
    90 REM  STORED IN THE STRING VARIABLE 'NA$'.
    100 REM
    110 REM NOW WE CAN PROCESS THE DATA...
    120 SCNCLR
    130 PRINT"HELLO ";NA$
    140 END

This 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 Types

Now 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:

    10 REM  CLEAR SCREEN
    20 SCNCLR
    30 REM FLASH A TITLE TO IDENTIFY THE PROGRAM TO THE USER
    40 PRINT"MULTIPLY PROGRAM"
    50 REM GET THE USERS NAME USING INPUT AND STORE TO STRING VARIABLE NA$
    60 INPUT"NAME";NA$
    70 REM TELL USER WHAT THIS PROGRAM DOES
    80 PRINT"THIS PROGRAM MULTIPLIES TWO NUMBERS"
    90 REM GET THE FIRST NUMBER AND STORE TO INTEGER VARIABLE N1
    100 INPUT"FIRST NUMBER";N1
    110 REM GET THE NEXT NUMBER AND STORE TO INTEGER VARIABLE N2
    120 INPUT"SECOND NUMBER";N2
    130 REM PERFORM MULTIPLICATION AND STORE TO INTEGER VARIABLE N3
    140 N3=N1*N2
    150 REM PRINT THE PROBLEM AND RESULTS (N1+N2=N3)
    160 PRINT N1;"+";N2;"=";N3
    170 REM PROMPT USER IF WANTS TO MULTIPLY AGAIN
    180 INPUT"AGAIN";A$
    190 REM THE NEXT LINE HAS A NEW STATEMENT-REFER TO DESCRIPTION
    200 IF A$="Y" THEN 10
    210 REM IF USER DID NOT ENTER 'Y' THEN END THE PROGRAM
    220 END


Now we are getting to know how to make a useful program!

Here is a small program demonstrating Integer and Floating-Point variables:

    10 REM CLEAR SCREEN
    20 SCNCLR
    30 REM ASSIGN A VALUE TO FLOATING-POINT VARIABLE 'A'
    40 A=1.6
    50 REM ASSIGN A VALUE TO INTEGER VARIABLE 'B%'
    60 B%=A
    70 REM PRINT B% TO SEE HOW COMPUTER ASSIGNS A FLOAT VALUE TO AN INTEGER VARIABLE
    80 PRINT B%
    90 REM DONE. END THE PROGRAM.
    100 END

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.