Contents / Part 2 / Previous chapter / Next chapter / Index


10 PRINT formatting and cursor control

This section describes the PRINT statement which is used to put text on the screen or to a printer. It assumes that you understand that a variable (such as X) can be used to hold a number and that a string variable (such as A$) can be used to hold a line of text.

The following program will help to illustrate some of the ideas. Press BREAK and then type in the following program.

10 X=8
20 A$="HELLO"
30 PRINT X, X, X

When this is run it produces this:

>RUN
         8         8         8

This shows that commas separating items in the print list (the print list is the list of things to be printed - X,X,X in this case) will force items to be printed in columns or 'fields' ten characters wide. Numbers are printed at the right hand side of each column whereas words are printed on the left hand side. You can see the difference if we add some lines to the program.

10 X=8
20 A$="HELLO"
30 PRINT X,X/2,X/4
40 PRINT AS,A$,A$
>RUN
         8         4         2
HELLO     HELLO     HELLO

Field widths in different screen modes

As we said above, the width of each 'field' is automatically set to ten characters when the computer is switched on.

Since the computer can operate in different screen modes, displaying either 20 or 40 or 80 characters to the line, clearly the number of fields which can be displayed on the screen will differ depending on the mode. So try typing in a new line and re running the program above.

5 MODE 5

or, if you have a Model B machine

5 MODE 0

80 character modes 40 character modes 20 character modes
(MODES 0 and 3) (MODES 1,4,6 and 7) (MODES 2 and 5)

Note: the widths of the fields can be altered by the use of a special command @% (see page 70).

So commas between items in the print list always put things in columns or 'fields'. On the other hand semi-colons between items in the print list cause items to be printed next to each other, without spaces:

10 X=8

20 A$="HELLO"

30 PRINT A$; X; A$; X; X

>RUN

HELLO8HELLO88

Of course if the first item is a number it will be printed to the right of a 'field' unless it is preceded by a semi colon.

10 X=8

20 A$="HELLO"

30 PRINT X; A$; A$

>RUN

8HELLOHELLO

or

10 X=8

20 A$="HELLO"

30 PRINT ;X;A$;A$

>RUN

8HELLOHELLO

As well as printing variables and string variables as shown above the computer can print any characters placed in between pairs of inverted commas exactly as they have been typed in, providing they are in a print statement. The next program asks for your name and remembers it in the string variable N$.

10 PRINT "WHAT IS YOUR NAME ";

20 INPUT N$

30 PRINT "HELLO ";N$;". HOW ARE YOU?"

>RUN

WHAT IS YOUR NAME ?JOHN

HELLO JOHN. HOW ARE YOU?

Notice the semi-colon at the end of line 10 that makes the computer stay on the same line while it waits for you to provide it with a value for N$. Without the semi-colon this happens:

>RUN

WHAT IS YOUR NAME

?JOHN

HELLO JOHN. HOW ARE YOU?

Note also the space after the word HELLO and before the word HOW in line 30. Without these spaces the words run together to produce.

HELLOJOHN.HOW ARE YOU?

It is quite legitimate to do calculations in a print list - for example

10 X=4.5
20 PRINT X,X+2,X/3,X*X
>RUN
   4.5      6.5       1.5     20.25

but look what happens if instead of X = 4.5 we put X = 7

10 X=7
20 PRINT X,X+2,X/3,X*X
>RUN
        7         92.33333333        49

because x/3 is 2.33333333 it makes the number move left in the field until it immediately follows the previous field which contains a 9 and appears to give a result 92.33333333, which is misleading. For this reason, amongst others, the next section is important if you want to print out a lot of numbers.

Altering the width of the field and the way in which numbers are printed

It is often useful to be able to specify the width of the field when printing columns of figures or words and also to be able to specify the number of decimal places to which numbers will be printed.

On the BBC Microcomputer this can be done by setting a special 'variable' (called @%) in a particular way. For the moment this must be treated as a bit of 'magic' but, for example, if we write

@%=&20209

then this statement tells the computer to print in a field 9 characters wide, and that numbers will be printed with a fixed number of decimal places - in this case, to 2 decimal places. The following program shows this being used:

 5 @%=820209
10 X=7
20 PRINT X,X+2,X/3,X*X
>RUN
    7.00     9.00     2.33      49.00

For the more technically minded

@% is made up of a number of parts

& 2 02 09
(Means Hexadecimal numbers follow) Format
number 2 i.e.
fixed number
of decimal places
2decimal places field width of 9 characters

@%=&20309 would give Format 2, 3 decimal places and field width of 9 characters.

 5 @%=&20309
10 X=7
20 PRINT X,X+2,X/3,X*X
>RUN
    7.000   9.000    2.333    49.000

If you want 4 decimal places and a field width of 12 you would put the following

 5 @%=&2040C
10 X=7
20 PRINT X,X+2,X/3,X*X
>RUN
      7.0000   9.0000   2.3333   49.0000

The & tells the computer that the numbers which follow are 'hexadecimal' numbers - that is, numbers based not on 10s but on 16s. For the sake of completeness, here is a list of hexadecimal numbers (which include the letters A to F)

Decimal number Hex number
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F
16 10
17 11
1B 12
19 13
20 14

A few points:

  1. The maximum number of significant figures is 9
  2. Format 1 gives Figures as exponential values
  3. Format 2 gives figures to a fixed number of decimal places
  4. Format 0 is the 'normal' configuration
  5. To set the print format back to its normal value (Format 0 and field width 10), set @% =10

