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.

Note

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:

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:

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.

1.1.1.1 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.

1.1.1.2 Programs Without Line Numbers

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

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

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:

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%) 
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 text.

1.1.3.1 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:

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
GO TO BY DESC FNEND
GO SUB BY REF FNEXIT
ON ERROR BY VALUE FUNCTIONEND
  END DEF FUNCTIONEXIT
  END FUNCTION NOECHO
  END GROUP NOMARGIN
  END IF SUBEND
  END PROGRAM SUBEXIT
  END RECORD  
  END SELECT  
  END SUB  
  EXIT DEF  
  EXIT FUNCTION  
  EXIT SUB  
  INPUT LINE  
  MAP DYNAMIC  
  MAT INPUT  
  MAT LINPUT  
  MAT PRINT  
  MAT READ  

1.1.3.2 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":


PRINT "IF-THEN-ELSE- & 
       END-IF" 

This example is valid:


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

1.1.3.3 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:


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" 

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" 
     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 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:

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.

Note

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:

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:

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:

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

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
(Decimal
Digits)
Integer      
BYTE 8 bits -128 to +127 3
WORD 16 bits -32768 to +32767 5
LONG 32 bits -2147483648 to
+2147483647
10
QUAD 64 bits -9223372036854775808 to
+9223372036854775807
19
Real      
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      
DECIMAL(d,s) 0 to 16
bytes
1 * 10 -31 to 1 * 10 31 d
String      
STRING One
character
per byte
Max = 65535 NA
RFA      
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:

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.

Note

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