# WRITING CODE FOR THE C64: 4- BEGINNING BASIC #2

One of the most important aspects to coding is understanding how to use the arithmetic operations available on the C64. It is capable of solving simple arithmetic and more complicate maths such as algebra and trigonometry, these functions are commonly used in game programming.

Arithmetic Symbols

There are four arithmetic symbols which you can use on your keyboard:

• This means Subtract
• / This means Divide
• * This means multiply

Lets look at how a simple addition statement works. Type in the following statement and press the <RETURN>key. You should see the following :

When you pressed the <RETURN> key, the computer executed the statement. It told you that the answer is 4 and displayed the prompt symbol.

Evaluating an ExpressionMore Terminology

In mathematics, a group of numbers and math symbols is called an expression. It states or expresses something. 2+2 is an expression. It is a numeric expression because it is created with numbers and mathematical symbols. It also has a numeric value. The value of the numeric expression of 2+2 is 4.

Finding the value of an expression is called evaluating the expression. If I say “How much is 2+2?” your reply will be “4”. I gave you an arithmetic expression and you evaluated it. In other words you gave the answer.

The difference between PRINT “” and PRINT

Lets consider the example of PRINT “something” and the above example which doesn’t use the quotation marks in the print statement. When using quotation marks, the Commodore 64 will print anything inside those marks. If you don’t use quotation marks it will attempt to evaluate an expression and print the result.

Some Homework

I’m not the one going to do all the work here, its time for you guys to do some homework. The best way to learn programming is to do it and work out what has happened. In this exercise I want you to use the various arithmetic symbols to see how they work.

Enter the Following:

PRINT 2+2

PRINT 5-1

PRINT 8/2

PRINT 2*2

As you do so, think through what results you expect before pressing the enter key.

Using ‘Program Mode’

In the previous lesson we discussed the difference between Immediate Mode and Program Mode. The following statements will demonstrate the key difference between these two modes. Take the following statements and execution.

In Immediate Mode as each statement is entered, it is executed immediately. The screen display doesn’t look very good because the statements remain on the screen along with the results. We can see that the BASIC prompt appears twice – after each statement is executed.

To put the computer into the program mode, all you have to do is begin a statement with a number, called the line number. When the BASIC interpreter ‘sees’ a statement from the keyboard that begins with the line number, it knows automatically that you are entering a program line. Instead of executing that statement immediately, it stores it in computer memory. This is stored in order to execute later.

Let’s look at the following example of Program Mode.

Notice in the above example that neither statement was executed. The numbered lines are now in computer memory. Congratulations, you have written a two line program.

The cursor is waiting below line 20, waiting for you to either make another statement or to ask it to execute the program. If you write a statement without a line number, this will once again place the Commodore 64 into Immediate mode. So be careful to ensure that you continue with the line numbering to stay in program mode.

The RUN Command

The BASIC word RUN causes the computer to execute the BASIC program in memory. It switches to program mode and then runs whatever program it finds in the memory. If there is no such program stored, a RUN statement has no result because there is nothing to execute.

When running a program, the computer executes each statement in the program starting with the lowest line number. It executes the statements in line-number order. Beginning with line 10, line 20 etc until the highest line number has been executed. Then it stops and displays the BASIC prompt symbol to show that it has finished running the program and is ready to do what you wish.

Enter the RUN command now.

We see that the earlier program entered in memory has now executed successfully starting with the code at line 10 of the program.

Key Advantages of the Program Mode

• If you want the computer to execute a series of statements and then display the result. It make sense to write a program.
• Programs can be stored on tape or disk and run repeatedly, anytime you need them. If you had to repeat an operation and had used Immediate Mode you would have to enter the program all over again.
• A Program can ‘remember‘ a long series of complicated statements.
• There are a few things (which we will cover in later lessons) which can only be done in a program. You can’t do them in immediate mode as they require the structure of a program.