HP OpenVMS Systems Documentation

Content starts here

HP BASIC for OpenVMS
Reference Manual


Previous Contents Index

1.7.3.3 Logical Expressions

A logical expression can have one of the following formats:

  • A unary logical operator and one integer operand
  • Two integer operands separated by a binary logical operator
  • One integer operand

Logical expressions are valid only when the operands are integers. If the expression contains two integer operands of differing data types, the resulting integer has the same data type as the higher integer operand. For example, the result of an expression that contains a BYTE integer and a WORD integer would be a WORD integer. Table 1-11 lists the logical operators.

Table 1-11 Logical Operators
Operator Example Meaning
NOT NOT A% The bit-by-bit complement of A%. If A% is true (--1), NOT A% is false (0).
AND A% AND B% The logical product of A% and B%. A% AND B% is true only if both A% and B% are true.
OR A% OR B% The logical sum of A% and B%. A% OR B% is false only if both A% and B% are false; otherwise, A% OR B% is true.
XOR A% XOR B% The logical exclusive OR of A% and B%. A% XOR B% is true if either A% or B% is true but not if both are true.
EQV A% EQV B% The logical equivalence of A% and B%. A% EQV B% is true if A% and B% are both true or both false; otherwise the value is false.
IMP A% IMP B% The logical implication of A% and B%. A% IMP B% is false only if A% is true and B% is false; otherwise, the value is true.

The truth tables in Figure 1-2 summarize the results of these logical operations. Zero is false; --1 is true.

Figure 1-2 Truth Tables


The operators XOR and EQV are logical complements.

BASIC determines whether the condition is true or false by testing the result of the logical expression to see whether any bits are set. If no bits are set, the value of the expression is zero and it is evaluated as false; if any bits are set, the value of the expression is nonzero, and the expression is evaluated as true. However, logical operators can return unanticipated results unless --1 is specified for true values and zero for false.

In the following example, the values of A% and B% both test as true because they are nonzero values. However, the logical AND of these two variables returns an unanticipated result of false.


A% = 2%
B% = 4%
IF A% THEN PRINT 'A% IS TRUE'
IF B% THEN PRINT 'B% IS TRUE'
IF A% AND B% THEN PRINT 'A% AND B% IS TRUE'
             ELSE PRINT 'A% AND B% IS FALSE'
END

Output


A% IS TRUE
B% IS TRUE
A% AND B% IS FALSE

The program returns this seemingly contradictory result because logical operators work on the individual bits of the operands. The 8-bit binary representation of 2% is as follows:


0  0  0  0  0  0  1  0

The 8-bit binary representation of 4% is as follows:


0  0  0  0  0  1  0  0

Each value tests as true because it is nonzero. However, the AND operation on these two values sets a bit in the result only if the corresponding bit is set in both operands. Therefore, the result of the AND operation on 4% and 2% is as follows:


0  0  0  0  0  0  0  0

No bits are set in the result, so the value tests as false (zero).

If the value of B% is changed to 6%, the resulting value tests as true (nonzero) because both 6% and 2% have the second bit set. Therefore, BASIC sets the second bit in the result and the value tests as nonzero and true.

The 8-bit binary representation of --1 is as follows:


1  1  1  1  1  1  1  1

The result of --1% AND --1% is --1% because BASIC sets bits in the result for each corresponding bit that is set in the operands. The result tests as true because it is a nonzero value, as shown in the following example:


A% = --1%
B% = --1%
IF A% THEN PRINT 'A% IS TRUE'
IF B% THEN PRINT 'B% IS TRUE'
IF A% AND B% THEN PRINT 'A% AND B% IS TRUE'
             ELSE PRINT 'A% AND B% IS FALSE'
END

Output


A% IS TRUE
B% IS TRUE
A% AND B% IS TRUE

Your program may also return unanticipated results if you use the NOT operator with a nonzero operand that is not --1.

In the following example, BASIC evaluates both A% and B% as true because they are nonzero. NOT A% is evaluated as false (zero) because the binary complement of --1 is zero. NOT B% is evaluated as true because the binary complement of 2 has bits set and is therefore a nonzero value.


A%=-1%
B%=2
IF A% THEN PRINT 'A% IS TRUE'
      ELSE PRINT 'A% IS FALSE'
IF B% THEN PRINT 'B% IS TRUE'
      ELSE PRINT 'B% IS FALSE'
IF NOT A% THEN PRINT 'NOT A% IS TRUE'
          ELSE PRINT 'NOT A% IS FALSE'
IF NOT B% THEN PRINT 'NOT B% IS TRUE'
          ELSE PRINT 'NOT B% IS FALSE'
END

Output


A% IS TRUE
B% IS TRUE
NOT A% IS FALSE
NOT B% IS TRUE

1.7.4 Evaluating Expressions

BASIC evaluates expressions according to operator precedence. Each arithmetic, relational, and string operator in an expression has a position in the hierarchy of operators. The operator's position informs BASIC of the order in which to perform the operation. Parentheses can change the order of precedence.

