RarityGuide.com - http://www.rarityguide.com/articles
Academy BASIC: 104 - An Interactive BASIC Program
http://www.rarityguide.com/articles/articles/28/1/Academy-BASIC-104---An-Interactive-BASIC-Program/Page1.html
By chronodev (Ron)
Published on 11/2/2008
 
In this module you will be writing your first interactive commodore BASIC program and you will learn on how programs store information in variables.

Difficulty level: Freshman
Prerequisites: Academy BASIC: 103 - Writing Your First BASIC Program
Outcome: Learn about variables and user input.
Suggested time to complete this module: 40 minutes

The previous program you wrote was not very interactive. It always displayed the same message and there was no way to interact with the computer and have it do something else depending on what you tell it. Well now you are going to write your first interactive program!

Launch the console. If you already have it open, you can reset it by going to file->reset->soft or by pressing ALT-R. Reset the console whenever you want to clear whatever you have written and start over.

Remember that on the Commodore 64 keyboard the double quotes are above the "2" key so you need to press SHIFT-2 to get those. Also, the semi colon is where the single quote is. You do not need to hold down shift when typing to get capital letters. All letters are upper case by default. If you do hold down the shift, you will get some graphic symbols. This might be confusing at first but you will get used to it as you advance in the modules, and if you ever get the chance to program on a real Commodore 64 computer, adjusting to the new keyboard will be a breeze.

So now let's get to work. Type in the following lines of code: (Observe that the symbol in lines 20 and 30 is the dollar sign, not the letter 'S')

10 PRINT "WHAT IS YOUR NAME?"
20 INPUT A$
30 PRINT "HELLO "; A$
RUN


Congratulations! You have just written your first interactive program! When you run it, the computer will ask you for your name. After you type in your name, the computer will greet you with a personalized hello message. Your console should look similar to this:

Commodre BASIC interactive program

Let's go over the program line by line to understand how it works.

The first line,

10 PRINT "WHAT IS YOUR NAME?"

Just prints a message on the screen. You should already be familiar with the PRINT statement from the previous module in which you wrote your first program.

The second line has some new code:

20 INPUT A$

The INPUT statement instructs the computer that it needs to wait for user input. The program will not continue until it receives said input. The User in this case is you, but later on when you write games and other programs and have your family or friends try them, the User could be somebody else.

OK so what is this A$ ? This is a very important part of the lesson. A$ is a Variable. a variable is a place in the computer's memory in which you store information. You will be using variables everywhere in your programs. If you have experience with programming in any programming language, then you have already used variables a lot. If you are new to programming, here is an example to help you understand what variables are.

Let's say somoeone you met gives you his or her phone number on a piece of paper. You plan on calling that person later, so now you just take that note and place it in your right pocket. Later that day, you want to call that person and you take the note out of your pocket to get the phone number. Variables are like pockets. You put information in them, and once you do that, you can use that information whenever you want. A$ is in this case your right pocket, but you have more pockets. For example you can put some more information in B$ which is your left pocket. There can be many variables. Variable names in Commodore BASIC consist of 1 or 2 characters. Here are some examples of variables: G$, K$, AV$, XX$. The $ sign tells the computer we want to store a string, such as a word or a sentence. If we wanted to store a number, we would omit the $ sign. For example G, K, AV, XX.

The information is stored in memory and is lost as soon as you reset or exit the console.

In your program, you store your name in the A$ variable. In the next line, you will be retrieving this information:

30 PRINT "HELLO "; A$

This statement prints HELLO followed by the content of variable A$. This is where you take the "note" out of your pocket and use the information there. The semi colon is for formatting purposes, result should be the same if you take it out.

Make sure you understand this, as variables are a major foundation in any programming language. Let's add a couple of more lines of code to our program. We will now ask for a number. Type the following into your console:

40 PRINT "LUCKY NUMBER?"
50 INPUT B
60 PRINT "YOUR LUCKY NUMBER IS "; B


Expanding the interactive BASIC program

Go over lines 40-60. You should now understand what each line does. Note that we have B as a variable name without the $ sign. This is because B is a number. If we wanted to ask for a word we would've used B$ instead.

Congratulations! You have now learned how to use variables. This knowledge will help you in any programming languages you learn. And you will be putting this knowledge to good use in just a few modules where you will be writing your first game!

As always, if anything is not clear, head on over to the Forums where you can ask questions, discuss what you've learned, and share programs you've written.

Homework:
Write a program which asks users for their name, city, country, favorite color, favorite number, favorite video game, and favorite hobby. The program then displays that information back to the user.

Page copy protected against web site content infringement by Copyscape