TAB(X)

As well as controlling the print layout by using the comma and semi-colon one can use the TAB statement to start printing at a particular place on the screen. You will remember that there can be 20,40 or 80 characters to the line depending on the MODE. MODE 7 has 40 characters. Try this:

10 PRINT "012345678901234567890"
20 F=16
30 REPEAT
40 PRINT TAB(10);F;TAB(15);2*F
50 F=F+1
60 UNTIL F=18
>RUN
012345678901234567890
          16   32
          17   34

TAB(10) prints the value of F ten spaces from the left and then TAB(15) prints the value of 2*F fifteen spaces from the left, on the same line. Note the semi-colon after TAB(10) - this causes the computer to begin printing at that point.

Be sure to place an open bracket immediately after the word TAB. If you leave a space between them, thus: TAB (10) the computer will not understand and will report

No such variable.

If you are beyond the place that you tell the computer to TAB to, for example in position 15 with a request to TAB(10), then the computer moves to the next line and then tabs ten spaces.

Type in this replacement line

40PRINT TAB(15);F;TAB(10);2*F
>RUN
012345678901234567890
               16
          32
               17
          34

TAB(X,Y)

A useful extension of the TAB statement allows print to be placed at any specific character location anywhere on the screen. You will remember that in MODE 7 the text co-ordinates are

This program counts to 1000, printing as it goes

5 CLS

10 Q=1

Z0 REPEAT

30 PRINT TAB(18,5);Q

40 Q=Q+1

50 UNTIL Q=1000

The two numbers in the brackets after TAB represent the X and Y text co-ordinates where printing should start (see also the program on page 132).

Advanced print positioning

Using PRINT TAB(X,Y) allows text etc to be printed in any character 'cell' in the appropriate MODE. In MODE 5 there are 20 cells across the screen and 32 cells (lines) down the screen. Sometimes it is useful to be able to position characters on a much finer grid. The statement VDU 5 enables text to be printed at the exact position of the graphics cursor. The statement MOVE can be used to position text. Note that this will not work in MODE 7. You will remember that the graphics screen is addressed thus in all modes except MODE 7.

A few sums will show that each character cell is 32 graphic units high and, in a 40 character mode such as MODE 4, 32 units wide. Suppose we want to subscript a letter to produce for example the chemical formula H2 this can he done as follows

10 MODE 4

20 VDU 5

30 MOVE 500,500

40 PRINT "H"

50 MOVE 532,484

60 PRINT "2"

70 VDU 4

Note that the letter H is positioned with its top left corner at 500,500. The 2 is then printed one character to the right (532) and half a character down (484). Again the top left of 2 is at 532,484.

A neater way of achieving the same effect is to replace line 50 with

PLOT 0,0,-16

One further feature of the BBC computer which is not normally available on "personal" computers is the ability to superimpose

characters. One obvious use of this facility is to generate special effects such as accents and true underlining. The short program below prints the word role with the accent correctly placed.

10 MODE 4

20 VDU 5

30 X=500

40 Y=500

50 MOVE X,Y

60 PRINT "role"

70 MOVE X+32,Y+16

80 PRINT"^"

90 VDU 4

Once in VDU 5 mode the screen will not scroll up if you reach the bottom of the page, instead the writing will start from the top of the screen again. In addition characters will be superimposed on those already on the screen. When in VDU 5 mode you can only print things in the graphics window and not in the text window, and colour is selected with the GCOL statement. VDU 5 will not work in text-only modes such as MODES 3, 6 and 7.

Cursor control

The text cursor is the flashing line on the screen which shows where text will appear if it is typed in on the keyboard. The text cursor also indicates where text will he printed on the screen by a PRINT statement. The cursor can be moved around the screen by a number of special "control codes" amongst which are

code	effect
8	move cursor left
9	move cursor right
10	move cursor down
11	move cursor up

These code numbers can be used either with the VDU command e.g. to move left four spaces, use either

VDU 8,8,8,8

or

PRINT CHR$(8);CHR$(8);CHR$(8);CHR$(8)

clearly the VDU command is simpler to type in in most cases.

In addition to the codes shown above the user can use the PRINT TAB(X,Y) statement to move the cursor directly to any character position on the screen. As we've seen in MODE 7 the screen can contain up to 25 lines (numbered 0 to 24) of up to 40 characters per line.

The position marked on the diagram above is 18 positions across and 6 lines down. The cursor could be moved directly there with the statement

PRINT TAB(18,6);

Note that the opening bracket must immediately follow the word TAB thus TAB( and not TAB (.

Exactly the same effect can be obtained with the statement

VDU 31,18,6

The cursor can be moved to the "home" position at the top left of the screen with the statement

VDU 30

If the user wishes to clear the screen as well as move the cursor to the home position then he or she can use the statement

VDU 12

The last of the VDU commands directly to do with cursor control is

VDU 127 which moves the cursor left and deletes the character there. If you wish to delete the next four characters and then return the cursor to its initial place you could use

VDU 9,9,9,9,127,127,127,127

Cursor ON/OFF

In some applications the flashing cursor can be a distraction. It can be turned off with the statement

VDU 23;8202;0;0;0;

and can be turned back on with the MODE statement

Note; In version 1.0 of the operating system

VDU 23,1,0;0;0;0; will turn the cursor off

VDU 23,1,1;0;0;0; will turn the cursor back on

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001