For Retro Computing Enthusiasts

BASIC for Beginners: PRINT and INPUT

By Larry Cotton — originally published in December 1986 (Vol. 4, No. 12)

Before we get back to experimenting with the PRINT statement, let’s take a brief look at the NEW command. When you type NEW and press RETURN, you’re telling the computer, “Erase this program and reset your memory for another one.” Without this command, you’d have to turn off the computer to start working on a new program.

NEW should be typed only when you’re sure you want to erase the program in memory. If you’re not sure, but you still want to start a new program, just save the old one to tape or disk.

I also promised last month that we’d clear up a few aspects of the PRINT statement which were left hanging. The first concerns the way things are arranged on the screen. This depends on how PRINT statements are punctuated.

Punctuating PRINT

There are four ways to punctuate the end of a PRINT statement—with a comma, semicolon, a colon, or with no punctuation at all. First let’s start with no punctuation. Type this:

There are nine letters in each string. Run this program and observe the results. Next, add a comma right after the second quotation mark in all lines.

When you run it now, the letters are arranged on the screen in columns. The comma at the end of each line tells the computer to arrange whatever is in quotes into four columns of 10 characters each (except for the VIC-20, which will arrange the text into 11-character columns).

The computer insists on putting at least one space between columns. Thus, if there were ten characters inside the quotes (go ahead—try it), the computer would push the information over into the next column. Also try changing the information in the quotes to seven or eight characters.

Changing the comma to a semicolon yields entirely different results. The messages are printed continuously without spaces.

Let’s assign some string variables and print them. Type NEW, then this:

When you run this, line 30 breaks the strings into columns, while lines 40 and 50 print them together. In the case of string variables, a semicolon is not required between the variable names to print them continuously.

Now type NEW and this:

A and B are numeric, not string, variables. They can have a maximum of seven numerals each (eight for the VIC-20) in order to be printed in adjacent columns because Commodore BASIC puts a space (or a negative sign if the number is less than zero) before and a space (for separation) after each printed numeric variable, in addition to the space between columns. Line 30 demonstrates this. Try changing A and B to numbers with four or five numerals.

Even with a semicolon (line 40), two spaces are added between the values for A and B. In line 50, we attempt to print A and B without any punctuation, but the computer interprets AB as another variable entirely. Since its value hasn’t been assigned, the computer prints the number 0.

Numeric and string variables can be either one letter, two or more letters, or a combination of a letter and number. Examples of valid variables are D, D$, D4, D4$, DE, DE$, DEVO, and DEVO$.

The number can’t come first in a variable—4D and 4D$ don’t work. And if you use more than two characters, the computer sees only the first two. Thus DE, DEVO, and DEVICE are all the same to the computer.

On To INPUT

We’ll continue to use PRINT in our short BASIC programs, but for now, let’s concentrate on a BASIC statement which can be even more fun to use—INPUT.

INPUT is fun to use because it requires the intervention of a human in order to work. Type NEW and then enter this program:

Now run it. Line 10 clears the screen and sends the cursor home. Line 20 causes the computer to wait for the user to type something, as indicated by the incessantly blinking cursor. It’s waiting for the user to type something that the computer will define as a string—A$.

The string can consist of almost anything that’s printable—letters, symbols, most punctuation, even numbers, although they’re treated just like a string of letters; you can’t perform any math operations on them.

INPUT also requires one more thing before it moves on, and that’s a RETURN. After you press RETURN, line 30 prints the message just like you typed it.

If you include a comma or colon in your INPUT message, the computer sends out a message of its own—?EXTRA IGNORED—after the offending character.

You’re probably thinking, “Big deal. I type in a message and the computer prints it right out. What’s this useful for?”

Not much, superficially. But remember—we have assigned a string variable, A$. In the immediate mode—that is, now, without a line number—type PRINT A$ and press RETURN. Bingo! Your message is printed again. Any time you want your message displayed on the screen, whether in immediate mode or within a program, just type PRINT A$. To see another use for INPUT, type NEW, then this two-line program:

Suddenly the computer gets smart. INPUT features the ability to print a message while it waits for the user to type something. Also, INPUT includes a complimentary question mark, as we’ve seen, so it doesn’t have to be in your message. Remember to include the semicolon when you use INPUT like this—it’s mandatory.

Now run your program.

What INPUT always looks for is a variable. It may appear right after the word INPUT with no punctuation, as INPUTA$, or it may appear after a quotation mark and semicolon, as in line 10 above. The variable may be numeric or string. Add these two lines to the program:

Look carefully at the general construction of these two lines. When the computer encounters the variable A in line 60, it expects you to type a number instead of a letter (unlike string variables, which don’t care). In fact, if you do type a letter and press RETURN, the cryptic message ?REDO FROM START pops up. This is not a syntax error—it is Commodore’s inimitable way of telling you that the computer is expecting a number. If you see this message, all you have to do is type a number.

In line 70, IS (with a leading space) and YEARS OLD! are inside quotation marks—they’re printed literally. But variables A$ (your name) and A (your age) are outside the quotes; the computer searches its memory for values to spit out when it encounters these variables.

If you have any questions on this very common BASIC construction, study the subtle ways numeric and string variables are handled and the way punctuation is used.

The Elves’ List

The INPUT statement may ask for more than one value, in which case they may be numeric, string, or mixed. NEW the program in memory and type in this one:

In keeping with the Christmas spirit, what we have here is an Elves’ List, which demonstrates a number of points about PRINT and INPUT.

Line 10 clears the screen and sends the cursor home. The first message is printed in line 20. Line 30 prints a blank line for readability. Line 40 is our first multiple INPUT statement—it waits for the user to type in five strings (A$-E$); press RETURN for each.

Lines 60-80 repeat the format of 20-40, but with a different message and five more string variables, F$-J$. Time to clear the screen and home the cursor again in line 90. Line 100 prints the heading for the complete list.

Note especially the two commas between NAUGHTY and NICE; these put the headings in two columns, but the extra comma puts an extra blank column between the two.

Line 110 prints a blank line after the heading, then lines 120-160 print out the strings. Again, note the two commas between the string variables’ names; these make the name lists fall directly under the appropriate heading.

We’ve covered a lot of semi-tricky ways that INPUT and PRINT can work with each other, as well as with numeric and string variables. The only way to master these is to practice and experiment. Don’t be afraid to try things which you don’t fully understand. Analyze them and you will.

And Merry Christmas—we’ll see you again next month.

Companion disk Companion Disk (.d64)

Enjoyed this? It’s what we do every month.

The revived COMPUTE!’s Gazette brings the spirit of the golden era to today’s retro scene — Commodore, Atari, Amiga, and more, in full-color print and digital every month.