15.2 Using Format Strings
Format strings determine the way in which items are to be
printed in the output file. Format strings can be any of the
following:
.
String variables
.
String literals
.
Named string constants
.
A combination of the previous strings
The PRINT USING statement must contain one or more
format strings. Each format string is made up of one for-
mat field . Each format field controls the output of one print
item and can contain only certain characters, as described
throughout the chapter.
The PRINT USING statement must also contain a list of
items you want printed. To format print items, you must
separate them with commas or semicolons. Separators be-
tween print items do not affect output format as they do with
the PRINT statement. However, if a comma or semicolon
follows the last print item, BASIC does not return the cursor
or print head to the beginning of the next line after it prints
the last item in the list.
When BASIC encounters an invalid character within the
current format field, it automatically ends the format field.
Therefore, you do not need to delimit format fields. The
character that terminates the previous field can be either a
new format field or a string literal.
In the following example, the first three characters in the
format string (###) make up a valid numeric format field.
The fourth character ( A ) is invalid in a numeric format
field; therefore, BASIC ends the first format field after the
third character. BASIC continues to scan the format string,
searching for a character that begins a format field. The first
such character is the number sign at character position 7.
Therefore, the characters at positions 4, 5, and 6 are treated
as a string literal. The characters at positions 7, 8, and 9
make up a second valid numeric format field.
PRINT USING "###ABC###", 123, 345
Output
123ABC345
When the statement executes, BASIC prints the first number
in the list using the first format field, then prints the string
literal ABC, and finally prints the second number in the list
using the second format field. If you were to supply a third
number in the list, BASIC would reuse the first format string.
This is called reversion .
PRINT USING "###ABC###", 123, 345,
564
Output
123ABC345
564ABC
Because any character not part of a format field is printed
just as it appears in the format field, you can use a space or
multiple spaces to separate format fields in the format string
as shown in the following example:
DECLARE STRING CONSTANT format_string = "###.## ###.##"
DECLARE SINGLE A,B
A = 2.565
B = 100.350
PRINT USING format_string, A, B, A, B
Output
2.57 100.35
2.57 100.35
When the BASIC compiler encounters the PRINT USING
statement, BASIC prints the value of A (rounded according
to PRINT USING rules), three spaces, then the value of B .
BASIC prints the three spaces because they are treated as a
string literal in the format string. Notice that when BASIC
reuses a format string, it begins on a new line.