Custom Search
 
  

 
CHAPTER 6

FORMATTING PRINTED OUTPUT

PRINT, TAB, PRINT USING, and Format statement

In Chapters 3 through 5, we have explored the use of statements to get data into a program either using the READ and DATA statements or the INPUT statement. We have seen how to assign values to constants and variables, print the results of calculations, create and control loops, construct arrays, define and use functions, and write subroutines. However, our flexibility in printing output has been limited to the spacing automatically generated by a PRINT statement in BASIC. In this chapter, additional features generally available for printing output will be presented. These are the TAB function and the PRINT USING statement.

TAB FUNCTION

The TAB function allows output to be printed in any location of an output line. You are not limited to the standard spacing. given by a comma, or the packed spacing given by a semicolon in a PRINT statement.

When a TAB function is used in a PRINT statement it must immediately precede the constant or variable to which it applies. The expression or number in parenthesis specifies the print position in which printing is to begin.

Example:

This PRINT statement with TAB functions causes the value represented by the string variable C$ to begin printing in print position 10, the value of the numeric variable A to begin printing in print position 30, and the value of the string variable D$ to begin printing in print position 40.

Care must be taken in specifying print positions with the TAB function because once a print position is passed, the printer does not backspace. It goes to the specified position on the next print line producing undesired results. For this reason, semicolons rather than commas are generally used as separators in PRINT statements with TAB functions. Remember, semicolons ignore print zones while commas are used for standard spacing (printing in zones). Therefore, it is recommended you use semicolons as separators when using the TAB function in PRINT statements.

The TAB function is useful in formatting various types of printed output. One use is in formatting headings for reports. The following example shows how this could be done.

Example:

In this example the report heading is to be centered on the page with the column headings spaced under it. By using a form like this you can get an idea of what the output will look like. Then when you code the TAB functions, use the position numbers to indicate where printing is to begin.

In lines 60 through 90 the PRINT statements with the TAB functions are used to format the report and column headings. Using the TAB function enables you to print these headings anywhere you want them on the output page. The output would look like this:

The form width for this example was sixty-two (62) positions. A formula that can be used to aid you in centering printed output is: 

This will give you the number of blank print positions to be left on each side of the heading. If it works out that you have an unequal number of positions before and after the heading, the extra position can be put after the heading. However, this is left to your discretion.

In the example, the heading S-7 DIVISION RECALL BILL had 24 characters. Using the formula, we can determine the number of spaces to the left of the heading; (62 24)/2 = 19. Therefore, the first print position will be 20; this number is then used in parentheses following the TAB function.

Example:

The column headings can be formatted in any way you want them. First determine how many print positions you want to allow for each data field, then determine how many characters the column headings will have. If you want the column headings centered over the data fields, use the same formula as for centering headings on the page, except substitute the number of print positions in a given field for total print positions. For example, the NAME field has 26 positions and NAME has four characters, thus (26 4)/2 = 11. Therefore, we start printing NAME in print position 12. For the remaining fields, you first determine how many spaces you want to leave between fields; this will give you the beginning position of each field. Now repeat the formula for determining the position of the column headings.

Lets examine a program which includes the TAB function to format the output. Well use the mortgage amortization program. For the output, we want to print headings for MONTH, PAYMENT, LOAN BALANCE, PRINCIPAL, and INTEREST. Then we want to print the data in columns under the headings. A program to do this could be written this way:

Example:

In this example the TAB functions caused the computer to tabulate to the specified print positions and print whatever values followed each TAB function. In the PRINT statements, lines 30 and 40, the values were the column headings; in lines 110 and 120, dollar signs followed by the values of the variables were printed, beginning in the specified print positions. The space between the dollar sign and the number is reserved for the minus sign, if the number is negative.

In the previous example, the headings look nice, but look at the numbers. You would not want an amortization schedule with cents expressed to five or more decimal places, and without commas to make the dollar values easier to read. Although we used the TAB function to specify where the column should be printed, we let the computer format each output field automatically. By using the PRINT USING statement we can override the print-zone restrictions and further enhance the appearance of the output.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business