07.05.2015 Views

Download - True BASIC

Download - True BASIC

Download - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

22 <strong>True</strong> <strong>BASIC</strong> Language System<br />

When run, this program produces the unexpected output:<br />

Answer 31 is<br />

The reason for this output is quite simple: the pound sign before the digit 1 (intended to represent the number sign)<br />

is interpreted as the first numeric field. Therefore, <strong>True</strong> <strong>BASIC</strong> rounds the value of 2.6 to 3 and prints it in this<br />

field. Other characters, including the 1, are printed as constants. Since no second print item is available for the<br />

second field (which we intended to be the only field), the format string is printed only to the first character in that<br />

field.<br />

Now consider the following potential solution:<br />

PRINT “Answer #1 is “;<br />

PRINT USING “##.#. Good job!”: 2.6<br />

END<br />

Since the problematic pound sign is no longer part of the PRINT USING statement, this form of the program<br />

solves that part of the problem, but it also brings up another problem. When you try to run this program, you get<br />

an error message claiming that you have a “Badly formed USING string.” This means there is now something illegal<br />

in the format string.<br />

This problem is a little trickier to find, but it makes sense. The second period (intended to end the first sentence)<br />

is directly adjacent to the format field. Since it is a valid place holder, <strong>True</strong> <strong>BASIC</strong> considers it part of the field.<br />

However, this results in a field with two decimal points which is not possible, so <strong>True</strong> <strong>BASIC</strong> generates the error.<br />

(This error didn’t occur in the first example because <strong>True</strong> <strong>BASIC</strong> stopped using the string before it reached that<br />

point.)<br />

To fix this problem, modify the program as follows:<br />

PRINT “Answer #1 is “;<br />

PRINT USING “##.#”: 2.6;<br />

PRINT “. Good job!”<br />

END<br />

When you run this version of the program it produces the output originally intended:<br />

Answer #1 is 2.6. Good job!<br />

Notice that a semicolon at the end of a PRINT USING statement has the same effect as at the end of a PRINT<br />

statement.<br />

String Format Fields<br />

<strong>True</strong> <strong>BASIC</strong> also lets you format string values with the PRINT USING statement, but the options are more limited.<br />

The PRINT USING statement with string values is most useful if you wish to define fixed length fields and<br />

control the justification (alignment) of the string values within those fields.<br />

You may print strings with any numeric format item. Unless you specify otherwise, <strong>True</strong> <strong>BASIC</strong> centers the string<br />

within the field, adding spaces if needed. (If necessary, there will be one more space to the right than to the left.)<br />

If the string is too long to fit the format field, asterisks are printed instead, just as with numbers.<br />

You may also tell <strong>True</strong> <strong>BASIC</strong> to align a string to the left or right within the format field using two special string<br />

place holders, “”:<br />

String PRINT USING Place Holders<br />

——————————————————————————————————————<br />

Place Holder<br />

Reserves Space For<br />

# character or space<br />

< character or space; left justifies string value in field<br />

> character or space; right justifies string value in field<br />

——————————————————————————————————————<br />

In case of more than one “”, the left most one decides.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!