[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP BASIC for OpenVMS
|
Previous | Contents | Index |
Unlike some of the numeric variables described so far, a string variable does not correspond to a single location in memory because a string variable is more likely to exceed a single location in memory. Therefore, the value of a string variable can be contained in any number of memory locations. However, a string variable is still referred to by a single name. For example:
DECLARE STRING Employee_name |
A subscripted variable is a floating-point, integer, packed decimal, RFA, or string variable that is part of an array. Chapter 6 describes arrays in more detail.
An array is a set of data organized in one or more dimensions. A one-dimensional array is called a list or vector. A two-dimensional array is called a matrix. Arrays can have up to 32 dimensions.
When you create an array, its size is determined by the number of dimensions and the maximum size, called the bound, of each dimension. Subscripts begin by default with 0, not 1. That is, when calculating the number of elements in a dimension, you count from zero to the bound specified.
The following DECLARE statement creates an 11 by 11 array of integers. Therefore, the array contains a total of 121 array elements.
DECLARE INTEGER My_array (10, 10) |
There are many applications where you need to reference data for a particular range of values. You can specify a lower bound other than zero for your arrays. The following example declares an array containing the birth rates for the years from 1945 to 1985:
OPTION TYPE = EXPLICIT, & SIZE = REAL SINGLE DECLARE REAL Birth_rates(1945 TO 1985) |
Subscripts define the position of an element in an array; the expression Birth_rates(1970) refers to the 26th value of the array Birth_rates. For more information about arrays, see Chapter 6.
By default, the compiler signals an error if a subscript is larger than the allowable range. Also, the amount of storage that the system can allocate depends on available memory. Therefore, very large arrays can cause an internal allocation error. |
BASIC sets variables to zero or null values at the start of program execution. Variables initialized include the following:
Keywords are elements of the BASIC language. Keywords that are not reserved can be used as user identifiers such as labels, variable or constant names, or names of MAP or COMMON areas. Depending upon the location of the keyword in your program statement, the compiler will treat it as either a keyword or a user identifier. Your programs use keywords and reserved words to:
See the HP BASIC for OpenVMS Reference Manual for a list of keywords and reserved words.
Keywords determine whether the statement is executable or nonexecutable. Executable statements such as PRINT, GOTO, and READ perform operations. Nonexecutable statements such as DATA, DECLARE, and REM describe the characteristics and arrangement of data, usage information, and comments.
Every statement except LET must begin with a keyword. A keyword cannot have embedded spaces or be split across lines of text. There must be a space or tab between the keyword and any other variables or operators.
There are also phrases of keywords. In this case, the spacing
requirements vary.
4.9 Operands, Operators, and Expressions
An operand contains a value. An operand can be a scalar, subscripted variable, named constant, literal, and so on. An operator specifies a procedure to be carried out by one or more operands. An expression consists of operands separated by operators.
The following are types of operators:
Arithmetic
String
Relational
Logical
When combined with operands, these operators can produce:
For more information about operands, operators, and expressions, see
the HP BASIC for OpenVMS Reference Manual.
4.10 Assignment Statements
The following statements assign values to variables:
LET and INPUT statements allow you to assign values to any type of variable, while LINPUT and INPUT LINE allow you to assign values to string variables. For example:
LET A = 1.25 |
LET is an optional keyword. You can assign a value to more than one variable at a time, although this is not recommended. Instead, use a separate assignment statement each time you assign a value to a variable.
Whenever you assign a value to a numeric variable, BASIC converts the value to the data type of the variable. If you assign a floating-point value to an integer variable, BASIC truncates the value at the decimal point. If you assign an integer value to a floating-point variable, BASIC converts the value to floating-point format.
You can also assign values to variables with the DATA and READ statements; however, this method requires that you know all input data values while you are coding your program.
The INPUT, LINPUT, and INPUT LINE statements all assign values in the context of data being read into the program. These statements are discussed in Chapter 5.
This chapter explains how to use BASIC statements to move data
to and from your program.
5.1 Program Input
BASIC programs receive data in the following ways:
The following sections describe how to use these statements in detail.
5.1.1 Providing Input Interactively
The INPUT, INPUT LINE, and LINPUT statements prompt a user for data
while the program runs.
5.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 5.2.1 for more information about print zones. For more information about formatting string prompts, see Section 5.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).
5.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 5.3 for information
about I/O to terminal-format files.
5.1.1.3 Enabling and Disabling the Question Mark Prompt
With the SET PROMPT statement, HP BASIC allows you to enable and disable the question mark prompt.
By default, HP 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.
5.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 5.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 14.
When you use the PRINT statement, HP 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. |
Previous | Next | Contents | Index |