[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP BASIC for OpenVMS
Reference Manual


Previous Contents Index

1.5.3 Explicitly Declared Variables

BASIC lets you explicitly assign a data type to a variable or an array. For example:


DECLARE DOUBLE Interest_rate

Data type keywords are described in Section 1.4. For more information about explicit declaration of variables, see the COMMON, DECLARE, DIMENSION, DEF, FUNCTION, EXTERNAL, MAP, and SUB statements in Chapter 3.

1.5.4 Subscripted Variables and Arrays

A subscripted variable references part of an array. Arrays can be of any valid data type. Subscripted variables and arrays follow the same naming conventions as unsubscripted variables. Subscripts follow the variable name in parentheses and define the variable's position in the array. When you create an array, you specify the maximum size of the array (the bounds) in parentheses following the array name.

In Example 1-2, the DECLARE statement sets the bounds of the arrayemp_name to 1000. Therefore, the maximum value for an emp_name subscript is 1000. The bounds of the array define the maximum value for a subscript of that array.

Example 1-2 Using the DECLARE Statement to Set Array Boundaries

DECLARE STRING emp_name(1000)
FOR I% = 0% TO 1000%
     INPUT "Employee name";emp_name(I%)
NEXT I%

Subscripts can be any positive LONG integer value between 0 and 2147483647.

An array is a set of data ordered in one or more dimensions. A one-dimensional array, like emp_name(1000), is called a list or vector. A two-dimensional array, like payroll_data(5,5), is called a matrix. An array of more than two dimensions, like big_array(15,9,2), is called a tensor.

As a default, BASIC arrays are always zero-based. The number of elements in any dimension includes element number zero. For example, the array emp_name contains 1001 elements because BASIC allocates element zero. Payroll_data(5,5) contains 36 elements because BASIC allocates row and column zero.

Often, however, applications call for arrays that are not zero-based. In BASIC, you can define arrays that are not zero-based by specifying a lower bound, as well as an upper bound, for the subscripts. In this way, you can create an array with arbitrary starting and ending points. For example, you might want to create array birth_rate that holds the annual birth rate statistics for the years 1950 to 1985:


DECLARE birth_rate(1950 TO 1985)

Lower bounds are not allowed with virtual arrays or arrays used in MAT statements. If a multidimensional array is declared with lower bounds specified for some dimensions and not others, zero will be used for those dimensions without lower bounds.

You can use the UBOUND and LBOUND functions to determine the upper and lower bounds of an array. For a description of these functions, see Chapter 3.

For all arrays except virtual arrays, the total number of array elements cannot exceed 2147483647. Note, however, that this is a theoretical value; the actual maximum size of an array that you can declare depends on the configuration of your system.

BASIC arrays can have up to 32 dimensions. You can specify the type of data the array contains with data type keywords. See Table 1-2 for a list of BASIC data types.

An element in a one-dimensional array has a variable name followed by one subscript in parentheses. You may optionally use a space between the array name and the subscript. For example:


A(6%)

B (6%)

C$ (6%)

A(6%) refers to the seventh item in this list:


A(0%)   A(1%)   A(2%)   A(3%)   A(4%)   A(5%)   A(6%)

An element in a two-dimensional array has two subscripts, in parentheses, following the variable name. The first subscript specifies the row number and the second subscript specifies the column number. Use a comma to separate the subscripts. You may optionally put a space between the array name and the subscripts. For example:


A (7%,2%)   A%(4%,6%)   A$ (10%,10%)

In Figure 1-1, the arrow points to the element specified by the subscripted variable A%(4%,6%).

Figure 1-1 Representation of the Subscript Variable A%(4%,6%)


Although a program can contain a variable and an array with the same name, this is poor programming practice. Variable A and the array A(3%,3%) are separate entities and are stored in completely separate locations, so it is a good idea to give them different names.

Note that a program cannot contain two arrays with the same name but a different number of subscripts. For example, the arrays A(3%) and A(3%,3%) are invalid in the same program.

BASIC arrays can be redimensioned at run time. See the HP BASIC for OpenVMS User Manual for more information about arrays.

1.5.5 Initialization of Variables

BASIC generally sets variables to zero or null values at the start of program execution. Variables initialized by BASIC include:

  • Numeric variables and in-storage array elements (except those in MAP or COMMON statements).
  • String variables (except those in MAP or COMMON statements).
  • Variables in subprograms. Subprogram variables are initialized to zero or the null string each time the subprogram is called.

BASIC does not initialize the following:

  • Virtual arrays
  • Variables in MAP and COMMON areas
  • Variables declared as EXTERNAL
  • Variables in routines that contain the option INACTIVE=SETUP

1.6 Constants

A constant is a numeric or character literal that does not change during program execution. A constant may optionally be named and associated with a data type. BASIC allows the following types of constants:

  • Numeric:
    • Floating-point
    • Integer
    • Packed decimal
  • String (ASCII characters enclosed in quotation marks)

A constant of any of the above data types can be named with the DECLARE CONSTANT statement. You can then refer to the constant by name in your program. See Section 1.6.3 for information about naming constants.

You can use the OPTION statement to declare a default data type for all constants in your program. This statement allows you to specify a data type for only the constants in your program; you can specify a different data type for variables. You can also use a special numeric literal notation to specify the value and data type of a numeric literal. Numeric literal notation is discussed in Section 1.6.4.

If you do not specify a data type for a numeric constant with the DECLARE CONSTANT statement or with numeric literal notation, the type and size of the constant is determined by the default REAL, INTEGER, or DECIMAL type set with the BASIC DCL command or the OPTION statement.

To simplify the representation of certain ASCII characters and mathematical values, BASIC also supplies some predefined constants.

The following sections discuss numeric and string constants, named constants, numeric literal notation, and predefined constants.

1.6.1 Numeric Constants

A numeric constant is a literal or named constant whose value never changes. In BASIC, a numeric constant can be a floating-point number, an integer, or a packed decimal number. The type and size of a numeric constant is determined by the following:

  • System default values
  • Defaults set by the qualifiers for the BASIC DCL command
  • Data type specified in a DECLARE CONSTANT or OPTION statement
  • Numeric literal notation

If you use a declarative statement to name and declare the data type of a numeric constant, the constant is of the type and size specified in the statement. For example:


DECLARE BYTE CONSTANT age = 12

This example associates the numeric literal 12 and the BYTE data type with the identifier age. To specify a data type for an unnamed numeric constant, you must use the numeric literal notation format described in Section 1.6.4.

1.6.1.1 Floating-Point Constants

A floating-point constant is a literal or named constant with one or more decimal digits, either positive or negative, with an optional decimal point and an optional exponent (E notation). If the default data type is integer, BASIC will treat the literal as an INTEGER unless it contains a decimal point or the character E. If the default data type is DECIMAL, an E is required or BASIC treats the literal as a packed decimal value.

Table 1-3 contains examples of floating-point literals with REAL, INTEGER, and DECIMAL default data types.

Table 1-3 Specifying Floating-Point Constants
REAL
Default Type
INTEGER
Default Type
DECIMAL
Default Type
-8.738 -8.738 -8.738E
239.21E-6 239.21E-6 239.21E-6
.79 .79 .79E
299 299E 299E

Very large and very small numbers can be represented in E (exponential) notation. To indicate E notation, a number must be followed by the letter E (or e). It also must be followed by an exponent sign and an exponent. The exponent sign indicates whether the exponent is positive or negative and is optional only if you are specifying a positive exponent. The exponent is an integer constant (the power of 10).

See Table 1-2 for decimal-place precision of floating-point keywords.

Table 1-4 compares numbers in standard and E notation.

Table 1-4 Numbers in E Notation
Standard Notation E Notation
.0000001 .1E-06
1,000,000 .1E+07
--10,000,000 --.1E+08
100,000,000 .1E+09
1,000,000,000,000 .1E+13

The range and precision of floating-point constants are determined by the current default data types or the explicit data type used in the DECLARE CONSTANT statement. However, there are limits to the range allowed for numeric data types. See Table 1-2 for a list of BASIC data types and ranges. BASIC signals the fatal error "Floating point error or overflow" (ERR=48) when your program attempts to specify a constant value outside of the allowable range for a floating-point data type.

1.6.1.2 Integer Constants

An integer constant is a literal or named constant, either positive or negative, with no fractional digits and an optional trailing percent sign (%). The percent sign is required for integer literals only if the default type is not INTEGER.

In Table 1-5, the values are all integer constants. The presence of the percent sign varies depending on the default data type.

Table 1-5 Specifying Integer Constants
INTEGER
Default Type
REAL or
DECIMAL
Default Type
81257 81257%
--3477 --3477%
79 79%

The range of allowable values for integer constants is determined by either the current default data type or the explicit data type used in the DECLARE CONSTANT statement. Table 1-2 lists BASIC data types and ranges. BASIC signals an error for a number outside the applicable range.

If you want BASIC to treat numeric literals as integer numbers, you must do one of the following:

  • Set the default data type to INTEGER.
  • Make sure the literal has a percent sign suffix.
  • Use explicit literal notation.

Note

You cannot use percent signs in integer constants that appear in DATA statements. Doing so causes BASIC to signal "Data format error" (ERR=50).

1.6.1.3 Packed Decimal Constants

A packed decimal constant is a number, either positive or negative, that has a specified number of digits and a specified decimal point position (scale). You specify the number of digits (d) and the position of the decimal point (s) when you declare the constant as a DECIMAL(d,s). If the constant is not declared, the number of digits and the position of the decimal is determined by the representation of the constant.

For example, when the default data type is DECIMAL, 1.234 is a DECIMAL(4,3) constant, regardless of the default decimal size. Likewise, using numeric literal notation, "1.234"P is a DECIMAL(4,3) constant, regardless of the default data type and default DECIMAL size. Numeric literal notation is described in Section 1.6.4.

1.6.2 String Constants

String constants are either string literals or named constants. A string literal is a series of characters enclosed in string delimiters. Valid string delimiters are as follows:

  • Double quotation marks ("text")
  • Single quotation marks ('text')

You can embed double quotation marks within single quotation marks ('this is a "text" string') and vice versa ("this is a 'text' string"). Note, however, that BASIC does not accept incorrectly paired quotation marks and that only the outer quotation marks must be paired. For example, the following character strings are valid:


"The record number does not exist."
"I'm here!"
"The terminating 'condition' is equal to 10."
"REPORT 543"

However, the following strings are not valid:


"Quotation marks that do not match'
"No closing quotation mark

Characters in string constants can be letters, numbers, spaces, tabs, 8-bit data characters, or the NUL character (ASCII code 0). If you need a string constant that contains a NUL, you should use CHR$(NUL). See Section 1.6.4 for information about explicit literal notation.

Note that NUL is a predefined integer constant. See Section 1.6.5.

The compiler determines the value of the string constant by scanning all its characters. For example, because of the number of spaces between the delimiters and the characters, these two string constants are not the same:


"    END-OF-FILE REACHED    "
"END-OF-FILE REACHED"

BASIC stores every character between delimiters exactly as you type it into the source program, including:

  • Lowercase letters (a to z)
  • Leading, trailing, and embedded spaces
  • Tabs
  • Special characters

The delimiting quotation marks are not printed when the program is executing. The value of the string constant does not include the delimiting quotation marks. For example:


PRINT "END-OF-FILE REACHED"

END

Output


END-OF-FILE REACHED

BASIC does, however, print double or single quotation marks when they are enclosed in a second paired set. For example:


PRINT 'FAILURE CONDITION: "RECORD LENGTH"'

END

Output


FAILURE CONDITION: "RECORD LENGTH"

1.6.3 Named Constants

BASIC allows you to name constants. You can assign a name to a constant that is either internal or external to your program and refer to the constant by name throughout the program. This naming feature is useful for the following reasons:

  • If a commonly used constant must be changed, you need to make only one change in your program.
  • A logically named constant makes your program easier to understand.

You can use named constants anywhere you can use a constant, for example, to specify the number of elements in an array.

You cannot change the value of an explicitly named constant during program execution.

1.6.3.1 Naming Constants Within a Program Unit

You name constants within a program unit with the DECLARE statement, as is shown in Example 1-3.

Example 1-3 Naming Constants Within a Program Unit

DECLARE DOUBLE CONSTANT preferred_rate = .147
DECLARE SINGLE CONSTANT normal_rate = .162
DECLARE DOUBLE CONSTANT risky_rate = .175
   .
   .
   .
new_bal = old_bal * (1 + preferred_rate)^years_payment

When interest rates change, only three lines have to be changed rather than every line that contains an interest rate constant.

Constant names must conform to the rules for naming internal, explicitly declared variables listed in Section 1.5.1.

The value associated with a named constant can be a compile-time expression as well as a literal value, as shown in Example 1-4.

Example 1-4 Associating Values with Named Constants

DECLARE STRING CONSTANT Congrats =            &
         "+--------------------+" + LF + CR + &
         "| Congratulations!   |" + CR + CR + &
         "+--------------------+"
   .
   .
   .
PRINT Congrats
   .
   .
   .
PRINT Congrats

Named constants can save you programming time because you do not have to retype the value every time you want to display it.

Valid operators in DECLARE CONSTANT expressions include string concatenations and all valid arithmetic, relational, and logical operators except exponentiation. You cannot use built-in functions in DECLARE CONSTANT expressions.

BASIC allows constants of all data types except RFA to be named constants. Because you cannot declare a constant of the RFA data type, you cannot name a constant of that type.

You can specify only one data type in a DECLARE CONSTANT statement. To declare a constant of a different data type, you must use a second DECLARE CONSTANT statement.

1.6.3.2 Naming Constants External to a Program Unit

To declare constants outside the program unit, use the EXTERNAL statement, as shown in Example 1-5.

Example 1-5 Declaring Constants Outside the Program Unit

EXTERNAL LONG CONSTANT SS$_NORMAL
EXTERNAL WORD CONSTANT IS_SUCCESS

The first line declares the OpenVMS status code SS$_NORMAL to be an external LONG constant. The second line declares IS_SUCCESS, a success code, to be an external WORD constant. Note that BASIC allows only external BYTE, WORD, LONG, QUAD, and SINGLE constants. The OpenVMS Linker supplies the values for the constants specified in EXTERNAL statements.

In BASIC, the named constant might be a system status code or a global constant declared in another OpenVMS layered product.

1.6.4 Explicit Literal Notation

You can specify the value and data type of numeric literals by using a special notation called explicit literal notation. The format of this notation is as follows:


[radix] "num-str-lit" [data-type]

Radix specifies an optional base, which can be any of the following:

D Decimal (base 10)
B Binary (base 2)
O Octal (base 8)
X Hexadecimal (base 16)
A ASCII

The BASIC default radix is decimal. Binary, octal, and hexadecimal notation allow you to set or clear individual bits in the representation of an integer. This feature is useful in forming conditional expressions and in using logical operations. The ASCII radix causes BASIC to translate a single ASCII character to its decimal equivalent. This decimal equivalent is an INTEGER value; you specify whether the INTEGER subtype should be BYTE, WORD, LONG, or QUAD.

Num-str-lit is a numeric string literal. It can be the digits 0 and 1 when the radix is binary, the digits 0 to 7 when the radix is octal, the digits 0 to F when the radix is hexadecimal, and the digits 0 to 9 when the radix is decimal. When the radix is ASCII, num-str-lit can be any valid ASCII character.

Data-type is an optional single letter that corresponds to one of the data type keywords that follow:

B BYTE
W WORD
L LONG
Q QUAD
F SINGLE
D DOUBLE
G GFLOAT
S SFLOAT
T TFLOAT
X XFLOAT
P DECIMAL
C CHARACTER

The following are examples of explicit literals:

D"255"L Specifies a LONG decimal constant with a value of 255
"4000"F Specifies a SINGLE decimal constant with a value of 4000
A"M"L Specifies a LONG integer constant with a value of 77
A"m"B Specifies a BYTE integer constant with a value of 109

A quoted numeric string alone, without a radix and a data type, is a string literal, not a numeric literal. For

"255" Is a string literal
"255"W Specifies a WORD decimal constant with a value of 255

If you specify a binary, octal, ASCII, or hexadecimal radix, data-type must be an integer. If you do not specify a data type, BASIC uses the default integer data type. For example:

B"11111111"B Specifies a BYTE binary constant with a value of -1
B"11111111"W Specifies a WORD binary constant with a value of 255
B"11111111" Specifies a binary constant of the default data type (BYTE, WORD, LONG, or QUAD)
B"11111111"F Is illegal because F is not an integer data type
X"FF"B Specifies a BYTE hexadecimal constant with a value of -1
X"FF"W Specifies a WORD hexadecimal constant with a value of 255
X"FF"D Is illegal because D is not an integer data type
O"377"B Specifies a BYTE octal constant with a value of -1
O"377"W Specifies a WORD octal constant with a value of 255
O"377"G Is illegal because G is not an integer data type


Previous Next Contents Index