ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

PRINT Statement

Summary

Displays textual output on the screen console.

Syntax

PRINT [list of expressions]

[list of expressions] consists of zero one or more expressions. Each pair of expressions must be separated by either a semicolon or a comma. The list of expressions may end in a trailing semicolon or comma (see description below).

[list of expressions] is therefore: [[expression][,|;]] [ [[expression][,|;]] [ [[expression][,|;]] ... ] ]

Remarks and Examples

A PRINT statement can be used to write a string of text to the screen. For example,

PRINT "Hello world!"

will output

Hello world!

To write an empty line to the screen, use the PRINT statement alone without any expressions after it, as shown in the following example:

10 PRINT "Hello world!"
20 PRINT
30 PRINT "Goodbye!"
RUN
Hello world!

Goodbye!

PRINT statements can include any expressions that evaluate to a string or numeric value. For example, we can show the results of a calculation like this:

PRINT 2 * 3 + 1
7

We can also display the value of a variable like this:

X = 25 * 2
PRINT X
50.0

By using semicolons, we can display multiple values one after the other:

10 N$ = "Bob"
20 PRINT "Hello "; N$; ", the answer is "; 5 * 3
RUN
Hello Bob, the answer is 15

Note that ReadyBASIC does not insert any extra spaces when it encounters a semicolon.

Normally the cursor is moved to the beginning of the next line (like the carriage return on a typewriter) after all of the values have been displayed. If we want to suppress this behavior and leave the cursor at the end of the line, so that the next PRINT statement will keep writing on the same line, we can write a semicolon after the list of expressions, like this:

10 PRINT "The answer is ";
20 X = 2 * 15 / 3
30 PRINT X
RUN
The answer is 10

A comma can be used instead of a semicolon to separate values. With the comma, the cursor is advanced to the next tab stop position. By default tab stops are 10 characters wide. If the current cursor position is in the first column (1), and a value is output that is less than ten characters in length, and a comma is used, then the cursor will be advanced to column 10 before the next value is printed. If the value that was output was, say, 15 characters in length, then the cursor would have been advanced to the next tab stop position, that is, column 20.

Commas and semicolons can be used together in the same PRINT statement, as shown in the following example:

10 PHONE$ = "555-1212"
20 PRINT "Name", "Age", "Phone"
30 PRINT "Alice", 25, PHONE$; " Ext. 123"
40 PRINT "Bob", 19, PHONE$; " Ext. 456"
50 PRINT "Carol", 63, PHONE$; " Ext. 789"
RUN
Name     Age       Phone
Alice    25        555-1212 Ext. 123
Bob      19        555-1212 Ext. 456
Carol    63        555-1212 Ext. 789

Note: The size of the tab stops can be changed using the OPTION TABWIDTH statement.

If a trailing comma is used at the end of the expression list, the behavior is similar to the trailing semicolon, except that the cursor will be advanced to the next tab position and left there without a carriage return. Subsequent PRINT statements will continue writing at that position.

10 PRINT "Hi",
20 PRINT "there"
Hi       there

Note: In many BASIC dialects, numeric values are printed with a leading space, unless the number is a negative number. This unusual feature is ostensibly included to allow numbers with and without minus signs to "align" when printed in columns, but it leads to either awkward "double spaces" when positive numbers are output in a PRINT statement, or missing spaces when a negative number is output. ReadyBASIC avoids this problem by not printing leading spaces before positive numbers. Should the programmer need to include leading spaces, he or she can always test whether the number is positive and print a space as needed.

Note: In some BASIC dialects, an question mark (" ? ") can be used as an abbreviation for the PRINT statement. This is not presently supported by ReadyBASIC.




Copyright 2006-2008, Kevin Matz, All Rights Reserved.