Q(uick)BASIC Function: TAB

Quick View

TAB

A device I/O function that moves the print position when used in the PRINT or LPRINT statements

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • TAB(column%)
Description/Parameter(s)
column% The column number of the new print position.
Example
PRINT TAB(25); "Text"
Syntax
  • TAB(column)
Description/Parameter(s)

The argument, column, is a numeric expression that is the column number of the new print position. If the current print position is already beyond column, the TAB function moves the print position to that column on the next line. Column 1 is the leftmost position, and the rightmost position is the current line width of the output device. If column is greater than the output width, TAB wraps the output and the print position becomes 1+(column MOD width). If column is less than 1, TAB moves the print position to column 1.

TAB can only be used in PRINT and LPRINT statements.

Examples

The following example uses TAB to locate columns of output:

FOR I = 1 TO 4 READ A$,B$ PRINT A$ TAB(25) B$ NEXT DATA NAME, AMOUNT,,, G.T. JONES, $25.00, H.L. STEVENS, $32.25

Sample Output:

NAME AMOUNT G.T. JONES $25.00 H.L. STEVENS $32.25

The following example shows the effects of different values used as arguments to TAB:

'Assumes 80-column screen width. PRINT TAB(1287); "one" PRINT TAB(255); "two" PRINT TAB(-5); "three" PRINT "123456789012345678901234567890" TAB(20) "four"

Sample Output:

one two three 123456789012345678901234567890 four
Syntax
  • TAB(column%)
Description/Parameter(s)

Usage Notes

  • The leftmost print position is always 1. The rightmost position is the current line width of the output device (which can be set with the WIDTH statement).
  • Here is an example of using TAB in the PRINT function:
    • PRINT TAB(10); City$; TAB(40); State$; TAB(45); Zip$
  • The behavior of a TAB function depends on the relationship between three values: column%, the current print position on the current output line when the TAB function is executed, and the current output-line width:
    • If the current print position on the current line is greater than column%, TAB skips to column% on the next output line.
    • If column% is less than 1, TAB moves the print position to column 1!
    • If column% is greater than the output-line width, then TAB calculates
      • print position = (column% MOD width).
      If the calculated print position is less than the current print position, the cursor jumps to the next line at the calculated print position. If the calculated print position is greater than the current print position, the cursor moves to the calculated print position on the same line.
Example

This example uses the TAB function with the PRINT and PRINT USING statements to center a text string and position a page number at a particular location on an 80-column screen.

CLS INPUT "What is your full name "; Name$ PRINT PRINT "Do you want the page number <C>entered or <R>ight-justified? " DO Answer$ = UCASE$(INKEY$) LOOP UNTIL Answer$ = "C" OR Answer$ = "R" CLS PageNumber% = INT(RND * 10) 'Divide the length of Name$ by 2 and subtract from screen center point. PRINT TAB(40 - (LEN(Name$) \ 2)); Name$ LOCATE 23, 1 IF Answer$ = "C" THEN PRINT TAB(35); USING "Page ##"; PageNumber% ELSE PRINT TAB(72); USING "Page ##"; PageNumber% END IF END