Compaq BASIC for OpenVMS
Alpha and VAX Systems
Reference Manual


Previous Contents Index

1.5.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 INTEGER DECIMAL
-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).

A number can be carried to a maximum of 6 decimal places for SINGLE precision, 16 decimal places for DOUBLE precision, 15 decimal places for GFLOAT precision, 33 places for HFLOAT precision, 6 places for SFLOAT, 15 places for TFLOAT, and 33 places for XFLOAT.

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

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

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

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.5.4 for information about explicit literal notation.

Note that NUL is a predefined integer constant. See Section 1.5.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:

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

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.5.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.4.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.5.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.5.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 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 (Alpha BASIC only)
F SINGLE
D DOUBLE
G GFLOAT
H HFLOAT (VAX BASIC only)
S SFLOAT (Alpha BASIC only)
T TFLOAT (Alpha BASIC only)
X XFLOAT (Alpha BASIC only)
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

When you specify a radix other than decimal, overflow checking is performed as if the numeric string were an unsigned integer. However, when this value is assigned to a variable or used in an expression, the compiler treats it as a signed integer.

In the following example, BASIC sets all 8 bits in storage location A. Because A is a BYTE integer, it has only 8 bits of storage. Because the 8-bit two's complement of 1 is 11111111, its value is -1. If the data type is W (WORD), BASIC sets the bits to 0000000011111111, and its value is 255.


DECLARE BYTE A 
A = B"11111111"B 
PRINT A 

Output


-1 

Note

In BASIC, D can appear in both the radix position and the data type position. D in the radix position specifies that the numeric string is treated as a decimal number (base 10). D in the data type position specifies that the value is treated as a double-precision, floating-point constant. P in the data type position specifies a packed decimal constant. For example:
"255"D Specifies a double-precision constant with a value of 255
"255.55"P Specifies a DECIMAL constant with a value of 255.55

You can use explicit literal notation to represent a single-character string in terms of its 8-bit ASCII value. For example:


The letter C is an abbreviation for CHARACTER. The value of the numeric string must be from 0 to 255. This feature lets you create your own compile-time string constants containing nonprinting characters.

The following example declares a string constant named control_g (ASCII decimal value 7). When BASIC executes the PRINT statement, the terminal bell sounds.


DECLARE STRING CONSTANT control_g = "7"C 
PRINT control_g 

1.5.5 Predefined Constants

Predefined constants are symbolic representations of either ASCII characters or mathematical values. They are also called compile-time constants because their value is known at compilation rather than at run time.

Predefined constants help you to:

Table 1-6 lists the predefined constants supplied by BASIC, their ASCII values, and their functions.

Table 1-6 Predefined Constants
Constant Decimal/
ASCII Value
Function
NUL 0 Integer value zero
BEL (Bell) 7 Sounds the terminal bell
BS (Backspace) 8 Moves the cursor one position to the left
HT (Horizontal Tab) 9 Moves the cursor to the next horizontal tab stop
LF (Line Feed) 10 Moves the cursor to the next line
VT (Vertical Tab) 11 Moves the cursor to the next vertical tab stop
FF (Form Feed) 12 Moves the cursor to the start of the next page
CR (Carriage Return) 13 Moves the 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

You can use predefined constants in many ways. The following example shows how to print and underline a word on a hardcopy display:


PRINT "NAME:" + BS + BS + BS + BS + BS + "_____" 
END 

Output


NAME:

The following example shows how to print and underline a word on a video display terminal:


PRINT ESC + "[4mNAME:" + ESC + "[0m" 
END 

Output


NAME:

Note that in the previous example, m must be lowercase.


Previous Next Contents Index