Table 1-12 lists all operators as BASIC evaluates them. Note the following:

  • Operators with equal precedence are evaluated logically from left to right.
  • BASIC evaluates expressions enclosed in parentheses first, even when the operator in parentheses has a lower precedence than that outside the parentheses.

Table 1-12 Numeric Operator Precedence
Operator Precedence
** or ^ 1
-- (unary minus) or + (unary plus) 2
* or / 3
+ or -- 4
+ (concatenation) 5
all relational operators 6
NOT 7
AND 8
OR, XOR 9
IMP 10
EQV 11

For example, BASIC evaluates the following expression in five steps:


A = 15^2 + 12^2 - (35 * 8)
1. (35 * 8) = 280 Multiplication
2. 15^2 = 225 Exponentiation (leftmost expression)
3. 12^2 = 144 Exponentiation
4. 225 + 144 = 369 Addition
5. 369 -- 280 = 89 Subtraction

There is one exception to this order of precedence: when an operator that does not require operands on either side of it (such as NOT) immediately follows an operator that does require operands on both sides (such as the addition operator (+)), BASIC evaluates the second operator first. For example:


A% + NOT B% + C%

This expression is evaluated as follows:


(A% + (NOT B%)) + C%

BASIC evaluates the expression NOT B before it evaluates the expression A + NOT B. When the NOT expression does not follow the addition (+) expression, the normal order of precedence is followed. For example:


NOT A% + B% + C%

This expression is evaluated as:


NOT ((A% + B%) + C %)

BASIC evaluates the two expressions (A% + B%) and ((A% + B%) + C%) because the + operator has a higher precedence than the NOT operator.

BASIC evaluates nested parenthetical expressions from the inside out.

In the following example, BASIC evaluates the parenthetical expression A quite differently from expression B. For expression A, BASIC evaluates the innermost parenthetical expression (25 + 5) first, then the second inner expression (30 / 5), then (6 * 7), and finally (42 + 3). For expression B, BASIC evaluates (5 / 5) first, then (1 * 7), then (25 + 7 + 3) to obtain a different value.


A = ((((25 + 5) / 5) * 7) + 3)
PRINT A
B = 25 + 5 / 5 * 7 + 3
PRINT B

Output


45
35

1.8 Program Documentation

Documentation within a program clarifies and explains source program structure. These explanations, or comments, can be combined with code to create a more readable program without affecting program execution. Comments can appear in two forms:

  • Comment fields (including empty statements)
  • REM statements

1.8.1 Comment Fields

A comment field begins with an exclamation point (!) and ends with a carriage return. You supply text after the exclamation point to document your program. You can specify comment fields while creating BASIC programs at DCL level. BASIC does not execute text in a comment field. Example 1-6 shows how to specify a comment field.

Example 1-6 Specifying a Comment Field

! FOR loop to initialize list Q
FOR I = 1 TO 10
    Q(I) = 0 ! This is a comment
NEXT I
! List now initialized

BASIC executes only the FOR...NEXT loop. The comment fields, preceded by exclamation points, are not executed.

Example 1-7 shows how you can use comment fields to help make your program more readable and allow you to format your program into readily visible logical blocks. Example 1-7 also shows how comment fields can be used as target lines for GOTO and GOSUB statements.

Example 1-7 Using Comment Fields to Format a Program

!
! Square root program
!
INPUT 'Enter a number';A
PRINT 'SQR of ';A;'is ';SQR(A)
!
! More square roots?
!
INPUT 'Type "Y" to continue, press RETURN to quit';ANS$
GOTO 10 IF ANS$ = "Y"
!
END

You can also use an exclamation point to terminate a comment field, but this practice is not recommended. You should make sure that there are no exclamation points in the comment field itself; otherwise, BASIC treats the text remaining on the line as source code.

Note

Comment fields in DATA statements are invalid; the compiler treats the comments as additional data.

1.8.2 REM Statements

A REM statement begins with the REM keyword and ends when BASIC encounters a new line number. The text you supply between the REM keyword and the next line number documents your program. Like comment fields, REM statements do not affect program execution. BASIC ignores all characters between the keyword REM and the next line number. Therefore, the REM statement can be continued without the ampersand continuation character and should be the only statement on the line or the last of several statements in a multistatement line. Example 1-8 shows the use of the REM statement.

Example 1-8 Using REM Statements in BASIC Programs

  5   REM This is an example
      A=5
      B=10
      REM A equals 5
          B equals 10
 10   PRINT A, B

Output



0       0

Note that because line 5 began with a REM statement, all the statements in line 5 were ignored.

The REM statement is nonexecutable. When you transfer control to a REM statement, BASIC executes the next executable statement that lexically follows the referenced statement.

Note

Because BASIC treats all text between the REM statement and the next line number as commentary, REM should be used very carefully in programs that follow the implied continuation rules. REM statements are disallowed in programs without line numbers.

In the following example, the conditional GOTO statement in line 20 transfers program control to line 10. BASIC ignores the REM comment on line 10 and continues program execution at line 20.


10  REM ** Square root program
20  INPUT 'Enter a number';A
    PRINT 'SQR of ';A;'is ';SQR(A)
    INPUT 'Type "Y" to continue, press RETURN to quit';ANS$
    GOTO 10 IF ANS$ = "Y"
