CHAPTER 3

INFORMATION INPUT AND OUTPUT (I/O)

This chapter teaches you how to put information into a program when written and while it is running, and how to get information out of the program.


THE INPUT STATEMENT


The INPUT statement requests information from you through the keyboard. When the Sorcerer executes an INPUT instruction, it prints a question mark (?) on the screen to tell you it is waiting for information. This question mark is called the INPUT prompt. The Sorcerer does not continue the program until you give it the data it wants. If you don't give it as much information as requested, the Sorcerer prints ?? and waits for more information. If you give it too much information, it returns the message EXTRA IGNORED and continues the program. If you give the wrong kind of data (a string instead of a number or an expression instead of a number) you get an SN error message (SYNTAX ERROR) and the program stops.

Here is the simplest form of the INPUT statement:

INPUT <list of variables>

The variables in the INPUT list must be separated by commas. (If you use only one variable it stands alone.)

Example:

10 REM ONE NUMBER
20 PRINT "GIVE ME A NUMBER"
30 INPUT A
40 PRINT
50 PRINT "YOUR NUMBER IS"
60 PRINT A

Now run this program:

Sorcerer's response:

GIVE ME A NUMBER
?

Now you can enter one number.

You type:

12345 RETURN

Sorcerer's response:

YOUR NUMBER IS
12345

Example:

10 REM SUM OF TWO NUMBERS
20 PRINT "WHAT ARE THE TWO NUMBERS?"
30 INPUT A,B
40 PRINT
50 PRINT "THE SUM IS"
60 PRINT A+B

RUN the program:

Sourcerer's response:

WHAT ARE THE TWO NUMBERS?
?

Next type two numbers separated by a comma:

You type:

3.5,7.5

Sorcerer response:

THE SUM IS
11

You can make the Sorcerer print a message in front of the INPUT prompt. Use this format for your instruction:

INPUT "<prompt string>";<variable list>

Replace lines 20 and 30 in the previous program with a new line 20:

20 INPUT "WHAT ARE THE TWO NUMBERS";A,B

Now when you run the program, the Sorcerer's response is"

WHAT ARE THE TWO NUMBERS?

Note that the Sorcerer prints the question mark for you (as the input prompt); if you put a question mark in the prompt string you will end up with two of them.


MORE ABOUT PRINT STATEMENTS


You can make the Sorcerer print several different things on the same line of the monitor screen. The instruction is:

PRINT <list of strings or numerical expressions>

You should separate the items in the list by commas (,) or semicolons (;).

Example:

27 PRINT "THE ANSWER IS"; 27.5*X, "NOT"; Y
42 PRINT "3+4"; 3+4


NOTE

You may not recognize that mysterious asterisk. In the world of computer languages the asterisk (*) is used in place of the multiplication sign (X) to avoid confusion with the letter X. You must always use the * for multiplication; placing two expressions next to each other, as in the mathematics with which you are probably familiar, does not imply multiplication in BASIC. You must say 27.5*X and (3+A)*(4X+B); using 27.5X or (3+A)(4X+B) produces unpredictable results.)


The Sorcerer divides each line of printing into fields 14 characters long. If you separate two items in the PRINT list by a comma, the Sorcerer prints the first item and moves to the next field before printing the second item. If you separate two items by a semicolon, the Sorcerer prints the second immediately after the first. (Note that if the item is numeric, one space is left for an implied + sign. If a number is followed by a string, a space follows the number.)

Example:

You type:

10 PRINT "A";"B","C"
20 PRINT "A","B";"C"
30 PRINT 1;2;3;"A";1,1;"1";"1";1

Sorcerer replies:

AB         C
A BC
1 2 3 A 1 1 11 1
READY
COLUMN NUMBERS: 01234567890123456789012345678901

Just so that you can see what's happening, we print the column numbers beneath the Sorcerer's reply. These do not appear on the screen. Now, for example, the first letter C prints in column 12 (the first column is column 0); the letter A in the next line prints in column 15, and the first 1 in the group of four ones prints in column 26.

If you put a comma or semicolon at the end of a PRINT list, the next PRINT instruction prints on the same line, either in the next 14-character field (if a comma is used) or right after the end of the first PRINT (if a semicolon).

Example:

10 PRINT "A";
20 PRINT "B",
30 PRINT "C"

Run this program and the Sorcerer prints:

AB           3
COLUMN NUMBERS: 01234567890123456789012345

The Sorcerer has a special tab function that works something like the tab key on a typewriter.

Example:

You type:

PRINT "ABC";TAB(10);"DEF"

Sorcerer replies:

ABC       DEF
COLUMN NUMBERS:
01234567890123456789012345

Note that the second string prints in position 10; TAB does not advance ten spaces.

