HP OpenVMS SystemsBASIC
Compaq BASIC for OpenVMS
This chapter discusses BASIC program elements and structure.
Compaq BASIC for OpenVMS Alpha systems (hereafter referred to as Alpha BASIC) does not support all features of Compaq BASIC for OpenVMS VAX (hereafter referred to as VAX BASIC). For a discussion of the differences between Alpha BASIC and VAX BASIC, see Appendix C.
The building blocks of a BASIC program are as follows:
A BASIC program is a series of program lines that contain instructions for the compiler.
All BASIC program lines can contain the following:
Only a line terminator is required in a program line. The other elements are optional.
A program line can contain any number of text lines. A text line cannot
exceed 255 characters.
1.1.1 Line Numbers
Line numbers are not required in programs; you can compile, link, and
execute a program with or without line numbers. There are, however,
different rules for writing programs with line numbers and for writing
programs without line numbers. These differences are described in the
220.127.116.11 Programs with Line Numbers
In VAX BASIC, if you are entering program lines directly into the VAX BASIC Environment in line mode, then only those statements with line numbers are allowed to start in the first column. Also, any programs entered in line mode must have an initial line number associated with the first program line.
A line number must be a unique integer from 1 through 32767, and must
be terminated by a space or tab. Leading spaces, tabs, and zeros in
line numbers are ignored. Embedded spaces, tabs, and commas cause
BASIC to signal an error.
18.104.22.168 Programs Without Line Numbers
BASIC searches for a line number on the first line of program text when you do the following:
If your program contains multiple units, the point at which BASIC breaks each program unit is determined by the placement of the statement that terminates each program unit. Any text that follows the program terminator becomes associated with the the following program unit. A program terminator can be END, END PROGRAM, END FUNCTION, END SUB, or END PICTURE1.
In VAX BASIC, you cannot use the APPEND command in the VAX BASIC Environment, or a plus sign (+) at DCL level, to concatenate programs without line numbers.
Note that when you compile a program from DCL, or when you copy a program into the VAX BASIC Environment with the OLD command, program statements can begin in the first column.
Instead of line numbers, you can use labels to identify and reference
A label is a 1- to 31-character name that identifies a statement or block of statements. The label name must begin with a letter; the remaining characters, if any, can be any combination of letters, digits, dollar signs ($), underscores (_), or periods (.).
Yes_routine: PRINT "Your answer is YES."
The colon is not part of the label name; it informs BASIC that the label is being defined rather than referenced. Consequently, the colon is not allowed when you use a label to reference a statement. For example:
200 GOTO Yes_routine
|Example 1-1 Referencing Label Names in BASIC Programs|
100 IF A% = B% THEN 1000 ELSE 1050 200 IF A$ = "YES" THEN GOTO Yes ELSE GOTO No
LET A% = 534% + (SUM% - DIF%) PRINT A%
BASIC statements can be either executable or nonexecutable:
BASIC can accept and process one statement on a line of text,
several statements on a line of text, multiple statements on multiple
lines of text, and single statements continued over several lines of
Every BASIC statement except LET1 and empty statements must begin with a keyword. Most keywords are reserved in the BASIC language. The keywords are listed in Appendix B, and the unreserved keywords are footnoted. Keywords are used to do the following:
Reserved keywords cannot be used as user identifiers, such as variable names, labels, or names for MAP or COMMON areas. Reserved keywords cannot be used in any context other than as BASIC keywords. The assignment STRING$ = "YES", for example, is invalid because STRING$ is a reserved BASIC keyword and, therefore, cannot be used as a variable. See Appendix B for a list of all the BASIC keywords.
Some keywords use two words, and some can be combined with other keywords. Their spacing requirements vary, as shown in Table 1-1.
|Optional Space||Required Space||No Space|
|GO TO||BY DESC||FNEND|
|GO SUB||BY REF||FNEXIT|
|ON ERROR||BY VALUE||FUNCTIONEND|
A single-statement line consists of one statement on one text line, or one statement continued over two or more text lines. For example:
30 PRINT B * C / 12
This single-statement line has a line number, the keyword (PRINT), the operators (*, /), and the operands (B, C, 12).
You can have a single statement span several text lines by typing an ampersand (&) and pressing the Return key. Note that only spaces or tabs are valid between the ampersand and the carriage return. For example:
OPEN "SAMPLE.DAT" AS FILE 2%, & SEQUENTIAL VARIABLE, & MAP ABC
The ampersand continuation character may be used but is not required for continued REM statements. The following example is valid:
REM This is a remark And this is also a remark
PRINT "IF-THEN-ELSE- & END-IF"
This example is valid:
PRINT "IF-"; & "THEN-"; & "ELSE-"; & "END-"; & "IF"
22.214.171.124 Multistatement Lines
Multistatement lines contain several statements on one line of text or
multiple statements on separate lines of text.
40 PRINT A \ PRINT V \ PRINT G
You can also write a multistatement program line that associates all statements with a single line number by placing each statement on a separate line. BASIC assumes that such an unnumbered line of text is either a new statement or an IF statement clause.
In the following example, each line of text begins with a BASIC statement and each statement is associated with line number 400:
400 PRINT A PRINT B PRINT "FINISHED"
100 REM Determine if the user's response was YES or NO. 200 IF (A$ = "YES") OR (A$ = "Y") THEN PRINT "You typed YES" ELSE PRINT "You typed NO" STOP END IF
You can use any BASIC statement in a multistatement line. Because the
compiler ignores all text following a REM keyword until it reaches a
new line number, a REM statement must be the last statement on a
multistatement line. REM statements are disallowed in programs without
1.1.4 Compiler Directives
By including compiler directives in a program, you can do the following:
Follow these rules when using compiler directives:
For more information about compiler directives, see Chapter 2 and the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual.
1 END PICTURE is for VAX BASIC only.
1 The LET keyword is optional.
Appendix A lists the full ASCII character set and character values.
The compiler does not distinguish between uppercase and lowercase letters except in string literals or within a DATA statement. The compiler does not process characters in REM statements or comment fields, nor does it process nonprinting characters unless they are part of a string literal.
In string literals, BASIC processes:
You can use nonprinting characters in your program, for example, in string constants, but to do so you must use one of the following:
Each unit of data in a BASIC program has a specific data type that determines how that unit of data is to be interpreted and manipulated by the compiler. This data type also determines how many storage bits make up the unit of data.
BASIC recognizes the following primary data types:
Integer data is stored as binary values in a byte, word, longword, or quadword. These values correspond to the BASIC data type keywords BYTE, WORD, LONG, and QUAD; these are all subtypes of the type INTEGER. (VAX BASIC does not support QUAD.)
Floating-point values are stored using a signed exponent and a binary fraction. BASIC allows the floating-point formats F_floating, D_floating, G_floating, H_floating, S_floating, T_floating, and X_floating. These formats correspond to the BASIC data type keywords SINGLE, DOUBLE, GFLOAT, HFLOAT, SFLOAT, TFLOAT, and XFLOAT.1 These are all subtypes of the type REAL. (See Section 1.3.3.)
Character data consists of strings of bytes containing ASCII code as binary data. The first character in the string is stored in the first byte, the second character is stored in the second byte, and so on. BASIC allows up to 65,535 characters for a STRING data element.
For the DECIMAL(d,s) data type, you can specify the total number of digits (d) in the data type and the number of digits to the right of the decimal point (s). For example, DECIMAL(10,3) specifies decimal data with a total of 10 digits, 3 of which are to the right of the decimal point.
BASIC also recognizes a special RFA data type to provide information about a record's file address. An RFA uniquely specifies a record in a file: you can access RMS files of any organization by a record's file address. By specifying the address of a record, RMS retrieves the record at that address. Accessing records by RFA is more efficient and faster than other forms of random record access. The RFA data type can only be used for the following:
You cannot declare a constant of the RFA data type, nor can you use RFA variables for any arithmetic operations.
BASIC packed decimal data is stored in a string of bytes. See the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual for more information about the storage of packed decimal data.
Table 1-2 lists BASIC data type keywords and summarizes BASIC data types.
|Data Type Keyword||Size||Range||Precision
|BYTE||8 bits||-128 to +127||3|
|WORD||16 bits||-32768 to +32767||5|
|SINGLE||32 bits||0.29E-38 to 1.70E38||6|
|DOUBLE||64 bits||0.29E-38 to 1.70E38||16|
|GFLOAT||64 bits||0.56E-308 to 0.90E308||15|
|HFLOAT||128 bits||0.84E-4932 to 0.59E4932||33|
|SFLOAT||32 bits||1.18E-38 to 3.40E38||6|
|TFLOAT||64 bits||2.23E-308 to 1.80E308||15|
|XFLOAT||128 bits||6.48E-4966 to 1.19E4932||33|
0 to 16
|1 * 10 -31 to 1 * 10 31||d|
|Max = 65535||NA|
In Table 1-2, REAL and INTEGER are generic data type keywords that specify floating-point and integer storage, respectively. If you use the REAL or INTEGER keywords to type data, the actual data type used (SINGLE, DOUBLE, GFLOAT, HFLOAT, SFLOAT, TFLOAT, XFLOAT, BYTE, WORD, LONG, or QUAD) depends on the current default.
You can also specify whether program values are to be typed implicitly or explicitly. The following sections discuss data type defaults and implicit and explicit data typing.
1 Alpha BASIC does not support the HFLOAT data type. VAX BASIC does not support the SFLOAT, TFLOAT, and XFLOAT data types.