RarityGuide.com - http://www.rarityguide.com/articles
Academy BASIC: 110 - The Missing President
http://www.rarityguide.com/articles/articles/31/1/Academy-BASIC-110---The-Missing-President/Page1.html
By chronodev (Ron)
Published on 11/5/2008
 
In this module you will learn about the LIST command and line numbering when writing Commodore BASIC programs.

Difficulty level: Freshman
Prerequisites: Academy BASIC: 104 - An Interactive BASIC Program
Outcome: Learn about LIST and line numbering.
Suggested time to complete this module: 30 minutes

In just a few modules, you will be writing your first game in Commodore 64 BASIC! Before we get to that though, there are a couple issues we have to learn about.

You might have observed that so far we have always written the line numbers in increments of 10 (i.e. 10,20,30...) and you might be asking why can't we use the normal count as line numbers? First line should be number 1, second line should be number 2, Third line should be number 3, and so on.

OK so let's do just that. Suppose, that you got an assignment to write a program that lists, in order, the first 10 presidents of the United States of America. And suppose you write the following program: (Type it into your console)

1 PRINT "GEORGE WASHINGTON"
2 PRINT "THOMAS JEFFERSON"
3 PRINT "JAMES MADISON"
4 PRINT "JAMES MONROE"
5 PRINT "JOHN QUINCY ADAMS"
6 PRINT "ANDREW JACKSON"
7 PRINT "MARTIN VAN BUREN"
8 PRINT "WILLIAM HENRY HARRISON"
9 PRINT "JOHN TYLER"
10 PRINT "JAMES K. POLK"

Easy, wasn't it? But wait a moment! We forgot to list John Adams, the second president of the United States! Oh boy, what can we do now? Well as things currently stand, you have to write the following line:

2 PRINT "JOHN ADAMS"

and then you have to write lines 3-10 AGAIN, with 3 being Thomas Jefferson, etc. That's pretty tedious isn't it?

But suppose we wrote the program with increments of 10, like we always do. Reset the console (ALT-R), and then type in the following:

10 PRINT "GEORGE WASHINGTON"
20 PRINT "THOMAS JEFFERSON"
30 PRINT "JAMES MADISON"
40 PRINT "JAMES MONROE"
50 PRINT "JOHN QUINCY ADAMS"
60 PRINT "ANDREW JACKSON"
70 PRINT "MARTIN VAN BUREN"
80 PRINT "WILLIAM HENRY HARRISON"
90 PRINT "JOHN TYLER"
100 PRINT "JAMES K. POLK"

Before I show you how easily we can correct the problem now, let's examine a new command which will come in handy when writing programs, especially long programs. Type this into the console:

LIST

We will get back to the LIST in just one moment. Your console should now look something like this:

Commodore BASIC screenshot

All you have to do to insert the missing president is to insert the new line with a number between 10 and 20. You do not have to re-write any of the other lines.

15 PRINT "JOHN ADAMS"

Finally let's delete the 11th president from our program. We delete a line by just typing the line number with nothing after it:

100

Now, let's LIST the program again:

C64 BASIC Program

Voila! Since we used increments of 10, it was easy to insert the missing line. This might not seem like a big deal with this small program, but if you have a program with hundreds or even thousands of lines of code, you don't want to have to rewrite everything.

Let's go back to LIST now. The LIST command displays whatever is in the BASIC RAM, which is usually your Commodore BASIC program. Observe (And try for yourself) the following uses for LIST:

LIST 
Shows all of the lines in your BASIC program

LIST 40
Shows only line 40

LIST 40-80
Shows lines 40 to 80

LIST 40-
Shows lines from 40 until the end of the program

LIST -40
Shows lines from beginning of program up to, and including, 40

That's it for this lesson. Make sure to visit the Forums if you want to discuss anything you learnt here.

Homework:
Write the command to display line number 50.
Write the command to display all lines up to 70.
Write the command to display lines 50 to 70.
Change the president program as follows: After each president, the program should print "and the next president was..."

Page copy protected against web site content infringement by Copyscape