PRINT USING STATEMENT
ThePRINT USING statement allows greater flexibility in formatting printed output. You can "dress up" your output. You can define the format you want with a format statement, then use a PRINT USING statement to print the output in accordance with the specifications in the format line. The general structure of the PRINT USING statement and format line is:
The PRINT USING statement, line 30, specifies line 100 is to be used as theFORMAT line for printing variable Y. Line 100 specifies the number of positions to be printed and the format in which they are to be printed. For example, if Y = 25.782, it would print as $25.78. If it were 05.3753, it would print as $5.37.
The PRINT USING statement, line 40, specifies line 110 is to be used asits format line. Line 110 is a different type of format line. It specifies where the literal, MORTGAGE AMORTIZATION SCHEDULE, is to be printed. Literals are words or phrases in a format statement that are to be printed exactly as they appear in the format statement.
The per cent sign used as the first character in lines 100 and 110 indicatesthat these are format lines. Some computers use a colon; some use the letters FMT. Refer to your userís reference manual for applicable characters.
Format control characters within a format line are used to describe theoutput images desired, and to control spacing and positioning of data in the output line. The types of control characters and their syntax may vary depending on the computer you are using. However, they usually include #, $, comma(,), and decimal point(.).
When specifying an output image, be sure to allow enough positions forthe largest possible number or character string in that field. If you fail to allow enough positions in a given field, either truncation of some of the data will occur; or, you will get an error message indicating the field is too small. You should pay close attention to this possibility and ensure it doesnít happen.
Format lines may contain a combination of literals and output images.However, if a format line only specifies literals (i.e., headings), it will not contain format control characters.
The PRINT USING statement includes the line number of the formatstatement followed by any variables or expressions to be printed. The items in the PRINT USING statement are separated by commas. However, if you are printing only literals, the PRINT USING statement will contain only the line number of the format statement.
Now that we have seen the mechanics of the PRINT USING statement,letís use it to modify the mortgage amortization program to print the output in a more realistic format.
Examine the PRINT USING statements in lines 30, 110, and 150. Eachof these statements refers to another line number, which is the format state-ment where the format of the output is described. The PRINT USING state-ment in line 30 causes the information in line 40 to be printed exactly as it appears.
The PRINT USING statement, line 110, causes the values for the variableslisted to be printed according to the images specified in line 120. In format line 120, we have reserved three spaces, ###, for month, which is represented by the first variable name, M, in line 110. Next $#,###.## reserves space for the payment (variable D). Then $###,###.## reserves space for the loan balance. The next two images are for principal and interest and are the same structure as the image for payment. Note the use of the dollar sign, comma, and decimal point in the images for payment, loan balance, principal and interest; these will appear in the output as appropriate. Also, note that .005 was added to variables B, P, and A in order to round them.
Now examine the PRINT USING statement in line 150 and its associatedformat line, line 160. Line 160 contains a literal and an image specification. The literal will be printed exactly as it appears in the format line, and the rounded value for variable B will be Printed in the specified positions including the dollar sign, comma, and decimal point as appropriate.
The output from the modified program would look like this:
Both theTAB function in a PRINT statement and the PRINT USING statement allow you greater flexibility in formatting your printed output than the spacing automatically generated by a PRINT statement.
With a TAB function you can specify exactly where you want an item tobe printed in an output line. It causes the computer to tabulate to a specified print position. The semicolon rather than the comma is generally used as a separator in PRINT statements containing TAB functions.
The other way to enhance printed output is with the PRINT USING state-ment. It allows even more flexibility than the TAB function. With it, you can print literals or print the values of variables (or expressions) in a given format, specified in a format line. It allows you to line up your output sothat it is more meaningful and easier to read. Literals specified in a format line are printed exactly as they appear. The values for variables are printed in the location and image specified in a format line. Format control characters are used to describe output images and to position data in the output line. When specifying images, be sure to allow enough spaces for the largest possible number or character string in that field.