Wednesday, June 2, 2010

PRINT Statement

PRINT Statement
Syntax: PRINT [expression list] [{,|;}]...

Description: Prints text to the screen at the cursor location.


PRINT "hello world!"

Standard Use

If your program displays text on the screen for users, you probably want to use the PRINT statement. The first form of PRINT we will look at has the following syntax:
PRINT expression

This simple form of the PRINT statement can be seen in hello.bas, which is our "Hello World" program (the standard first program in any programming language).
'      O------------------------------------------O
'      |                hello.bas                 |
'      |   This is the standard first program     |
'      |in any programming language. This program |
'      |   prints the words "Hello World!" to     |
'      |               the screen.                |
'      |                                          |
'      |                KEYWORDS:                 |
'      |                  PRINT                   |
'      O------------------------------------------O

PRINT "Hello world!"



This program prints the words "Hello World!" to the screen.

String Variables

The text in quotes in hello.bas is called a "string literal". It is not assigned a place in memory and there is no way for you to call this string anywhere else in the program or to change this string as the program is running. PRINT displays string literals just as they appear in code (minus the quotes). PRINT can also display string string variables.

In our next program, hello2.bas, we assign a string to a string variable and then use the PRINT command to print the contents of the variable to the screen.

'      O-----------------------------O
'      |         hello2.bas          |
'      |  hello2.bas uses a string   |
'      | variable to print the words |
'      |"Hello World!" to the screen.|
'      |                             |
'      |          KEYWORDS:          |
'      |             DIM             |
'      |            PRINT            |
'      O-----------------------------O

DIM hello$
hello$ = "Hello world!"

PRINT hello$


In hello2.bas, we assign a string ("Hello World!") as a value to a string variable (hello$). We can change the value of hello$ anywhere in our program.

In hello3.bas, we have one string variable that holds a series of strings. Each PRINT statement is the same, but as the value in the string variable changes, so does the text printed to the screen.
'      O-------------------------O
'      |       hello3.bas        |
'      |hello3.bas uses a string |
'      |variable to print strings|
'      |     to the screen.      |
'      |                         |
'      |        KEYWORDS:        |
'      |           DIM           |
'      |          PRINT          |
'      O-------------------------O

DIM hello$

hello$ = "Hello Canada!"
PRINT hello$

hello$ = "Hello world!"
PRINT hello$

hello$ = "Hello universe!"
PRINT hello$

hello$ = "What's the frequency, Kenneth?"
PRINT hello$


Full Syntax

The use of QuickBASIC's PRINT statement in hello.bas demonstrates how PRINT can display information on the screen, but does not show the real potential of PRINT. In hello.bas, PRINT displayed the string literal "Hello world!", but PRINT can also display the results of simple or complex expressions involving variables, constants, math, and string concatenation. The full syntax of the QuickBASIC PRINT statement is:
PRINT [expression list] [{,|;}]...

Numeric Literals

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.