Classic Computer Magazine Archive COMPUTE! ISSUE 78 / NOVEMBER 1986 / PAGE 87

The Beginners Page

C. Regena

More About PRINTing

We started exploring the PRINT command last month. Let's now move on to look at ways to format text. Some of the commands we'll discuss are not identical on all versions of BASIC, so you'll need to experiment to see if they work on your computer, and, if so, what parameters (numbers and limits) are allowed. Try them out; it won't hurt the machine if it doesn't recognize a command. Your computer will just reply that you've made an error.

WIDTH is a command in some versions of BASIC that controls how many characters can be printed on a line across the screen. On some computers (such as an IBM PCjr), this command also determines the size of the characters or the resolution of the screen. For example, WIDTH 40 is a 40-character line with larger letters and medium resolution, and WIDTH 80 is an 80-character line with smaller letters. On the IBM PCjr, WIDTH 20 is a 20-character line in the low-resolution screen. Note that on the PCjr, changing the WIDTH also clears the screen.

On other computers (such as the Amiga and Atari ST), WIDTH n specifies n number of characters in the printed line, where n can be any number you wish to use to control margins. The size of the letters does not change. On computers with windows, it's possible to print beyond the visible portion of the window, so WIDTH is handy to keep the printing within the window. Here's an example using the WIDTH command:

10 A$="1234567890"
20 WIDTH 18
30 PRINT A$+A$+A$+A$
40 END

Specifying Location

Many versions of BASIC include commands for specifying where printing will be positioned on the screen. In BASIC for the IBM and for the Amiga, use LOCATE row,column to position the cursor, followed by PRINT to start printing:

80 LOCATE 5,10:PRINT "TITLE"

In Atari ST BASIC, use GOTOXY column,row to position the cursor, and then PRINT. Notice that this computer specifies the column number first, then the row number, and the upper left corner of the output window is 0,0. Also, the position of the printing will be slightly different than if you PRINT blank lines and then TAB over to a certain column:

80 GOTOXY 10,5:PRINT "TITLE"

Commodore BASIC has no special statements for positioning the cursor, but you can use cursor control characters within quotes to move up, down, left, and right. You can also use the TAB function, such as:

80 PRINT TAB(168);"HELLO"

The maximum number of character positions you can move with TAB is 255.

Create A Template

PRINT USING can be a real time-saver in specialized situations, but not all versions of BASIC offer it. The syntax varies slightly with brands of computers, so refer to your manual and experiment a little to see how this command works.

The main purpose of PRINT USING is to format your output— line up numbers or strings or perhaps print money amounts. If you print large numbers, you can use PRINT USING to place commas every third column for place values. You can print plus or minus signs for positive or negative numbers. You can print leading asterisks. You can use this command to round off numbers or to print to a certain number of decimal places even if there are trailing zeros. Here are some examples:

10 A=123.4567
20 B = 64
30 C = .3
40 D = 8.25031
50 PRINT USING "###";A
60 PRINT USING "###";B
70 PRINT USING "$$###.##";A
80 PRINT USING "$$###.##";B
90 PRINT USING "$$###.##";C
100 PRINT USING "###–";D
110 PRINT USING "###.# A ";A,B,C,D
120 PRINT USING "**###.##";C
130 END

You can also print strings with PRINT USING. The specifications vary with the brand of computer, so check your manual. Usually the exclamation point will print the first letter of a string, and backslashes or spaces between backslashes print certain numbers of characters in a string. The Atari ST allows characters (such as dots or numbers) between the backslashes to help you count how many characters can be printed. Here are some examples with strings:

10 S$="RICHARD
20 PRINT USING "!";S$
30 PRINT USING " \ \";S$
40 PRINT USING "HIS NICKNAME IS\ \";S$
50 PRINT USING "HIS INITIAL IS !.";S$

Review the last several "Beginner's Page" columns about strings. Using string control features with PRINT statements gives you significant control over the output of your computer. PRINT USING is very handy in printing columns of numbers for reports. You can also print lists such as name and address lists with columns lined up. Use a combination of TAB, SPC, and PRINT USING to get your reports to look precisely the way you want them to look.

If your computer has color, be sure to use color in printing on the screen to highlight certain words or to add variety to the output. The color statements vary with the computer. The following are a few examples:

10 REM ATARI ST
20 COLOR l.PRINT "NORMAL"10 REM ATARI ST
20 COLOR 1:PRINT "NORMAL"
30 COLOR 2:PRINT "RED
40 COLOR 3:PRINT "GREEN"
50 COLOR 1
60 END
10 REM IBM
20 COLOR 4:PRINT "RED
30 COLOR 2,7:PRINT "GREEN ON WHITE"
40 COLOR 0,7:PRINT "BLACK ON WHITE"
50 COLOR 7,0:PRINT "RETURN TO WHITE ON BLACK"
60 END
10 REM AMIGA
20 COLOR 2,3:PRINT "BLACK ON ORANGE"
30 COLOR 3,2:PRINT "ORANGE ON BLACK"
40 COLOR 0,l:PRINT "HELLO"
50 COLOR l,0:PRINT "BACK TO NORMAL"
60 END
10 REM COMMODORE 64 AND VIC-20
20 REM USE CTRL AND A NUMBER
30 PRINT "{RED} HELLO {PUR}BOB"
40 REM USE RVS ON FOR INVERSE COLORS
50 PRINT "{WHT}{RVS}TRY THIS"
60 END

Commodore computers allow certain graphics symbols in PRINT statements to draw with built-in characters. You can use either the graphics key or print CHR$(n) where n is a character number for a certain graphics character.

With all these options available for the PRINT command, even the beginning BASIC programmer can create elaborate computer effects by "just" printing.