HP OpenVMS Systems Documentation

Content starts here

User Manual

Previous Contents Index

Part 2
HP BASIC Programming Concepts

Part 2 explains HP BASIC programming concepts including input and output, arrays, data definition, program control, and functions.

Chapter 4
BASIC Concepts and Elements

A BASIC program is a series of instructions for the compiler. These instructions are built using the fundamental elements of BASIC. This chapter describes these elements or building blocks.

4.1 Line Numbers

BASIC gives you the option of developing programs with line numbers or without line numbers.

4.1.1 Programs with Line Numbers

If you use line numbers in your program, you must follow these rules:

  • A line number must be a unique integer from 1 to 32767. HP BASIC does not allow programs to have duplicate line numbers.
  • A line number can contain leading zeros; however, embedded spaces, tabs, and commas are invalid in line numbers.
  • There must be a line number on the first line of the program.
  • If a source file contains subprograms, then each subprogram must begin on a numbered line.

In a multiple-unit program with line numbers, any comments following an END, END SUB, or END FUNCTION statement become a part of the previous subprogram during compilation unless they begin on a numbered line. This is not the case in multiple-unit programs without line numbers.

Although line numbers are not required, you might want to use them on every line that can cause a run-time error, depending on the type of error handling you use. See Chapter 15 for more information about handling run-time errors.

4.1.2 Programs Without Line Numbers

If you do not use line numbers in your program, follow these rules:

  • Use a text editor to enter and edit the program.
  • No line numbers are allowed anywhere in the program module.
  • The ERL function is not allowed.
  • REM statements are not allowed.

In a multiple-unit program without line numbers, any comments following an END, END SUB, or END FUNCTION statement become a part of the next subprogram during compilation (unless there is no next subprogram). This is not the case in multiple-unit programs with line numbers.

You can avoid all of these restrictions by placing a line number on the first line of your program; no additional line numbers are required. The line number on the first program line causes the compiler to compile your program as a program with line numbers.

When you write a program with or without line numbers, you can begin your program statements in the first character position on a line.

To develop the following program, use a text editor, and observe the restrictions previously listed:

!This is a short program that does not contain any
!BASIC line numbers.
!This program must be entered using a text editor;
!it cannot be entered directly into the environment.
PRINT "This program converts kilogram weight to pounds"
INPUT "How many kilograms";A
!This is the conversion factor
B = A / 2.2
PRINT "For ";A;" kilograms, the pound weight is ";B


This program converts kilogram weight to pounds
How many kilograms? 11
For  11  kilograms, the pound weight is  5

You can use exclamation comment fields instead of REM statements to insert comments into programs without line numbers. An exclamation point in column 1 causes the HP BASIC compiler to ignore the rest of the line. You can also identify program statements in programs without line numbers by using labels.

4.1.3 Labels

A label is a 1- to 31-character identifier that you use to identify a block of statements. All label names must begin with a letter; the remaining characters, if any, can be any combination of letters, digits, dollar signs ($), underscores (_), or periods (.), but the final character cannot be a dollar sign.

Labels have the following advantages over line numbers:

  • Meaningful label names provide documentation.
  • You can use labels in programs with or without line numbers.

When you use a label to mark a program location, you must end the label with a colon (:). The colon is used to show that the label name is being defined instead of referenced. When you reference the label, do not include the colon.

In the following example, the label names end with colons when they mark a location, but the colons are not present when the labels are referenced:

OPTION TYPE = EXPLICIT        ! Require declarations
      IF A <> B
      IF B = C
          A = A + 1
          GOTO Outer_loop
          B = B + 1
          GOTO Inner_loop
        END IF
      END IF

Labels have no effect on the order in which program lines are executed; they are used to identify a statement or block of statements.

4.1.4 Continuation of Long Program Statements

If a program line is too long for one line of text, you can continue the program line by placing an ampersand (&) at the end of the line. Note that only spaces and tabs are valid between the ampersand and the carriage return.

A single statement that spans several text lines requires an ampersand at the end of each continued line. For example:

OPEN "SAMPLE.DAT" AS FILE #2%,       &
      SEQUENTIAL VARIABLE,           &
      RECORDSIZE 80%

