Previous | Contents | Index |
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 |
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-13 lists all operators as BASIC evaluates them. Note the following:
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 |
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:
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 as well as in the VAX BASIC Environment. In both cases, 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.
Comment fields in DATA statements are invalid; the compiler treats the comments as additional data. |
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 |
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.
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 |
The information in this chapter is specific to Compaq BASIC for OpenVMS VAX systems (referred to as VAX BASIC). The Environment commands are not supported by Compaq BASIC for OpenVMS Alpha systems (Alpha BASIC). For more information about the differences between Alpha BASIC and VAX BASIC, see Appendix C. |
Environment commands are commands that you use in the VAX BASIC Environment. With Environment commands you can display, edit, and merge VAX BASIC programs, set compiler defaults, move VAX BASIC source programs to and from storage, and execute programs.
This chapter alphabetically lists all of the compiler commands that can be used within the VAX BASIC Environment. For information about immediate mode and calculator mode statements, see the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual.
You can enter comments while in the VAX BASIC Environment by typing an exclamation point (!) and the comment.
None
Ready ! Comments here ... |
$ TYPE BUILD_SPECIAL.COM $ SET VERIFY $ BASIC !+ ! Set the compilation options by uncommenting ! the appropriate ones. !- ! SET LIST SET WORD SET DEBUG !+ ! Get the source module. !- OLD SPECIAL !+ ! Compile it. !- COMPILE !+ ! All done. !- EXIT |
You can execute a DCL command while in the VAX BASIC Environment by typing a dollar sign ($) before the command. VAX BASIC passes the command to the operating system for execution. The context of the VAX BASIC Environment and the program currently in memory do not change.
VAX BASIC passes system-command directly to the OpenVMS operating system without checking for validity.
Ready $ SHOW PROTECTION SYSTEM=RWED, OWNER=RWED, GROUP=RWED, WORLD=RE Ready |
The APPEND command merges an existing VAX BASIC source program with the program currently in memory.
File-spec is the name of the VAX BASIC program you want to merge with the program currently in memory. The default file type is .BAS.
Append file name-- |
Ready New FIRST_TRY.BAS Ready 10 PRINT "First program" APPEND NEW_PROG.BAS Ready LIST 10 PRINT "First Program" 20 PRINT "This section has been appended" . . . |
The ASSIGN command equates a logical name to a complete file specification, a device, or another logical name within the context of the VAX BASIC Environment.
ASSIGN [HENRY.BAS] PRO: |
The COMPILE command converts a VAX BASIC source program to an object module and writes the object file to disk.
COMPILE/OBJ/NOOBJ |
The following qualifiers cannot be used within the VAX BASIC Environment with the COMPILE command:
/ANALYSIS_DATA
/CHECK
/DEPENDENCY_DATA
/DESIGN
/DIAGNOSTICS
/INTEGER_SIZE
/OLD_VERSION=CDD_ARRAY
/OPTIMIZE
/REAL_SIZE
/SCALE
If an object file for the program already exists in your directory, VAX BASIC creates a new version of the .OBJ file.
You should not specify both a file name and file type. For example, if you enter the following command line, VAX BASIC creates two versions of NEWOBJ.FIL:
COMPILE NEWOBJ.FIL/LIS/OBJ |
The first version, NEWOBJ.FIL;1, is the listing file; the second version, NEWOBJ.FIL;2, is the object file. If you specify only a file name, VAX BASIC uses the .OBJ and .LIS file type defaults when creating these files. Use the COMPILE/NOOBJECT command to check your program for errors without producing an object file.
When you exit from the VAX BASIC Environment, all options set with qualifiers return to the system default values. Use the SHOW command to display your system defaults before setting any qualifiers.
Command Qualifiers
The /ANSI_STANDARD qualifier causes VAX BASIC to compile programs according to the ANSI Minimal BASIC standard and to flag syntax that does not conform to the standard. The /NOANSI_STANDARD qualifier causes VAX BASIC not to compile the program according to the ANSI Minimal BASIC standard. The default is /NOANSI_STANDARD.
The /AUDIT qualifier causes VAX BASIC to include a history list entry in the Commond Data Dictionary (CDD) data base when a CDD definition is extracted. Str-lit is a quoted string. File-spec is a text file. The history entry includes the following:
If you specify /NOAUDIT, VAX BASIC does not include a history list entry. The default is /NOAUDIT.
The /BOUNDS_CHECK qualifier causes VAX BASIC to perform range checks on array subscripts. With bounds checking enabled, VAX BASIC checks that all subscript references are within the array boundaries set when the array was declared. If the subscript bounds are not within the bounds initially declared for the array, VAX BASIC signals an error message. If you specify /NOBOUNDS_CHECK, VAX BASIC does not check that all subscript references are within the array bounds set. The default is /BOUNDS_CHECK.
The /BYTE qualifier causes VAX BASIC to allocate 8 bits of storage as the default for all integer data not explicitly typed in the program. Untyped integer values are treated as BYTE values and must be in the BYTE range or VAX BASIC signals the error "Integer error or overflow." Table 1-2 lists VAX BASIC data types and ranges. By default, the VAX BASIC compiler allocates 32 bits of storage.
If you use the /CROSS_REFERENCE qualifier with the /LIST qualifier when you compile your program, the VAX BASIC compiler includes cross-reference information in the program listing file. If you specify /CROSS_REFERENCE=KEYWORDS, VAX BASIC also cross-references VAX BASIC keywords used in the program. If you specify /NOCROSS_REFERENCE, VAX BASIC does not include a cross reference section in the compiler listing. The default is /NOCROSS_REFERENCE.
The /DEBUG qualifier appends to the object file information about symbolic references and line numbers. This information is used by the OpenVMS Debugger when you debug your program. When you specify the /DEBUG qualifier on the COMPILE command, you cause the debugger to be invoked automatically when the program is run at DCL level (unless you specify RUN/NODEBUG). If you specify COMPILE/NODEBUG, information about program symbols and line numbers is not included in the object file. The default is /NODEBUG.
See the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual for more information about using the OpenVMS Debugger.
The /DECIMAL_SIZE qualifier allows you to specify the default size and precision for all DECIMAL data not explicitly assigned size and precision in the program. You specify the total number of digits (d) and the number of digits to the right of the decimal point (s). VAX BASIC signals the error "Decimal error or overflow" (ERR=181) when DECIMAL values are outside the range specified with this qualifier. See Table 1-2 for more information about the storage and range of packed decimal data. The default is /DECIMAL_SIZE=(15,2).
The /DOUBLE qualifier causes VAX BASIC to allocate 64 bits of storage in D_floating format as the default size for all floating-point data not explicitly typed in the program. Untyped floating-point values are treated as DOUBLE values and must be in the DOUBLE range or VAX BASIC signals the error "Floating-point error or overflow." Table 1-2 lists VAX BASIC data types and ranges. The default is /SINGLE.
The /FLAG qualifier causes VAX BASIC to provide compile-time information about program elements that are not compatible with Alpha BASIC or BASIC-PLUS-2 or that Compaq designates as not recommended for new program development. For more information about the differences between Alpha BASIC and VAX BASIC, see Appendix C.
Previous | Next | Contents | Index |