The Beginners Page
The Many Faces Of PRINT
I am happy to be taking over "The Beginner's Page" from Tom Half-hill, who has assumed new responsibilities as editor of COMPUTE!'s Atari ST Disk & Magazine. Since buying my first computer in 1980, I have written hundreds of BASIC programs and have accumulated several newer machines (most recently, an Atari ST). So I have been a "beginner" several times. My goal for this column is to help you learn to program in BASIC on your own computer—and to enjoy doing it. Although each brand of computer has its own quirks, all versions of BASIC share many similarities; this column will focus on broad concepts that apply to all home computers.
This month let's look at one of the most important commands—the PRINT statement. PRINT used by itself prints a blank line on the screen. PRINT may be followed by items to be printed, either variables (using string variable or numeric variable names) or constants (actual numbers, or characters enclosed in quotation marks). You may also print the product of a BASIC function, such as the tangent of an angle or a segment of a string. Many computers allow you to abbreviate the keyword PRINT with a question mark (?).
Printing Multiple Items
If you include more than one item in a PRINT statement, the items may be separated by a special character—usually a comma or semicolon—known as a delimiter. Try these commands:
PRINT "HELLO", "FRIENDS" PRINT "ME"; "AND"; "YOU"
Notice the difference in the results. On most computers, the comma positions the next item in the next print column. The column width is predefined (different types of computers may use different column widths). The semicolon prints one item right after the other. If you need spaces between words, you can include a space inside the quotation marks as shown here:
PRINT "ME"; " AND "; "YOU"
In some versions of BASIC, you can print multiple items without any delimiters at all, which is the same as using a semicolon. On Commodore computers, for instance, the statement PRINT A$"HI" works the same as PRINT A$;"HI".
When a delimiter falls at the end of a PRINT statement, it affects the next PRINT statement. This method is useful when you want to print something that doesn't fit conveniently into one program line.
100 FOR T = 1 TO 5 110 READ N$ 120 PRINT N$; " "; 130 NEXT T 140 DATA ED, BILL, JOHN, JIMMY, RI CHARD
The TAB function mimics the operation of a tab key on a conventional typewriter, allowing you to move to a certain column before printing. The number in parentheses indicates the column where printing begins (some computers start with column 0; others start with column 1). Here are some examples:
PRINT TAB(8); "INDENT TO HERE" PRINT TAB(5); L$; TAB(15); F$ PRINT TAB(T); A; TAB(T + 8); B; TAB(T + 16); C
Some computers let you skip screen lines by using a large value with TAB. For example, on a 40-column Commodore computer the statement TAB(85) skips two 40-column lines and indents five spaces. When you print numeric values, keep in mind that the computer adds space before the number to allow for a sign. If the number is negative, a minus sign (-) appears before the number. If the number is positive, an extra blank space appears. If you use TAB with a numeric value, don't forget to allow for these extra spaces.
You may prefer to move the cursor by printing actual spaces. The SPC function prints the number of spaces indicated by the value in parentheses. The difference between TAB and SPC is that TAB usually moves the cursor column without printing anything in the intervening area, but SPC prints spaces.
PRINT "SCORE"; SPC(5); SC PRINT "JEFF"; SPC(8); "JILL" PRINT TAB(T); X$; SPC(14); Y$
Closely related to the SPC function is the SPACE$ function—available in more advanced BASICs like those for the IBM, Amiga, and Atari ST—which creates a string consisting of the number of spaces specified in parentheses.
S$ = SPACE$(15) PRINT "ONE";S$;"TWO"
A string made by SPACE$ can also be concatenated (combined) with other strings.
S$="ONE" + SPACE$(20) + "TWO" PRINT S$
STRING$ is another useful function of the more advanced versions of BASIC. It works like SPACE$, but allows you to create a string using any ASCII character. The first value enclosed in parentheses is the number of characters desired in the string, and the second item can be either an ASCII value or a character inside quotation marks. For example, you can print a string of 12 asterisks with either STRING$(12,42) or STRING$(12, "*").