In an IF...THEN...ELSE construction, ampersands (&) are not necessary. If a continuation line begins with THEN or ELSE, then no ampersand is necessary. Similarly, in a line following a THEN or an ELSE, there is no ampersand.

IF (A$ = B$)
   PRINT "The two values are equal"
   PRINT "The two values are different"

Several statements can be associated with a single program line. If there are several statements on one line, they must be separated by backslashes (\). For example:


Because all statements are on the same program line, any reference to this program line refers to all three statements.

4.2 Identifying Program Units

You can delimit a main program compilation unit with the PROGRAM and END PROGRAM statements. This allows you to identify a program with a name other than the file name. The program name must not duplicate the name of a SUB, FUNCTION, or PICTURE subprogram. For example:

PROGRAM Sort_out

If you include the PROGRAM statement in your program, the name you specify becomes the module name of the compiled source. This feature is useful when you use object libraries because the librarian stores modules by their module name rather than the file name. Similarly, module names are used by the OpenVMS Debugger and the OpenVMS Linker.

For more information about PROGRAM units, see Chapter 12.

4.3 BASIC Character Set

BASIC uses the full ASCII character set, which includes the following:

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

See the HP BASIC for OpenVMS 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:

  • Use a predefined constant such as ESC or DEL
  • Use the CHR$ function to specify an ASCII value

See Section 4.6 for more information about predefined constants. See Chapter 10 for more information about the CHR$ function.

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

  • Using meaningful variable names
  • Including sufficient white space
  • Indenting your program lines according to the structure of your code

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
!   Set up error handler
WHEN ERROR USE Error_routine
!   Declarations

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.

4.5 Declarations and Data Types

Following are methods for creating variables and specifying data types:

  • Implicit data typing
  • Explicit data typing

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 4.5.2) to name and type your program values.

Following are the data types you can specify:

  • Integer (INTEGER)
  • Floating-point (REAL)
  • String (STRING)
  • Packed Decimal (DECIMAL)
  • Record File Address (RFA)

Within the INTEGER and REAL data types there are further subdivisions: BYTE, WORD, LONG, or QUAD for INTEGER and SINGLE, DOUBLE, GFLOAT, SFLOAT, TFLOAT, or XFLOAT for REAL. Choosing one of these subtypes lets you control the following:

  • The amount of storage required for the value; its container size
  • The range and precision that the value can accept

For more information about data types, see Chapter 8.

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

  • A percent sign suffix (%) specifies the INTEGER data type.
  • A dollar sign suffix ($) specifies the STRING data type.
  • Any other ending character specifies a variable of the default data type.

The default data type is SINGLE on Alpha BASIC and SFLOAT on I64 BASIC. However, you can specify your own default at DCL command level or with the OPTION statement in your program. For more information about establishing default data types, see Chapter 2, as well as the OPTION statement in the HP BASIC for OpenVMS 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


4.5.2 Explicit Data Typing

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:


The statement you choose depends on the way in which you will use the variables:

  • DECLARE and DIMENSION allocate dynamic storage for variables; storage is allocated when the program executes.
  • COMMON and MAP statements allocate storage for variables statically; storage is allocated when the program is compiled.

All declarative statements associate a data type with a variable. For more information, see Chapter 7.

4.6 Constants

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:

  • Integer
  • Floating-point
  • Packed decimal
  • String

In addition, predefined constants are provided and are useful for the following:

  • Formatting program output to improve clarity
  • Making source code easier to understand
  • Using nonprinting characters without having to look up their ASCII values

Table 4-1 lists the predefined constants.

Table 4-1 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:


You can also create your own predefined constants with the DECLARE CONSTANT statement.

For more information about constants, see Chapter 7 and the HP BASIC for OpenVMS Reference Manual.

4.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 4.7.5 for more information on arrays.)

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

4.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, LONG, WORD, and BYTE allow the minimum values -9223372036854775808, -2147483648, -32768, and -128, respectively, you cannot use these constants explicitly, because HP 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

4.7.3 Packed Decimal Variables

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.


DECLARE DECIMAL (8,2) My_decimal

Packed decimal numbers are most useful for dollars-and-cents calculations.

Previous Next Contents Index