[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP BASIC for OpenVMS
User Manual


Previous Contents Index

5.2.1 Print Zones---The Comma and the Semicolon

A terminal line contains zones that are 14 character positions wide. The number of zones in a line depends on the width of your terminal: a 72-character line contains 5 zones, which start in columns 1, 15, 29, 43, and 57. A 132-character line has additional print zones starting at columns 71, 85, 99, and 113.

The PRINT statement formats program output into these zones in different ways, depending on the character that separates the elements to be printed. If a comma precedes the PRINT item, BASIC prints the item at the beginning of the next print zone. If the last print zone on a line is filled, BASIC continues output at the first print zone on the next line. For example:


INPUT A ,B ,C ,D ,E ,F
PRINT A ,B ,C ,D ,E ,F
END

Output


? 5,10,15,20,25,30[Return]
 5             10            15            20           25
 30

BASIC skips one print zone for each extra comma between list elements. For example, the following program prints the value of A in the first zone and the value of B in the third zone:


A = 5
B = 10
PRINT "first zone",,"third zone"
PRINT A,,B
END

Output



first zone                   third zone
 5                            10

If you separate print elements with a semicolon, BASIC does not move to the next print zone. In the following example, the first PRINT statement prints two numbers. (Printed numbers are preceded by a space or a minus sign and followed by one space.) The second PRINT statement prints two strings.


PRINT 10; 20
PRINT "ABC"; "XYZ"
END

Output


 10  20
ABCXYZ

Whether you use a comma or a semicolon at the end of the PRINT statement, the cursor remains at its current position until BASIC encounters another PRINT or INPUT statement. In the following example, BASIC prints the current values of X, Y, and Z on one line because a comma follows the last item in the line PRINT X, Y:


INPUT X,Y,Z
PRINT X,Y,
PRINT Z
END

Output


? 5,10,15
 5            10                 15

The following example shows PRINT statements using a comma, a semicolon, and no formatting character after the last print item:


!No comma after I%, so each element
!Prints on its own line
!
PRINT I% FOR I% = 1% TO 10%
PRINT
!
!A comma follows J%, so each
!element prints in a separate zone
!
MARGIN 80%
PRINT J%, FOR J% = 1% TO 10%
PRINT
!
!A semicolon follows K%, so print
!elements are packed together
!
PRINT K%; FOR K% = 1% TO 10%
END

Output


1

2

3

4

5

6

7

8

9

10

1             2             3              4               5

6             7             8              9               10

1  2  3  4  5  6  7  8  9  10

Commas and semicolons also let you control the placement of string output. For example:


PRINT "first zone",,"third zone",,"fifth zone"
END

Output


first zone                  third zone                  fifth zone

The extra comma between strings causes BASIC to skip another print zone. In the following example, the first string is longer than the print zone. When the two strings are printed, the second string begins in the third print zone because that is the next available print zone after the first string is printed.


PRINT "abcdefghijklmnopqrstuvwxyz","pizza"
PRINT "first zone","second zone","third zone"

Output


abcdefghijklmnopqrstuvwxyz  pizza
first zone    second zone   third zone

5.2.2 Output Format for Numbers and Strings

BASIC prints strings exactly as you type them, with no leading or trailing spaces. It does not print quotation marks unless they are delimited by another matching pair. For example:


PRINT 'PRINTING "QUOTATION" MARKS'
END

Output


PRINTING "QUOTATION" MARKS

BASIC follows these rules for printing numbers:

  • When you print numeric fields, BASIC precedes each number with a space or a minus sign and follows it with a space.
  • BASIC does not print trailing zeros to the right of the decimal point. If all digits to the right of the decimal point are zeros, BASIC omits the decimal point as well.
  • When you print LONG integers, BASIC prints up to 10 significant digits.
  • When you print DECIMAL values, HP BASIC prints up to 31 digits.

HP BASIC follows these rules for printing floating-point numbers:

  • If a floating-point number can be represented exactly by 6 decimal digits (or fewer) and, optionally, a decimal point, BASIC prints it that way.
  • When you print a floating-point number whose integer portion is 6 decimal digits or less (for example, 1234.567), BASIC rounds the number to 6 digits (1234.57). If the integer portion of the number is 7 decimal digits or larger, BASIC rounds the number to 6 digits and prints it in E format. See the HP BASIC for OpenVMS Reference Manual for more information about E format.
  • When you print a floating-point number with magnitude from 0.1 to 1, BASIC rounds it to 6 digits. When you print a floating-point number with more than 6 digits, and with magnitude smaller than 0.1, BASIC rounds it to 6 digits and prints it in E format.

The PRINT statement displays only up to 6 digits of precision for floating-point numbers. This corresponds to the precision of the SINGLE or SFLOAT data types. To display the extra digits in DOUBLE, GFLOAT, TFLOAT, or XFLOAT numbers, you must use the PRINT USING statement. See Chapter 14 for more information about the PRINT USING statement.

The following example shows how BASIC prints various numbers with single precision:


FOR I = 1 TO 20
    PRINT 2^(-I),I,2^I
NEXT I
END

Output


 .5              1               2
 .25             2               4
 .125            3               8
 .0625           4               16
 .03125          5               32
 .015625         6               64
 .78125E-02      7               128
 .390625E-02     8               256
 .195313E-02     9               512
 .976563E-03     10              1024
 .488281E-03     11              2048
 .244141E-03     12              4096
 .12207E-03      13              8192
 .610352E-04     14              16384
 .305176E-04     15              32768
 .152588E-04     16              65536
 .767939E-05     17              131072
 .38147E-05      18              262144
 .190735E-05     19              524288
 .953674E-06     20              .104858E+07

5.3 Terminal-Format Files

Terminal-format files let you perform simple I/O to disk files. The records in a terminal-format file must be accessed sequentially. That is, you must access the records in the file one by one, from the first to the last. You can add new records only at the end of the file.

Just as the INPUT, LINPUT, and INPUT LINE statements receive information from a terminal, the INPUT #, LINPUT #, and INPUT LINE # statements receive information from a terminal-format file. And, as the PRINT statement sends information to the terminal, the PRINT # statement sends information to a terminal-format file.

Terminal-format files are useful for creating files to be printed on a line printer, or for supplying a program with moderate amounts of input. However, if you want to use the same file for both input and output, you should not use terminal-format files. Instead, use sequential, relative, or indexed files. For more information, see Chapter 13.

You do not have to use a program to create a terminal-format file. You can use a text editor to create a file and insert data, then use a BASIC program to open the file and retrieve the data.

5.3.1 Opening and Closing a Terminal-Format File

You use the OPEN statement to create a file, or to gain access to an existing file. If you do not specify either FOR INPUT or FOR OUTPUT in the OPEN statement, BASIC tries to open an existing file. If the file does not exist, BASIC creates a new one.

The channel specification lets you associate a number with the file for as long as the file is open. All I/O operations to or from the file use this number.

When you are finished accessing a file, you close it with the CLOSE statement.

5.3.2 Writing Records to a Terminal-Format File

The following example receives information from a terminal, then writes the information to a terminal-format file as a report:


PRINT "This program creates a daily sales report file named SALES.DAT"
OPEN "SALES.DAT" FOR OUTPUT AS FILE #4%
PRINT #4%, "Salesperson","Sales Area","Items Sold"
PRINT #4%
INPUT "How many salespersons for today's report"; sales_persons%
FOR I% = 1% TO sales_persons%
    INPUT "Salesperson's name"; s_name$
    INPUT "Sales area"; area$
    INPUT "Number of items sold"; items_sold%
    PRINT #4%, s_name$, area$, items_sold%
NEXT I%
CLOSE #4%
END

Output


This program creates a daily sales report file named SALES.DAT
How many salespersons for today's report? 3
Salesperson's name? JONES
Sales area? NJ
Items sold? 5
Salesperson's name? SMITH
Sales area? NH
Items sold? 6
Salesperson's name? BAINES
Sales area? VT
Items sold? 8

This program first prints a header explaining its purpose, then opens a terminal-format file on channel 4. After this file is opened, the two PRINT # statements place an explanatory header followed by a blank line into the file.

The program then prompts you for the number of salespersons for which data is to be entered. The FOR...NEXT loop prompts for the name, sales area, and items sold for each salesperson. The FOR...NEXT loop executes only as many times as there are salespersons. See Chapter 9 for more information about FOR...NEXT loops.

After the data has been entered for each salesperson, the program writes this information to the terminal-format file. Because the response to the first question was 3, the FOR...NEXT loop executes three times.

After the last item has been printed to the file, the program closes the file and ends. When you display the file with the DCL command TYPE, you see that the information is printed under the proper headers. You can also print the file on a line printer. The PRINT # statement formats the output in print zones as the PRINT statement does.


$ TYPE SALES.DAT

Salesperson   Sales Area    Items Sold

JONES         NJ             5
SMITH         NH             6
BAINES        VT             8


Chapter 6
Arrays

An array is a set of data that is ordered in any number of dimensions. This chapter describes how to create and use HP BASIC arrays.

6.1 Overview of Arrays

A one-dimensional array is called a list or vector. A two-dimensional array is called a matrix. HP BASIC arrays can have up to 32 dimensions, and a specific type of HP BASIC arrays can be redimensioned at run time. In addition, you can specify the data type of the values in an array by using data type keywords or suffixes.

The subscript of an element in an array defines that element's position in the array. When you create an array, you specify:

  • The number of dimensions that the array contains
  • The range of values for the subscripts in each dimension of the array

BASIC arrays are zero-based by default; that is, when calculating the number of elements in a dimension, you count from zero to the number of elements specified. For example, an array with an upper bound of 10 and no specified lower bound has 11 elements: 0 to 10, inclusive. The array My_array(3,3) has 16 elements: 0 to 3 in each dimension, or 42.

BASIC also lets you specify a lower bound for any or all dimensions in an array, unless the array is a virtual array. By specifying lower and upper bounds for arrays, you can make your array subscripts meaningful. For example, the following array contains sales information for the years 1990 to 1999:


DECLARE REAL Sales_data(1990 TO 1999)

To refer to an element in the array Sales_data, you need only specify the year you are interested in. For example, to print the information for the year 1999, you would enter:


PRINT Sales_data(1999)

You can create arrays either implicitly or explicitly. You implicitly create arrays having any number of dimensions by referencing an element of the array. If you implicitly create an array, BASIC sets the upper bound to 10 and the lower bound to zero. Therefore, any array that you create implicitly contains 11 elements in each dimension.

The following example refers to the array Student_grades. If the array has not been previously declared, BASIC will create a one-dimensional array with that name. The array contains 11 elements.


Student_grades(8) = "B"

You create arrays explicitly by declaring them in a DIM, DECLARE, COMMON, or MAP statement, or record declaration. Note that if you want to specify lower bounds for your array subscripts, you must declare the array explicitly.

When you declare an array explicitly, the value that you give for the upper bound determines the maximum subscript value in that dimension. If you specify a lower bound, then that is the minimum subscript value in that dimension. If you do not specify a lower bound, BASIC sets the lower bound in that dimension to zero. You can specify bounds as either positive or negative values. However, the lower bound of each dimension must always be less than or equal to the upper bound for that dimension.

You can use MAT statements to create and manipulate arrays; however, MAT statements are valid only on arrays of one or two dimensions. In addition, the lower bounds of all dimensions in an array referenced in a MAT statement must be zero.

6.2 Creating Arrays Explicitly

You can create arrays explicitly with four BASIC statements: DECLARE, DIMENSION, COMMON, and MAP.

In addition, you can declare arrays as components of a record data type. See Chapter 8 for more information about records.

Normally, you use the DECLARE statement to create arrays. However, you might want to create the array with another BASIC statement as follows:

  • Use the DIM statement to create virtual arrays and arrays that can be redimensioned at run time.
  • Use the COMMON statement to create arrays that can be shared among program modules or to create arrays of fixed-length strings.
  • Use the MAP statement to create an array and associate it with a record buffer, or to overlay the storage for an array, thus accessing the same storage in different ways.

When you create an array, the bounds you specify determine the array's size. The maximum value allowed for a bound can be as large as 2147483467; however, this number is actually limited by the amount of virtual storage available to you. Very large arrays and arrays with many dimensions can cause fatal errors at both compile time and run time.

The following restrictions apply to arrays:

  • When referencing an array, you must use the same number of subscripts as was specified when the array was created.
  • You can use identical names for a simple variable and an array; for example, A% and A%(5,5). However, this is not a recommended programming practice. If you use identical names for arrays with a different number of subscripts, for example, A(5), and A(10,10), BASIC prints the error "Inconsistent subscript usage" at compile time.
  • If subscript checking is enabled, HP BASIC signals the error "Subscript out of range" (ERR=55) if you reference an array element whose subscripts are one of the following:
    • Greater than the current upper bound of the array
    • Less than the current lower bound of the array
    • Less than zero where no lower bound was specified

6.2.1 Creating Arrays with the DECLARE Statement

The DECLARE statement creates and names variables and arrays. All elements of arrays created with the DECLARE statement are initialized to zero or the null string. The following statement creates a longword integer array with 11 elements:


DECLARE LONG FIRST_ARRAY(1980 TO 1990)

Note that the STRING data type with the DECLARE statement causes the creation of an array of dynamic strings. To create an array of fixed-length strings, declare the array in a COMMON or MAP statement or as part of a RECORD structure.

6.2.2 Creating Arrays with the DIM Statement

The DIM statement creates and names one or more arrays. Use the DIM statement to create an array when you want to:

  • Redimension the array at run time
  • Create a virtual array

When creating arrays with the DIM statement, you specify the data type of the array elements with a data type keyword, a special suffix on the array name, or both. The array name can be any valid variable name. If you do not supply a data type keyword, the data type is determined by the suffix of the array name:

  • If the array name ends with a dollar sign ($), the array stores string data.
  • If the array name ends with a percent sign (%), the array stores integer data.
  • If the array name does not end with either a percent sign or a dollar sign, the array stores data of the default type. The default type is single-precision, floating-point unless you change the default. See Chapter 4 for more information about default data types.

Even if the DIM statement contains a data type keyword, the array name can still end in the appropriate data type suffix. This makes the data type of the array immediately obvious.

The DIM statement can be either executable or declarative. If the specified bounds are constants, the DIM statement is declarative. This means that the storage is allocated at compile time, and the array cannot appear in any other DIM statement.

However, if any of the specified bounds are variables (simple or subscripted), the DIM statement is executable. This means that the storage for the array is allocated at run time, and the array can be redimensioned with a DIM statement any number of times.

Note

In the DIM statement, bounds can be either constants or variables (simple or subscripted), but not expressions.

When an array is redimensioned with the executable DIM statement, the array can become larger or smaller than it was. However, redimensioning an array in this way causes it to be reinitialized, and all data in the array is lost.

In contrast, MAT statements let you redimension an array to be the same size or smaller than it was. However, MAT statements redimension arrays only when assigning values or performing matrix I/O; therefore, the fact that MAT statements reinitialize the array does not matter. See Section 6.6 for more information about MAT statements.

6.2.2.1 Declarative DIM Statements

Declarative DIM statements have integer constants as bounds. The percent sign is optional for bounds; however, BASIC signals the error "Integer constant required" if a constant bound contains a decimal point. The following statement creates a 101-element virtual array containing string data. The elements of this array can each have a maximum length of 256 characters.


DIM #1%, STRING VIRT_ARRAY(100) = 256%

The following restrictions apply to the use of declarative DIM statements:

  • A declarative DIM statement must lexically precede any reference to the array it dimensions.
  • The lower bounds of all virtual array dimensions must be zero.
  • You must open a VIRTUAL file on the specified channel before you can access elements of the virtual array.


Previous Next Contents Index