07.05.2015 Views

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

Bronze Edition Guide - 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.

38 BRONZE <strong>Edition</strong> <strong>Guide</strong><br />

Notice the variable total. In the fourth line, an arithmetic expression assigns a value to total<br />

(the sum of the three items, or 1476 in this case):<br />

LET total = item1 + item2 + item3<br />

The next line uses that value of total with the constant .04 to compute the value of tax (.04<br />

* 1476 = 59.04). Now examine the next statement:<br />

LET total = total + tax<br />

This statement assigns a new value to total by adding the previous value of total (1476) to<br />

the value of tax (59.04). After this statement, total has this new value (1535.04), and thus<br />

the PRINT statement uses that value when you run the program.<br />

You could rewrite the COST program to use a separate variable (such as itemtotal or subtotal)<br />

for the intermediate total. Indeed, using two different variables may often be the wisest<br />

choice. However, this ability to add to the value of a variable is important as you’ll see<br />

when you begin to use loops in your programs (see Chapter 8).<br />

An Introduction to Strings<br />

<strong>True</strong> <strong>BASIC</strong> processes words as well as numbers. In computer terminology, anything that<br />

doesn’t have a numeric value is called a string. Your age is a number, but your name or<br />

street address is a string. Strings can include any character your computer can display.<br />

Like numbers, strings can be constants, variables, or expressions.<br />

In the Chapter 5, you used strings with PRINT statements to tell the user what to enter for<br />

the INPUT statements in your MPG2 program:<br />

! Compute miles per gallon<br />

!<br />

PRINT “How many miles”;<br />

INPUT miles<br />

PRINT “How many gallons”;<br />

INPUT gallons<br />

PRINT miles, gallons, miles/gallons<br />

END<br />

Another common use of strings in computer programs is to print text with the output, to<br />

make it clear what the numbers mean. You could add another PRINT statement near the<br />

end of the above program:<br />

. . .<br />

PRINT “Miles”, “Gallons”, “Miles per Gallon”<br />

PRINT miles, gallons, miles/gallon<br />

END

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

Saved successfully!

Ooh no, something went wrong!