Wednesday, June 23, 2010

Comments and REMarks

REM is the QB keyword for REMark. REM statements are ignored entirely by QB. They are only there for programmers and anyone else who reads your BASIC source code.

Your computer would be just fine if you programmed it in assembler or machine code, but would you? BASIC is an easy to understand language with simple English keywords, but even BASIC programs can look cryptic and confusing without proper comments.

You can also use apostrophes to begin a comment. Unlike the keyword REM, you can place this second form of REMark after other BASIC statements.

Take a look at REMarks.bas to get an idea of how comments work. Run the code to see how QB ignores all REMarks.
'      O------------------------O
' | REMarks.bas |
' |REMarks.bas demonstrates|
' |the use of comments and |
' | REMarks in QB |
' | |
' | KEYWORDS: |
' | REM |
' | ' |
' O------------------------O

PRINT "This line gets printed."
REM "And this line does not."

PRINT "These words are printed, " ' and these are not.


REMarks.bas

The output for REMarks.bas should be:
This line gets printed.
These words are printed,


Note that you can place an apostrophe comment after the PRINT statement (or any other executable statement) but you can't place an executable statement after an apostrophe.

Different programmers use REMarks in different ways. You will see all of my programs start with a text box of sorts with each line beginning with an apostrophe. These text boxes contain information about the program. Similar, less ornate comments appear throughout my source code explaining what is happening in SUBs, FUNCTIONs, and wherever I feel the need.

You can use REMarks to explain what a SUB or FUNCTION is for, like this:
REM This function calculates collision damage.

When I first started looking at professional source code I noticed many inside jokes and general sillyness inside source comments, function names, and variable names. At first I thought it was annoying, and then I realized it helped programmers "play" with the variables, code, and functions. This kind of play increases a programmer's interest in the code and this in turn increases creativity and understanding of the code. So don't be surprised when you see references in other people's code to Monty Python, the Hitch Hiker's Guide, or game characters named superSpeedyBadass.

Wednesday, June 2, 2010

PRINT Statement

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

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

Example:

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!"

PRINT

hello.bas

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$

hello2.bas

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$

hello3.bas


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