Get really spaced out with a touch of TAB
THIS month we're going to learn how to space characters out
on the screen with the use of the Basic statement TAB( ).
You can think of TAB as standing for tabulation which, if you
know anything about typewriters, should give you a good idea of
what it does -TAB allows you to pick the column your PRINT statement
works in.
For example, with your computer in Mode 6, try:
PRINT TAB(10) "HELLO"
You'll notice that HELLO gets printed several columns from the
side.
The number in brackets tells you the number of the column that
printing will begin in. Beware though, for — as in so many other
things in computing — you begin counting with the number 0.
This means that the first column is column 0, the second column
1 and so on.
In the example above, HELLO would have started in the eleventh
column.
The exact number of columns per line varies from mode to mode
— we'll cover the exact differences shortly. In Mode 6, there
are 25 lines of 40 columns each, so the rightmost column on any
line would be given by TAB(39).
TAB(40) would "wrap the printing round" to the next
line. Program I illustrates the ideas - have a good look at it.
10 REM *** PROGRAM I ***
20 MODE 6
30 PRINT TAB(0)"*"
40 PRINT TAB(10)"*"
50 PRINT TAB(20)"*"
40 PRINT TAB(30)"*"
70 PRINT TAB(39)"*"
80 PRINT TAB(40)"*"
One point you have to watch is that the bracket must directly
follow TAB. Using TAB ( ) instead of TAB( ) will cause all sorts
of problems.
Also, try experimenting with TAB numbers bigger than the line
length. Then try 256 - you might be surprised at the results as
256 is a special number in computing. When we reach it, we often
go back to 0 again.
We can achieve really powerful results by putting a loop variable
between the brackets of a TAB statement. Program II uses this
idea to obtain a diagonal line of asterisks down the screen.
10 REM *** PROGRAM II ***
20 MODE 6
30 FOR loop%=0 TO 23
40 PRINT TAB(loop%)"*"
50 NEXT loop%
Can you see a way of reversing the direction of the diagonal
line? Think for a moment before reading on.
What you have to do is start with the asterisks across to the
right of the screen, then gradually move them left.
This will entail starting with a high column number in the TAB
statement and working down to a low one.
We can do this by altering the loop variable so that it decreases
by one each time, starting with the high number. So, we change
line 30 to:
30 FOR loop%=23 TO 0 STEP -1
Nice as it is to be able to specify where on a line the asterisk
should appear, it would be better still if we could pick the line
it appears on as well.
We can do this with the TAB statement, by giving it two numbers
inside the brackets, separated by a comma.
As before, the first number denotes the column. The second number
tells us which line or row of the screen it will appear on. Try:
PRINT TAB(5,10) "HELLO"
and all will be revealed.
If you're into mathematics, you can think of the Mode 6 screen
as divided into a 40 by 25 grid, the first number inside the brackets
being an X co-ordinate and the second the Y co-ordinate. Even
if you're not mathematical, Figure I should still make sense.
Remember that in computing we start counting at zero, so, just
as the first column is specified by zero, so the first line is
given by zero.
This means that TAB(0,0) refers to the top left-hand corner
of the screen. Program III should make things clearer.
10 REM *** PROGRAM III ***
20 MODE 6
30 PRINT TAB(0,0)"*"
40 PRINT TAB(5,5)"*"
50 PRINT TAB(10,10)"*"
60 PRINT TAB(5,15)"*"
70 PRINT TAB(0,20)"*"
A point to watch is that, unlike TAB with just one number, TAB
used in this way does not "wrap around" if your numbers
are too large. Try:
PRINT TAB(45,45) "TEST"
to see what happens.
We can use this form of TAB to print out our diagonal of asterisks,
as in Program IV.
10 REM *** PROGRAM IV ***
20 MODE 6
30 FOR loop%"0 TO 20
40 PRINT TAB(loop%,loop%)"*"
50 NEXT loop%
What do you think will happen if we change line 30 to:
FOR loop%=20 TO 0 STEP -1
Have a good think about it before you run it, remembering that
we made a similar alteration to Program II.
Did you predict the outcome? Can you see what's going on? It's
well worth thinking about.
If we really want to draw the other diagonal we should ensure
that the column number starts at the high number, then "moves
in" as the row number increases by one each time.
10 REM *** PROGRAM V ***
20 MODE 6
30 FOR loop%=0 TO 20
40 PRINT TAB(20-loop%,loop%)"*"
50 NEXT loop%
Line 40 in Program V achieves this by subtracting the loop variable
loop% from 20 and using it to "index" the column number.
The first time through the loop, the column number is 20, the
row number 0. The next time the column number is 19, the row number
1 and so on.
You can stack TAB statements one after another following a print
statement. Line 40 of Program VI uses this idea to use one loop
to draw two parallel lines of asterisks.
10 REM *** PROGRAM VI ***
20 MODE 6
30 FOR loop%=0 TO 20
40 PRINT TAB(5,loop%)"*" TAB(35,loop%)"*"
50 NEXT loop%
You might like to use the techniques we've covered this month
to produce the shape shown in Figure II with asterisks. You'll
certainly master the ideas if you do!
Last month we made some triangles of asterisks. This month we're
going to be producing some more. Only this time we're going to
use the TAB statement to indent the rows of asterisks from the
side of the screen.
We want the triangles to appear more like Figure IIIb rather
than Figure IIIa, which we did last month.
Program VII does the trick.
10 REM *** PROGRAM VII ***
20 MODE 6
30 FOR loop%=0 TO 10
40 asterisk$=asterisk$+"*"
50 PRINT TAB(10-loop%,loop%) asterisk$
60 NEXT loop%
in line 50 we use the same trick as in Program V, subtracting
the loop variable from an "offset" - this time 10 -
to provide the indent.
Each time through the loop we add one asterisk to the string,
asterisk$ that we are printing.
You might try to alter the program so that the triangle is printed
upside-down.
Finally, try Program VIII. This, too, prints out another triangle
of asterisks.
10 REM *** PROGRAM VIII ***
20 MODE 6
30 asterisk$="*"
40 FOR loop%=0 TO 10
50 PRINT TAB(10-loop%,loop%) asterisk$
60 asterisk$=asterisk$+"**"
70 NEXT loop%
Can you work out what's happening? Why add two asterisks in
line 60?
Once you've "cracked" it you can try to turn this
triangle upside down, as well.

Figure I: Column and Row values in Mode 6
MIKE BIBBY