Example:

You type:

PRINT TAB(5);"***"

Sorcerer replies:

     ***
COLUMN NUMBERS:
01234567890123456789012345

If you want the Sorcerer to space over to a certain position before printing an item in a PRINT list, use

TAB (<number of the position>)

in front of the item in the PRINT instruction. The positions are numbered 0 through 63, from left to right on the screen. The Sorcerer doesn't backspace on TAB -- if it has already gone past the space you call for, it simply ignores the TAB.

Example:

You type:

PRINT "0123456789";TAB(5);"ABC"

Sorcerer replies:

01234567890ABC

While Standard BASIC usually isn't too fussy about whether you use spaces or not, it definitely does not want you to use a space after the word TAB. You must use the form TAB(X) or you get a result you might not predict; the Sorcerer will think that TAB is a variable name.


DATA, READ AND RESTORE STATEMENTS


So far ybu have fed data into your programs with INPUT statements (which tell the Sorcerer to ask for information) and LET statements (which assign values to variables, one at a time). You can also write lists of data directly into your programs, and instruct the Sorcerer to read data from the lists.

A DATA statement looks like this:

DATA < list of constants >

The items in the list (if more than one) must be separated by commas.

Example:

25 DATA 27.5,3.2,-10

When the Sorcerer sees a DATA statement, it stores the number in memory. Ybu can retrieve these numbers with a READ statement:

READ <list of variables>

Again, the variables in the list (if more than one) must be separated by commas.

Example:

45 READ X,F,Y

When the Sorcerer sees a READ statement, it assigns values from the DATA lists (no matter where in the program these DATA lists are) to variables in the READ list. The assignments are made from left to right until the READ list is exhausted. If there are more numbers in the DATA list than variables in the READ list, then the next READ instruction begins at the first unassigned DATA item. (In one program, there may be more items in DATA lists than there are variables in READ lists. The Sorcerer ignores the extra DATA items. If there are more variables in the READ list than numbers in the DATA lists, you get an OD error message -- OUT OF DATA).

A single READ statement may assign numbers from several DATA lists, and one DATA list may supply values for several READ statements.

Example:

You type:

10 DATA 1,2
20 DATA 3,4
30 READ A,B,C,D
40 PRINT A,B,C,D
RUN

Sorcerer replies:

1        2        3        4

Example:

You type:

10 DATA 1,2,3,4
20 READ A,B,C
30 READ D
RUN

The Sorcerer makes these assignments:

A=1 B=2 C=3 D=4

The Sorcerer assigned variables A and B the values in the first DATA list, assigned C and D the values in the second DATA list, and printed the values of A, B, C, and D.

(Note that unless the previous program is RUN nothing happens; the Sorcerer makes no assignments. If you just entered the program and then typed, in direct mode, PRINT A;B;C;D, you would get this response, 0 0 0 0.)

Use the RESTORE statement to reset the DATA list. The RESTORE statement tells the Sorcerer that the next READ statement should start at the first item of the first DATA list, rather than at the next unaccessed item of DATA.

Example:

You type:

10 READ A,B,C
20 RESTORE
30 READ D,E
40 PRINT A;B;C;D;E
50 DATA 1,2,3,4,5
RUN

Sorcerer replies:

1 2 3 1 2

The last two DATA items (4, 5) are not used by the program. If you added this line:

35 READ F,G,H

the Sorcerer would make these assignments:

A=l B=2 C=3 D=1 E=2 F=3 G=4 H=5

By the way, DATA statements usually are put at the end of a program, for convenience, but may be placed anywhere. No matter where put in a program, Standard BASIC treats all DATA statements as if they were one statement. DATA statements separated by other statements are equivalent to one statement containing the same data; all that matters is the order. For example, the following two programs both produce the same results:

10 DATA 1,2
20 READ A,B
30 DATA 3,4
40 DATA 5
50 READ C
60 DATA 6
70 READ D,E,F
80 PRINT A,B,C,D,E,F

10 READ A,B,C,D,E,F
20 PRINT A,B,C,D,E,F
30 DATA 1,2,3,4,5,6

When you run either program, this is what you get:

1 2 3 4 5 6

You may also enter strings as data; the Sorcerer then reads the data using string variables.

(You will learn more about strings and string variables in Chapter 9. At the moment just be aware that a string is any group of alphanumeric characters within quotes, while a string variable is any variable that stands for or takes the place of a string. The name of a string variable always has $ as its last character.)

Example:

You type:

10 READ A$,B$,C$
20 DATA "ABC","ABC123","#"
30 PRINT A$,B$, C$
RUN

Sorcerer replies:

ABC ABC123 #


Table of Contents | Prev | Next

The Trailing Edge