[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP BASIC for OpenVMS
|
Previous | Contents | Index |
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 |
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:
|
You can use explicit literal notation to represent a single-character string in terms of its 8-bit ASCII value:
[radix] "num-str-lit" C |
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 |
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.
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.
1.7 Expressions
BASIC expressions consist of operands (constants, variables, and functions) separated by arithmetic, string, relational, and logical operators.
The following are types of BASIC expressions:
BASIC evaluates expressions according to operator precedence and uses the results in program execution. Parentheses can be used to group operands and operators, thus controlling the order of evaluation.
The following sections explain the types of expressions you can create
and the way BASIC evaluates expressions.
1.7.1 Numeric Expressions
Numeric expressions consist of floating-point, integer, or packed decimal operands separated by arithmetic operators and optionally grouped by parentheses. Table 1-7 shows how numeric operators work in numeric expressions.
Operator | Example | Use |
---|---|---|
+ | A + B | Add B to A |
-- | A---B | Subtract B from A |
* | A * B | Multiply A by B |
/ | A / B | Divide A by B |
^ | A^B | Raise A to the power B |
** | A**B | Raise A to the power B |
In general, two arithmetic operators cannot occur consecutively in the same expression. Exceptions are the unary plus and unary minus. The following expressions are valid:
A * + B A * - B A * (-B) A * + - + - B |
The following expression is not valid:
A - * B |
An operation on two numeric operands of the same data type yields a result of that type. For example:
A% + B% | Yields an integer value of the default type |
G3 * M5 | Yields a floating-point value if the default type is REAL |
If the result of the operation exceeds the range of the data type, BASIC signals an overflow error message.
The following example causes BASIC to signal the error "Integer error or overflow" because the sum of A and B (254) exceeds the range of -128 to +127 for BYTE integers. Similar overflow errors occur for REAL and DECIMAL data types whenever the result of a numeric operation is outside the range of the corresponding data type.
DECLARE BYTE A, B A = 127 B = 127 PRINT A + B END |
It is possible to assign a value of one data type to a variable of a different data type. When this occurs, the data type of the variable overrides the data type of the assigned value. The following example assigns the value 32 to the integer variable A% even though the floating-point value of the expression is 32.13:
A% = 5.1 * 6.3 |
When an expression contains operands with different data types, the data type of the result is determined by BASIC data type promotion rules:
Note that BASIC performs sign extension when converting BYTE, WORD, and LONG integers to a higher INTEGER data type (WORD, LONG, or QUAD). The high order bit (the sign bit) determines how the additional bits are set when the BYTE, WORD, or LONG is converted to WORD, LONG, or QUAD. If the high order bit is zero (positive), all higher-order bits in the converted integer are set to zero. If the high order bit is 1 (negative), all higher-order bits in the converted integer are set to 1.
The table below shows the data type of the result of an operation that combines arguments of differing data types. BASIC first promotes, if necessary, the arguments to the result data type, and then performs the operation.
Result Data Types in Expressions
BYTE WORD LONG QUAD SINGLE DOUBLE GFLOAT SFLOAT TFLOAT XFLOAT BYTE BYTE WORD LONG QUAD SINGLE DOUBLE GFLOAT SFLOAT TFLOAT XFLOAT WORD WORD WORD LONG QUAD SINGLE DOUBLE GFLOAT SFLOAT TFLOAT XFLOAT LONG LONG LONG LONG QUAD SINGLE DOUBLE GFLOAT TFLOAT TFLOAT XFLOAT QUAD QUAD QUAD QUAD QUAD GFLOAT GFLOAT GFLOAT TFLOAT TFLOAT XFLOAT SINGLE SINGLE SINGLE SINGLE GFLOAT SINGLE DOUBLE GFLOAT TFLOAT TFLOAT XFLOAT DOUBLE DOUBLE DOUBLE DOUBLE GFLOAT DOUBLE DOUBLE GFLOAT TFLOAT TFLOAT XFLOAT GFLOAT GFLOAT GFLOAT GFLOAT GFLOAT GFLOAT GFLOAT GFLOAT GFLOAT TFLOAT XFLOAT SFLOAT SFLOAT SFLOAT TFLOAT TFLOAT TFLOAT TFLOAT GFLOAT SFLOAT TFLOAT XFLOAT TFLOAT TFLOAT TFLOAT TFLOAT TFLOAT TFLOAT TFLOAT TFLOAT TFLOAT TFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT XFLOAT |
BASIC allows the DECIMAL(d,s) data type. The number of digits (d) and the scale or position of the decimal point (s) in the result of DECIMAL operations depends on the data type of the other operand. If one operand is DECIMAL and the other is DECIMAL or INTEGER, the d and s values of the result are determined as follows:
DECLARE DECIMAL(5,2) A DECLARE DECIMAL(4,3) B |
Note that only INTEGER data types are converted to the DECIMAL data type. If one operand is DECIMAL and one is floating-point, the DECIMAL value is converted to a floating-point value. The total number of digits in (d) in the DECIMAL value determines its new data type, as shown in Table 1-8.
If one argument is DECIMAL data type and one is a floating point data type, the DECIMAL data type argument is first converted to a floating point data type as follows in Table 1-8.
Number of DECIMAL Digits |
Floating-Point Operands | |||||
---|---|---|---|---|---|---|
in Operand | SINGLE | DOUBLE | GFLOAT | SFLOAT | TFLOAT | XFLOAT |
1-6 | SINGLE | DOUBLE | GFLOAT | SFLOAT | TFLOAT | XFLOAT |
7-15 | DOUBLE | DOUBLE | GFLOAT | TFLOAT | TFLOAT | XFLOAT |
16 | DOUBLE | DOUBLE | GFLOAT | XFLOAT | XFLOAT | XFLOAT |
17-31 | GFLOAT | GFLOAT | GFLOAT | XFLOAT | XFLOAT | XFLOAT |
GFLOAT maintains up to 15 digits of precision. Mixing DECIMAL items containing 16 or more bits with GFLOAT items may cause a loss of precision.
Operations performed on DOUBLE operands are performed in GFLOAT. When
the operation is complete, the GFLOAT result is converted to DOUBLE.
Therefore, it is possible to lose three binary digits of precision in
arithmetic operations using DOUBLE.
1.7.2 String Expressions
String expressions are string entities separated by a plus sign (+). When used in a string expression, the plus sign concatenates strings. For example:
INPUT "Type two words to be combined";A$, B$ C$ = A$ + B$ PRINT C$ END |
Output
Type two words to be combined? long ? word longword |
Conditional expressions can be either relational or logical expressions. Numeric relational expressions compare numeric operands to determine whether the expression is true or false. String relational expressions compare string operands to determine which string expression occurs first in the ASCII collating sequence.
Logical expressions contain integer operands and logical operators.
BASIC determines whether the specified logical expression is true or
false by testing the numeric result of the expression. Note that in
conditional expressions, as in any numeric expression, when BYTE, WORD,
and LONG operands are compared to WORD, LONG, and QUAD, the specified
operation is performed in the higher data type, and the result returned
is also of the higher data type. When one of the operands is a negative
value, this conversion will produce accurate but perhaps confusing
results, because BASIC performs a sign extension when converting BYTE
and WORD integers to a higher integer data type. See Section 1.7.1.1 for
information about integer conversion rules.
1.7.3.1 Numeric Relational Expressions
Operators in numeric relational expressions compare the values of two operands and return either -1 if the relation is true (as shown in Example 1), or zero if the relation is false (as shown in Example 2). The data type of the result is the default integer type.
A = 10 B = 15 X% = (A <> B) IF X% = -1% THEN PRINT 'Relationship is true' ELSE PRINT 'Relationship is false' END IF |
Output
Relationship is true |
A = 10 B = 15 X% = A = B IF X% = -1% THEN PRINT 'Relationship is true' ELSE PRINT 'Relationship is false' END IF |
Output
Relationship is false |
Table 1-9 shows how relational operators work in numeric relational expressions.
Operator | Example | Meaning |
---|---|---|
= | A = B | A is equal to B. |
< | A < B | A is less than B. |
> | A > B | A is greater than B. |
<= or =< | A <= B | A is less than or equal to B. |
>= or => | A >= B | A is greater than or equal to B. |
<> or >< | A <> B | A is not equal to B. |
== | A == B | A and B will PRINT the same if they are equal to six significant digits. However, if one value prints in explicit notation and the other value prints in E format notation, the relation will always be false. |
Operators in string relational expressions determine how BASIC compares strings. BASIC determines the value of each character in the string by converting it to its ASCII value. ASCII values are listed in Appendix A. BASIC compares the strings character by character, left to right, until it finds a difference in ASCII value.
In the following example, BASIC compares A$ and B$ character by character. The strings are identical up to the third character. Because the ASCII value of Z (90) is greater than the ASCII value of C (67), A$ is less than B$. BASIC evaluates the expression A$ < B$ as true (--1) and prints "ABC comes before ABZ".
A$ = 'ABC' B$ = 'ABZ' IF A$ < B$ THEN PRINT 'ABC comes before ABZ' ELSE IF A$ == B$ THEN PRINT 'The strings are identical' ELSE IF A$ > B$ THEN PRINT 'ABC comes after ABZ' ELSE PRINT 'Strings are equal but not identical' END IF END IF END IF END |
If two strings of differing lengths are identical up to the last character in the shorter string, BASIC pads the shorter string with spaces (ASCII value 32) to generate strings of equal length, unless the operator is the double equal sign (==). If the operator is the double equal sign, BASIC does not pad the shorter string.
In the following example, BASIC compares "ABCDE" to "ABC " to determine which string comes first in the collating sequence. "ABC " appears before "ABCDE" because the ASCII value for space (32) is lower than the ASCII value of D (68). Then BASIC compares "ABC " with "ABC" using the double equal sign and determines that the strings do not match exactly without padding. The third comparison uses the single equal sign. BASIC pads "ABC" with spaces and determines that the two strings match with padding.
A$ = 'ABCDE' B$ = 'ABC' PRINT 'B$ comes before A$' IF B$ < A$ PRINT 'A$ comes before B$' IF A$ < B$ C$ = 'ABC ' IF B$ == C$ THEN PRINT 'B$ exactly matches C$' ELSE PRINT 'B$ does not exactly match C$' END IF IF B$ = C$ THEN PRINT 'B$ matches C$ with padding' ELSE PRINT 'B$ does not match C$' END IF |
Output
B$ comes before A$ B$ does not exactly match C$ B$ matches C$ with padding |
Table 1-10 shows how relational operators work in string relational expressions.
Operator | Example | Meaning |
---|---|---|
= | A$ = B$ | Strings A$ and B$ are equal after the shorter string has been padded with spaces to equal the length of the longer string. |
< | A$ < B$ | String A$ occurs before string B$ in ASCII sequence. |
> | A$ > B$ | String A$ occurs after string B$ in ASCII sequence. |
<= or =< | A$ <= B$ | String A$ is equal to or precedes string B$ in ASCII sequence. |
>= or => | A$ >= B$ | String A$ is equal to or follows string B$ in ASCII sequence. |
<> or >< | A$ <> B$ | String A$ is not equal to string B$. |
== | A$ == B$ | Strings A$ and B$ are identical in composition and length, without padding. |
Previous | Next | Contents | Index |