Previous | Contents | Index |
BASIC uses the full ASCII character set, which includes the following:
See the Compaq BASIC for OpenVMS Alpha and VAX Systems Reference Manual for a complete list of the ASCII character set and character values.
The compiler does not distinguish between uppercase and lowercase letters, except for letters inside quotation marks (called string literals) or letters in a DATA statement. The compiler also does not process characters in a REM statement or comment field.
You can use nonprinting characters in your program---for example, in string literals and constants---but to do so you must do one of the following:
See Section 5.6 for more information about predefined constants. See
Chapter 11 for more information about the CHR$ function.
5.4 Program Documentation
Documenting a program is the process of putting explanatory text (comments) into your code to make the program more understandable. Program documentation does not affect the way a program executes. You can add comments throughout a program; however, programs that are neatly structured need fewer comments. You can clarify your code by doing the following:
A comment field starts with an exclamation point (!) and ends with another exclamation point or a carriage return. The following example contains both comments and program statements. Any text that follows an exclamation point is ignored.
PROGRAM sample !+ ! Require that all variables be declared !- OPTION TYPE = EXPLICIT !+ ! Set up error handler !- WHEN ERROR USE Error_routine !+ ! Declarations !- . . . END PROGRAM |
You can also mix comments and code on the same line. For example:
DECLARE & INTEGER & Print_page, ! Current page number & Print_line, ! Current line number & Print_column ! Current column number |
All text between the exclamation point and the carriage return is ignored, with one exception: the ampersand is still recognized. This is a continuation character that specifies that a single statement is being continued on the next line. Only spaces and tabs are valid between the ampersand and the carriage return.
Although you can also terminate a comment field with an exclamation point, this practice is not recommended. Any text that follows the second exclamation point is treated as part of your program code. |
Following are methods for creating variables and specifying data types:
With implicit data typing, BASIC creates and specifies a data type for a variable the first time you reference it in your program. With explicit data typing, you must use one of four declarative statements (see Section 5.5.2) to name and type your program values.
Following are the data types you can specify:
Within the INTEGER and REAL data types there are further subdivisions: BYTE, WORD, LONG, or QUAD for INTEGER and SINGLE, DOUBLE, GFLOAT, HFLOAT, SFLOAT, TFLOAT, or XFLOAT for REAL.1 Choosing one of these subtypes lets you control the following:
For more information about data types, see Chapter 8.
5.5.1 Implicit Data Typing
With implicit data typing, a data type for a variable is created and specified the first time you reference it. You specify the data type of the variable by a suffix on the variable name as follows:
The default data type is SINGLE; however, you can specify your own default at DCL command level, inside the VAX BASIC Environment, or with the OPTION statement in your program. For more information about establishing default data types, see Chapter 2 (VAX BASIC only) and Chapter 3 in this manual, and the OPTION statement in the Compaq BASIC for OpenVMS Alpha and VAX Systems Reference Manual.
The first time the variable is referenced, it creates a variable with that name and data type and allocates storage for that variable.
In the following example, two INTEGER variables are created, A% and B%. Even though the values assigned to these variables are REAL, the values are converted to INTEGER to match the data type specified for the variables. The sum of these two values is therefore 30, not 30.6, as it would be if the variables were named A and B.
A% = 10.1 B% = 20.5 PRINT A% + B% |
30 |
With explicit data typing, you use a declarative statement to name and specify a data type for your program values.
BASIC provides the following declarative statements. These statements create variables and allocate storage.
DECLARE
DIMENSION
COMMON
MAP
The statement you choose depends on the way in which you will use the variables:
All declarative statements associate a data type with a variable. For more information, see Chapter 8.
1 Alpha BASIC does not support HFLOAT. VAX BASIC does not support QUAD, SFLOAT, TFLOAT, or XFLOAT. |
A constant is a value that does not change during program execution. Constants can be either literals or named constants and can be of any data type except RFA. You can use the DECLARE CONSTANT statement to create named constants. Constants can be of the following types:
In addition, predefined constants are provided and are useful for the following:
Table 5-1 lists the predefined constants.
Constant | Decimal ASCII Value | Description |
---|---|---|
BEL (Bell) | 7 | Sounds the terminal bell |
BS (Backspace) | 8 | Moves cursor one position to the left |
HT (Horizontal Tab) | 9 | Moves cursor to the next horizontal tab stop |
LF (Line Feed) | 10 | Moves cursor to the next line |
VT (Vertical Tab) | 11 | Moves cursor to the next vertical tab stop |
FF (Form Feed) | 12 | Moves cursor to the start of the next page |
CR (Carriage Return) | 13 | Moves cursor to the beginning of the current line |
SO (Shift Out) | 14 | Shifts out for communications networking, screen formatting, and alternate graphics |
SI (Shift In) | 15 | Shifts in for communications networking, screen formatting, and alternate graphics |
ESC (Escape) | 27 | Marks the beginning of an escape sequence |
SP (Space) | 32 | Inserts one blank space in program output |
DEL (Delete) | 127 | Deletes the last character entered |
PI | None | Represents the number PI with the precision of the default floating-point data type |
These predefined constants simplify the task of using nonprinting characters in your programs. For example, the following statement causes a bell to sound on your terminal:
PRINT BEL |
You can also create your own predefined constants with the DECLARE CONSTANT statement.
For more information about constants, see Chapter 8 and the
Compaq BASIC for OpenVMS Alpha and VAX Systems Reference Manual.
5.7 Variables
A variable is a storage location that is referred to by a variable name. Variable values can change during program execution. Each named location can hold only one value at a time.
A variable name can have up to 31 characters. The name must begin with a letter; the remaining characters, if any, can be any combination of letters, digits, dollar signs ($), underscores (_), and periods (.).
Variables can be grouped in an orderly series (such as a list or table)
under a single name, called an array. You refer to a
single variable in an array by using one or more
subscripts that specify the variable's position in the
array. (See Section 5.7.5 for more information on arrays.)
5.7.1 Floating-Point Variables
A floating-point variable is a named location that stores a floating-point value. The storage space required to hold the value depends on the variable's REAL subtype. For example, each SINGLE floating-point variable requires 32 bits (4 bytes) of storage, while each DOUBLE floating-point variable requires 64 bits (8 bytes) of storage.
Note that if any integer value is assigned to a floating-point
variable, the value is converted to a floating-point number.
5.7.2 Integer Variables
An integer variable is a named location that stores a whole number. The storage space required to hold the value depends on the variable's INTEGER subtype. For example, each BYTE integer variable requires 8 bits (1 byte) of storage, while each LONG integer variable requires 32 bits (4 bytes) of storage.
If you assign a floating-point value to an integer variable, the fractional portion of the value is trunctated; it does not round to the nearest integer. In the following example, the value -5, not -6, is assigned to the integer variable.
B% = -5.7 |
Although the integer data types QUAD,2 LONG, WORD, and BYTE allow the minimum values -9223372036854775808, -2147483648, -32768, and -128, respectively, you cannot use these constants explicitly, because BASIC reports an integer overflow error while attempting to parse the literal constant. To use these values, you must use either radix notation, such as --"32768"L, or a constant expression. For example:
DECLARE WORD CONSTANT Word_const = -32767 - 1 |
A packed decimal (DECIMAL data type) variable is made up of several storage locations, the number of which depends on the declared size of the variable. However, a packed decimal variable is still referred to by a single variable name.
When you declare a packed decimal variable, you specify the total number of digits and the number of digits to the right of the decimal place that you want.
The following statement creates a packed decimal variable named My_decimal, which can contain up to 8 digits: 6 digits to the left of the decimal point and 2 digits to the right of the decimal point.
OPTION TYPE = EXPLICIT DECLARE DECIMAL (8,2) My_decimal |
Packed decimal numbers are most useful for dollars-and-cents
calculations.
5.7.4 String Variables
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 7 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 7.
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:
2 QUAD is not supported by VAX BASIC. |
5.8 Keywords and Reserved Words
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 Compaq BASIC for OpenVMS Alpha and VAX Systems 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.
5.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 Compaq BASIC for OpenVMS Alpha and VAX Systems Reference Manual.
5.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 6.
Previous | Next | Contents | Index |