HP OpenVMS Systems

Content starts here

Compaq BASIC for OpenVMS
Alpha and VAX Systems
Reference Manual

Previous Contents Index

Chapter 1
Program Elements and Structure

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:

  • Program lines and their components
  • The BASIC character set
  • BASIC data types
  • Variables and constants
  • Expressions
  • Program documentation

1.1 Components of Program Lines

A BASIC program is a series of program lines that contain instructions for the compiler.

All BASIC program lines can contain the following:

  • Line numbers or labels
  • Statements
  • Compiler directives
  • Comment fields
  • A line terminator (carriage return)

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 following sections. 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. Programs Without Line Numbers

BASIC searches for a line number on the first line of program text when you do the following:

  • Load a program into the VAX BASIC Environment with the OLD command.
  • Edit a program in the VAX BASIC Environment.
  • Compile a program from the DCL command line.

If no line number is found, then the following rules apply:

  • No line numbers are allowed in that program module.
  • References to the ERL function are not allowed.
  • A subroutine will signal the same errors as it would if it were compiled with the /NOLINES qualifier. If an error is resignaled back to the caller, ERL gives the line number of the calling site, rather than the line number of the actual error in the subprogram.
  • The REM statement is not allowed.

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 program lines.

1.1.2 Labels

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 (.).

A label name must be separated from the statement it identifies with a colon (:). For example:

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

You can reference a label almost anywhere you can reference a line number. However, there are the following exceptions:

  • You cannot compare a label with the value returned by the ERL function.
  • You cannot reference a label in an IF...THEN...ELSE statement without using the keyword GOTO or GO TO. You can use the implied GOTO form only to reference a line number. In Example 1-1, the GOTO keyword is not required in statement 100 because the reference is to a line number. However, the GOTO keyword is required in statement 200 because the references are to labels.

    Example 1-1 Referencing Label Names in BASIC Programs

    100 IF A% = B%
        THEN 1000 ELSE 1050
    200 IF A$ = "YES"

1.1.3 Statements

A BASIC statement generally consists of a statement keyword and optional operators and operands. For example, both of the following statements are valid:

LET A% = 534% + (SUM% - DIF%)

BASIC statements can be either executable or nonexecutable:

  • Executable statements perform operations (for example, PRINT, GOTO, and READ).
  • Nonexecutable statements describe the characteristics and arrangement of data, specify usage information, and serve as comments in the source program (for example, DATA, DECLARE, and REM).

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 text. Keywords

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:

  • Define data and user identifiers
  • Perform operations
  • Invoke built-in functions

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.

A BASIC keyword cannot be split across lines of text. There must be a space, tab, or special character such as a comma between the keyword and any other variable or operator.

Some keywords use two words, and some can be combined with other keywords. Their spacing requirements vary, as shown in Table 1-1.

Table 1-1 Keyword Space Requirements
Optional Space Required Space No Space
  MAT READ Single-Statement Lines and Continued Statements

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

You can continue any BASIC statement, but you cannot continue a string literal or BASIC keyword. The following example generates the error message "Unterminated string literal":


This example is valid:

PRINT "IF-";         &
      "THEN-";       &
      "ELSE-";       &
      "END-";        &
      "IF" Multistatement Lines

Multistatement lines contain several statements on one line of text or multiple statements on separate lines of text.

Multiple statements on one line of text must be separated by a backslash (\) character. For example:


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

BASIC also recognizes IF statement keywords on a new line of text and associates such keywords with the preceding IF statement. For example:

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"
     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 line numbers.

1.1.4 Compiler Directives

Compiler directives are instructions for the compiler. These instructions cause the compiler to perform certain operations as it compiles the program.

By including compiler directives in a program, you can do the following:

  • Place program titles and subtitles in the header that appears on each page of the listing file.
  • Place a program version identification string in both the listing file and object module.
  • Start or stop the inclusion of listing information for selected parts of a program.
  • Start or stop the inclusion of cross reference information for selected parts of a program.
  • Include BASIC code from another source file or a text library.
  • Conditionally compile parts of a program.
  • Terminate compilation.
  • Include CDD record definitions in a BASIC program.
  • Display messages during the compilation.

Follow these rules when using compiler directives:

  • Compiler directives must begin with a percent sign (%).
  • Compiler directives must be the only text on the line (except for%IF-%THEN-%ELSE-%END-%IF).
  • Compiler directives cannot appear within a quoted string.
  • Compiler directives can be preceded by an optional line number.

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.

1.2 BASIC Character Set

BASIC uses the full ASCII character set. This includes the following:

  • The letters A to Z, both uppercase and lowercase
  • The digits 0 to 9
  • Special characters

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:

  • Lowercase letters as lowercase
  • Nonprinting characters

The ASCII character NUL (ASCII code 0) and line terminators cannot appear in a string literal. Use the CHR$ function or explicit literal notation to use these characters and terminators.

You can use nonprinting characters in your program, for example, in string constants, but to do so you must use one of the following:

  • A predefined constant such as ESC or DEL
  • The CHR$ function to specify an ASCII value
  • Explicit literal notation

See Section 1.5.4 for more information about explicit literal notation.

1.3 BASIC Data Types

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
  • Floating-point
  • Character string
  • Packed decimal
  • Record file address

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:

  • RFA operations (the GETRFA function and the GET and FIND statements)
  • Assignments to other variables of the RFA data type
  • Comparisons with other variables of the RFA data type with the equal to (=) and not equal to (<>) relational operators
  • Formal and actual parameters
  • DEF and function results

You cannot declare a constant of the RFA data type, nor can you use RFA variables for any arithmetic operations.

The RFA data type requires 6 bytes of information. See the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual for more information about Record File Addresses and the RFA data type.

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.

Table 1-2 BASIC Data Types
Data Type Keyword Size Range Precision
BYTE 8 bits -128 to +127 3
WORD 16 bits -32768 to +32767 5
LONG 32 bits -2147483648 to
QUAD 64 bits -9223372036854775808 to
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
DECIMAL(d,s) 0 to 16
1 * 10 -31 to 1 * 10 31 d
per byte
Max = 65535 NA
RFA 6 bytes NA 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 specify data type defaults by doing the following:

  • Use SET and COMPILE commands in the VAX BASIC Environment (VAX only).
  • Use the DCL command BASIC at the DCL level.
  • Use the OPTION statement within the source program being compiled.

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.

Previous Next Contents Index