40  END


Chapter 2
Compiler Directives

Compiler directives are instructions that cause HP BASIC to perform certain operations as it translates the source program. This chapter describes all of the compiler directives supported by HP BASIC. The directives are listed and discussed alphabetically.


%ABORT

The %ABORT directive terminates program compilation and displays a fatal error message that you can supply.

Format

%ABORT [ str-lit ]


Syntax Rules

None


Remarks

  • Only a line number or a comment field can appear on the same physical line as the %ABORT directive.
  • HP BASIC stops the compilation and terminates the listing file as soon as it encounters a %ABORT directive. An optional str-lit is displayed on the terminal screen and in the compilation listing, if a listing has been requested.

Example


%IF %VARIANT = 2 %THEN
    %ABORT "Cannot compile with variant 2"
%END %IF

%CROSS

The %CROSS directive causes HP BASIC to begin or resume accumulating cross-reference information for the listing file.

Format

%CROSS


Syntax Rules

None


Remarks

  • Only a line number or a comment field can appear on the same physical line as the %CROSS directive.
  • The %CROSS directive has no effect unless you request both a listing file and a cross-reference. For more information about listing file format, see the HP BASIC for OpenVMS User Manual.
  • When a cross-reference is requested, the HP BASIC compiler starts or resumes accumulating cross-reference information immediately after encountering the %CROSS directive.

Example


%CROSS

%DECLARED

The %DECLARED directive is a built-in lexical function that allows you to determine whether a lexical variable has been defined with the %LET directive. If the lexical variable named in the %DECLARED function is defined in a previous %LET directive, the %DECLARED function returns the value -1. If the lexical variable is not defined in a previous %LET directive, the %DECLARED function returns the value 0.

Format

%DECLARED (lex-var)


Syntax Rules

  • The %DECLARED function can appear only in a lexical expression.
  • Lex-var is the name of a lexical variable. Lexical variables are always LONG integers.
  • Lex-var must be enclosed in parentheses.

Remarks

None


Example


! +
! Use the following code in %INCLUDE files
! which reference constants that may be already ! -
%IF %DECLARED (%TRUE_FALSE_DEFINED) = 0
%THEN
    DECLARE LONG CONSTANT True = -1, False = 0
    %LET %TRUE_FALSE_%END %IF

%DEFINE

The %DEFINE directive lets you define a user-defined identifier as another identifier or keyword.

Format

%DEFINE macro-id replacement-token


Syntax Rules

  • Macro-id is a user identifier that follows the rules for BASIC identifiers. It must not be a keyword or a compiler directive.
  • Replacement-token may be an identifier, a keyword, a compiler directive, a literal constant, or an operator.
  • The "&" line continuation character may be used after the macro-id to continue the %DEFINE directive on the next line.
  • The "\" statement separator cannot be used with the %DEFINE directive.
  • "!" comments and line numbers used with the %DEFINE directive behave in the same manner as they do with other compiler directives.

Remarks

  • The replacement-token is substituted for every subsequent occurrence of the macro identifier in the program text.
  • Macro-identifiers in REM or "!" comments, string literals, or DATA statements are not replaced.
  • A macro-id cannot be used as a line number.
  • A macro definition is in effect from the %DEFINE directive that defines it until either a corresponding %UNDEFINE directive or the end of the source module is encountered. This applies to any included code that occurs after the definition.
  • A previously defined macro identifier may be redefined by using the %DEFINE directive.
  • A previously defined macro may be canceled by using the %UNDEFINE directive.
  • Macros may not be nested. For example, if the replacement-token is an identifier that is defined by itself or some other %DEFINE directive, it is not replaced.
  • Macro-identifiers are not known to the Debugger.
  • The %DEFINE directive can be used within conditionally compiled code.

Example


%DEFINE widget LONG
DECLARE widget X
X = 3.75
PRINT "X squared :"; X*X

Output


X squared : 9

%IDENT

The %IDENT directive lets you identify the version of a program module. The identification text is placed in the object module and printed in the listing header.

Format

%IDENT str-lit


Syntax Rules

Str-lit is the identification text. Str-lit can consist of up to 31 ASCII characters. If it has more than 31 characters, HP BASIC truncates the extra characters and signals a warning message.


Remarks

  • Only a line number or a comment field can appear on the same physical line as the %IDENT directive.
  • The HP BASIC compiler inserts the identification text in the first 31 character positions of the second line on each listing page. HP BASIC also includes the identification text in the object module, if the compilation produces one, and in the map file created by the OpenVMS Linker.
  • The %IDENT directive should appear at the beginning of your program if you want the identification text to appear on the first page of your listing. If the %IDENT directive appears after the first program statement, the text will appear on the next page of the listing file.
  • You can use the %IDENT directive only once in a module. If you specify more than one %IDENT directive in a module, HP BASIC signals a warning and uses the identification text specified in the first directive.
  • No default identification text is provided.

Example


%IDENT "Version 10"
   .
   .
   .

Output


TIME$MAIN
Version 10

     1         10      %IDENT "Version 10"
   .
   .
   .


Previous Next Contents Index