Q(uick)BASIC Statement: PRINT

Quick View

PRINT

A device I/O statement that outputs data on the screen

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • PRINT [#filenumber%,] [expressionlist] [{; | ,}]
  • LPRINT [expressionlist] [{; | ,}]
Description/Parameter(s)
filenumber% The number of an open file. If you don't specify a file number, PRINT writes to the screen.
expressionlist A list of one or more numeric or string expressions to print.
{; | ,} Determines where the next output begins:
  • ; means print immediately after the last value.
  • , means print at the start of the next print zone.
  •   Print zones are 14 characters wide.
Example
OPEN "TEST.DAT" FOR OUTPUT AS #1 PRINT #1, USING "##.### "; 12.12345 CLOSE OPEN "TEST.DAT" FOR INPUT AS #1 INPUT #1, a$ PRINT a$ LPRINT "This is a line"; 1 LPRINT "This is a line", LPRINT 2
Syntax
  • PRINT [expressionlist][{,|;}]
Description/Parameter(s)

If expressionlist is omitted, a blank line is printed. If expressionlist is included, the values of the expressions are printed on the screen. The expressions in the list may be numeric or string expressions. (String literals must be enclosed in quotation marks.)

A printed number is always followed by a space. If the number is positive, it is also preceded by a space; if the number is negative, it is preceded by a minus sign (-).

There are two formats that PRINT uses to display single- and double-precision numbers: fixed point and floating point. If PRINT can represent a single-precision number in the fixed-point format with seven or fewer digits and no loss of accuracy, then it uses the fixed-point format; otherwise, it uses the floating-point format. For example, the number 1.1E-6 is output displayed as .0000011, but the number 1.1E-7 is output as 1.1E-7.

Similarly, if PRINT can represent a double-precision number in the fixed-point format with 16 or fewer digits and no loss of accuracy, then it uses the fixed-point format; otherwise, it uses the floating point format. For example, the number 1.1D-15 is output as .0000000000000011, but the number 1.1D-16 is output as 1.1D-16.

The PRINT statement supports only elementary BASIC data types (integers, long integers, single-precision real numbers, double- precision real numbers, and strings). To print information in a record, use the PRINT statement with individual record elements as in the following fragment:

  • TYPE MyType
  • Word AS STRING * 20
  • Count AS LONG
  • END TYPE
  • DIM Myrec AS MyType
  •  
  • PRINT Myrec.Word

Print Positions

The position of each printed item is determined by the punctuation used to separate the items in the list. BASIC divides the line into print zones of 14 spaces each. In the expression list, a comma makes the next value print at the start of the next zone. A semicolon makes the next value print immediately after the last value. Typing one or more spaces or tabs between expressions has the same effect as typing a semicolon.

If a comma or a semicolon terminates the list of expressions, the next PRINT statement prints on the same line, after spacing accordingly. If the expression list ends without a comma or a semicolon, a carriage-return and line-feed sequence is printed at the end of the line. If the printed line is wider than the screen width, BASIC goes to the next physical line and continues printing.

Examples

These examples show the use of commas and semicolons with PRINT.

Here is an example of using commas in a PRINT statement to print each value at the beginning of the next print zone:

CLS ' Clear screen X = 5 PRINT X + 5, X - 5, X * (-5), X ^ 5 END

Sample Output:

10 0 -25 3125

In this example, the semicolon at the end of the first PRINT statement makes the first two PRINT statements print on the same line. The last PRINT statement prints a blank line before the next prompt

CLS ' Clear screen DO INPUT "Input X (type 0 to quit): ", X IF X = 0 THEN EXIT DO ELSE PRINT X; "squared is"; X ^ 2; "and"; PRINT X; "cubed is"; X^3 PRINT END IF LOOP

Sample Output:

Input X (type 0 to quit): 9 9 squared is 81 and 9 cubed is 729 Input X (type 0 to quit): 21 21 squared is 441 and 21 cubed is 9261 Input X (type 0 to quit): 0

In this example, the semicolons in the PRINT statement print each value immediately after the preceding value. Note that a space always follows a number and precedes a positive number:

CLS ' Clear screen FOR X = 1 TO 5 J = J + 5 K = K + 10 PRINT J; K; NEXT X

Sample Output:

5 10 10 20 15 30 20 40 25 50
Syntax
  • PRINT [expressionlist] [{;|,}]
Description/Parameter(s)
expressionlist The values that are displayed on the screen.
{;|,} Determines the screen location of the text cursor for the next screen input or output statement:
  • ";" means print immediately after last value.
  • "," means print at start of next print zone.
If expressionlist is omitted, a blank line is displayed.
  • The argument expressionlist can contain numeric or string expressions. String literals must be enclosed in quotation marks.
  • The PRINT statement supports only elementary BASIC data types (integers, long integers, single-precision real numbers, double- precision real numbers, currency, and strings). To print information from a record, use individual record element names in the PRINT statement, as in the following code fragment:

  • TYPE MyType
  • Word AS STRING * 20
  • Count AS LONG
  • END TYPE
  • DIM Myrec AS MyType
  •  
  • PRINT Myrec.Word

Usage Notes

  • Item-Format Rules
    • A printed number is always followed by a space.
    • If the number is positive, it is also preceded by a space; if the number is negative, it is preceded by a minus sign (-).
    • If a single-precision number can be expressed as seven or fewer digits with no loss of accuracy, then it is printed in fixed- point format; otherwise, floating-point format is used. For example, the number 1.1E-6 is displayed as .0000011, but the number 1.1E-7 is displayed as 1.1E-7.
    • If a double-precision number can be expressed as 15 or fewer digits and with no loss of accuracy, then it is printed in fixed-point format; otherwise, floating-point format is used. For example, the number 1.1D-14 is displayed as .000000000000011, but the number 1.1D-15 is displayed as 1.1D-15.

  • Print-Line Format Rules
    • The print line is divided into print zones of 14 spaces each.
    • The position of each printed item is determined by the punctuation used to separate the items in expressionlist:
      • A semicolon makes the next value print immediately after the last value.
      • A comma makes the next value print at the start of the NEXT zone.
      • Using one or more spaces or tabs between expressions has the same effect as using a semicolon.
    • If a comma or a semicolon terminates the list of expressions, the next PRINT statement to execute prints on the same line, after spacing accordingly.
    • If the expression list ends without a comma or a semicolon, a carriage-return-and-line-feed sequence are printed at the end of the line.
    • If the printed line is wider than the screen width, BASIC goes to the next physical line and continues printing.
Examples

These three examples show the use of commas and semicolons with the PRINT statement.

This example uses commas in a PRINT statement to print each value at the beginning of the next print zone.

CLS 'Clear screen. X = 5 PRINT X + 5, X - 5, X * (-5), X ^ 5 END

Sample Output:

10 0 -25 3125

In this example, the semicolon at the end of the first PRINT statement makes the first two PRINT statements print on the same line. The last PRINT statement prints a blank line before the next prompt.

CLS 'Clear screen. DO INPUT "Input X (type 0 to quit): ", X IF X = 0 THEN EXIT DO ELSE PRINT X; "squared is"; X ^ 2; "and"; PRINT X; "cubed is"; X ^ 3 PRINT END IF LOOP

Sample Output:

Input X (type 0 to quit): 9 9 squared is 81 and 9 cubed is 729 Input X (type 0 to quit): 21 21 squared is 441 and 21 cubed is 9261 Input X (type 0 to quit): 0

In this example, the semicolons in the PRINT statement print each value immediately after the preceding value. Note that a space always follows a number and precedes a positive number.

CLS 'Clear screen. FOR X = 1 TO 5 J = J + 5 K = K + 10 PRINT J; K; NEXT X

Sample Output:

5 10 10 20 15 30 20 40 25 50