Previous | Contents | Index |
This chapter explains how to use BASIC statements to move data
to and from your program.
6.1 Program Input
BASIC programs receive data in the following ways:
The following sections describe how to use these statements in detail.
6.1.1 Providing Input Interactively
The INPUT, INPUT LINE, and LINPUT statements prompt a user for data
while the program runs.
6.1.1.1 INPUT Statement
The INPUT statement interactively prompts the user for data. You can use the optional prompt string to clarify the input request by specifying the type and number of data elements required by the program. This is especially useful when the program contains many variables, or when someone else is running your program. For example:
INPUT "PLEASE TYPE 3 INTEGERS" ;B% ,C% ,D% A% = B% + C% + D% PRINT "THEIR SUM IS"; A% END |
PLEASE TYPE 3 INTEGERS? 25,50,75 [Return] THEIR SUM IS 150 |
When your program runs, BASIC stops at each INPUT, LINPUT, or INPUT LINE statement, prints a string prompt, if specified, and an optional question mark (?)1 followed by a space; it then waits for your input. By using either a comma or semicolon, you can affect the format of your string prompt as follows:
See Section 6.2.1 for more information about print zones. For more information about formatting string prompts, see Section 6.1.1.3.
You must provide one value for each variable in the INPUT request. If you do not provide enough values, BASIC prompts you again. For example:
INPUT A,B END |
? 5 [Return] ? 6 [Return] |
BASIC interprets a carriage return (null input) as a zero value for numeric variables and as a null string for string variables. For example:
? 5 [Return] ? [Return] |
These responses assign the value 5 to variable A and zero to variable B. In contrast, if you provide more values than there are variables, BASIC ignores the excess.
In the following example, BASIC ignores the extra value (8). You can type multiple values if you separate them with commas. Because commas separate variables in the PRINT statement, BASIC prints each variable at the start of a print zone.
INPUT A,B,C PRINT A,B,C END |
? 5,6,7,8 [Return] 5 6 7 |
If you name a numeric variable in an INPUT statement, you must supply numeric data. If you supply string data to a numeric variable, BASIC signals "Illegal number" (ERR=52). If you supply a floating-point number for an integer variable, BASIC signals "Data format error" (ERR=50).
If you name a string variable in an INPUT statement, you can supply either numbers or letters, but BASIC treats the data you supply as a string. Because digits and a decimal point are valid text characters, numbers can be interpreted as strings. For example:
INPUT "Please type a number"; A$ PRINT A$ |
Please type a number? 25.5 25.5 |
BASIC interprets the response as a 4-character string instead of as a numeric value.
You can type strings with or without quotation marks. However, if you
want to input a string containing a comma, you should enclose the
string in quotation marks or use the INPUT LINE or LINPUT statement. If
you do not, BASIC treats the comma as a delimiter and assigns
only part of the string to the variable. If you use quotation marks, be
sure to type both beginning and ending marks. If you leave out the end
quotation mark, BASIC signals "Data format error"
(ERR=50).
6.1.1.2 INPUT LINE and LINPUT Statements
The INPUT LINE and LINPUT statements prompt you for string data while your program runs. You can respond with strings that contain commas, semicolons, and quotation marks, which are characters that the INPUT statement interprets as delimiters.
The INPUT LINE statement accepts and stores all characters, including quotation marks, semicolons, and commas, up to and including the line terminator or terminators. LINPUT accepts all characters up to, but not including, the line terminator or terminators.
In the following example, because both INPUT LINE and LINPUT treat your input as a string literal, BASIC interprets quotation marks, commas, and semicolons as characters, not as string delimiters. When A$ is input with the INPUT LINE statement, the carriage return line terminator is stored as part of the string. The first PRINT statement tells BASIC to print all three variables on one line, starting each one in a new print zone. However, when BASIC prints the three strings, it prints the carriage return character at the end of string A$; this terminates the current line and causes B$ to begin on a new line.
INPUT LINE A$ LINPUT B$ LINPUT C$ PRINT A$, B$, C$ PRINT "DONE" END |
? SINGLE, DOUBLE [Return] ? "GFLOAT" [Return] ? HFLOAT; REAL Data Types [Return] SINGLE, DOUBLE "GFLOAT" HFLOAT; REAL Data Types DONE |
The INPUT, INPUT LINE, and LINPUT statements can accept data from a
terminal or a terminal-format file. See Section 6.3 for information
about I/O to terminal-format files.
6.1.1.3 Enabling and Disabling the Question Mark Prompt
With the SET PROMPT statement, BASIC allows you to enable and disable the question mark prompt.
By default, BASIC displays the question mark prompt. The following example displays the default prompt string:
INPUT "Please input 3 integer values";A%, B%, C% |
Please input 3 integer values? |
You can, however, disable the question mark prompt by specifying the SET NO PROMPT statement.
SET NO PROMPT INPUT "Please input 3 integer values";A%, B%, C% |
Please input 3 integer values |
When you disable the question mark prompt, you can specify your own prompt at the end of each prompt string. The following example inserts a colon at the end of the prompt string:
SET NO PROMPT INPUT "Please enter your name: ";Employee_name$ |
Please enter your name: |
Now, if the SET PROMPT statement is specified, BASIC displays both the colon and a question mark.
SET PROMPT INPUT "Please enter your name: ";Employee_name$ |
Please enter your name: ? |
The SET [NO] PROMPT statement is valid for INPUT, LINPUT, INPUT LINE, and MAT INPUT statements. If the prompt is disabled, any one of the following commands reenables it:
The following sections describe the READ, DATA, and RESTORE statements. To use READ and DATA statements, you must know what data is required when writing the program. These statements do not stop to request data while the program runs; therefore, your program runs faster than with the INPUT statements.
The RESTORE statement lets you use the same data items more than once.
6.1.2.1 READ and DATA Statements
The READ statement reads values from a data block. A data pointer keeps track of the data read. Each time the READ statement requests data, BASIC retrieves the next available constant from a DATA statement. The DATA statement contains the values that the READ statement reads. In a DATA statement, integer constants are whole numbers; they cannot be followed by a percent sign. In the following example, BASIC signals an error because the integer constants in the DATA statement contain percent signs:
10 WHEN ERROR USE catch_it DATA 1%, 2%, 3% 20 READ A%, B%, C% END WHEN 400 HANDLER catch_it PRINT "ERROR NUMBER IS "; ERR PRINT "ERROR AT LINE "; ERL PRINT "ERROR MESSAGE IS "; ERT$(ERR) END HANDLER 500 END |
ERROR NUMBER IS 50 ERROR AT LINE 20 ERROR MESSAGE IS %Data format error |
A READ statement is not valid without at least one DATA statement. If your program contains a READ statement but no DATA statement, BASIC signals the compile-time error "READ without DATA".
READ statements can appear either before or after their corresponding DATA statements. The only restriction is that the DATA statements must be in the same order as their corresponding READ statements.
You can have more than one DATA statement in a program. DATA statements are ignored without at least one READ statement. You can use an ampersand to continue a DATA statement. For example:
10 DATA "ABRAMS", BAKER, CHRISTENSON, & DOBSON, "EISENSTADT", FOLEY |
Comment fields are not allowed in DATA statements. For example, the following statements cause A$ to contain the string "ABC !COMMENT":
READ A$ DATA ABC !COMMENT |
When you compile a program, BASIC creates one data block for each program unit. Each data block is local to the program or subprogram containing it; this means that you cannot share DATA statements between program modules.
The data block contains the values in all DATA statements in that program unit. These values are stored in line number order. Each time BASIC executes a READ statement, it retrieves the next value in the data block.
BASIC signals an error if you do one of the following:
BASIC ignores excess data in DATA statements.
The following example of READ and DATA mixes string and floating-point data types. The first READ statement reads the first data item in the program: "The circumference is". The second READ statement reads the second data item: 40.5.
DATA "The circumference is" DATA 40.5 READ text$ READ radius CIRCUMFERENCE = PI * radius * 2 PRINT text$; CIRCUMFERENCE END |
The circumference is 254.469 |
The RESTORE statement lets you read the same data more than once. It has no effect without READ and DATA statements.
RESTORE resets the data pointer to the beginning of the first DATA statement in the program unit. You can then read data values again. Consider the following program:
10 READ B,C,D 20 RESTORE 30 READ E,F,G 40 DATA 6,3,4,7,9,2 50 END |
The READ statement in line 10 reads the first three values in the DATA statement:
B=6
C=3
D=4
The RESTORE statement resets the pointer to the beginning of line 40. During the second READ statement (line 30), the first three values are read again:
E=6
F=3
G=4
Without the RESTORE statement, line 30 would assign the following values:
E=7
F=9
G=2
1 The SET NO PROMPT statement turns off the optional question mark; see Section 6.1.1.3. |
The PRINT statement displays data on your terminal during program execution. BASIC evaluates expressions before displaying results. You can also print and format data with the PRINT USING statement. For information about the PRINT USING statement, see Chapter 15.
When you use the PRINT statement, BASIC does the following:
When an element in a list is not a simple variable or constant, BASIC evaluates the expression before printing the value. For example:
A = 45 B = 55 PRINT A + B END |
100 |
However, BASIC interprets text inside quotation marks as a string literal.
A = 45 B = 55 PRINT "A + B" END |
A + B |
The PRINT statement without an expression prints a blank line.
PRINT "This example leaves a blank line" PRINT PRINT "between two lines." END |
This example leaves a blank line between two lines. |
< A terminal line contains zones that are 14 character positions wide. The number of zones in a line depends on the width of your terminal: a 72-character line contains 5 zones, which start in columns 1, 15, 29, 43, and 57. A 132-character line has additional print zones starting at columns 71, 85, 99, and 113.
The PRINT statement formats program output into these zones in different ways, depending on the character that separates the elements to be printed. If a comma precedes the PRINT item, BASIC prints the item at the beginning of the next print zone. If the last print zone on a line is filled, BASIC continues output at the first print zone on the next line. For example:
INPUT A ,B ,C ,D ,E ,F PRINT A ,B ,C ,D ,E ,F END |
? 5,10,15,20,25,30[Return] 5 10 15 20 25 30 |
BASIC skips one print zone for each extra comma between list elements. For example, the following program prints the value of A in the first zone and the value of B in the third zone:
A = 5 B = 10 PRINT "first zone",,"third zone" PRINT A,,B END |
first zone third zone 5 10 |
If you separate print elements with a semicolon, BASIC does not move to the next print zone. In the following example, the first PRINT statement prints two numbers. (Printed numbers are preceded by a space or a minus sign and followed by one space.) The second PRINT statement prints two strings.
PRINT 10; 20 PRINT "ABC"; "XYZ" END |
10 20 ABCXYZ |
Whether you use a comma or a semicolon at the end of the PRINT statement, the cursor remains at its current position until BASIC encounters another PRINT or INPUT statement. In the following example, BASIC prints the current values of X, Y, and Z on one line because a comma follows the last item in the line PRINT X, Y:
INPUT X,Y,Z PRINT X,Y, PRINT Z END |
? 5,10,15 5 10 15 |
The following example shows PRINT statements using a comma, a semicolon, and no formatting character after the last print item:
!No comma after I%, so each element !Prints on its own line ! PRINT I% FOR I% = 1% TO 10% PRINT ! !A comma follows J%, so each !element prints in a separate zone ! MARGIN 80% PRINT J%, FOR J% = 1% TO 10% PRINT ! !A semicolon follows K%, so print !elements are packed together ! PRINT K%; FOR K% = 1% TO 10% END |
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 |
Commas and semicolons also let you control the placement of string output. For example:
PRINT "first zone",,"third zone",,"fifth zone" END |
first zone third zone fifth zone |
The extra comma between strings causes BASIC to skip another print zone. In the following example, the first string is longer than the print zone. When the two strings are printed, the second string begins in the third print zone because that is the next available print zone after the first string is printed.
PRINT "abcdefghijklmnopqrstuvwxyz","pizza" PRINT "first zone","second zone","third zone" |
abcdefghijklmnopqrstuvwxyz pizza first zone second zone third zone |
BASIC prints strings exactly as you type them, with no leading or trailing spaces. It does not print quotation marks unless they are delimited by another matching pair. For example:
PRINT 'PRINTING "QUOTATION" MARKS' END |
PRINTING "QUOTATION" MARKS |
BASIC follows these rules for printing numbers:
BASIC follows these rules for printing floating-point numbers:
The PRINT statement displays only up to 6 digits of precision for floating-point numbers. This corresponds to the precision of the SINGLE data type. To display the extra digits in DOUBLE, GFLOAT, HFLOAT, TFLOAT, or XFLOAT numbers, you must use the PRINT USING statement. See Chapter 15 for more information about the PRINT USING statement.
The following example shows how BASIC prints various numbers with single precision:
FOR I = 1 TO 20 PRINT 2^(-I),I,2^I NEXT I END |
.5 1 2 .25 2 4 .125 3 8 .0625 4 16 .03125 5 32 .015625 6 64 .78125E-02 7 128 .390625E-02 8 256 .195313E-02 9 512 .976563E-03 10 1024 .488281E-03 11 2048 .244141E-03 12 4096 .12207E-03 13 8192 .610352E-04 14 16384 .305176E-04 15 32768 .152588E-04 16 65536 .767939E-05 17 131072 .38147E-05 18 262144 .190735E-05 19 524288 .953674E-06 20 .104858E+07 |
Previous | Next | Contents | Index |