| Liberty BASIC - Help Online |
If...Then - Adding Smarts to our Tax Program
The program we designed above will only do one thing for us, no frills. Let's learn how to add some smarts to our program. One way that this can be done is with the if . . . then statement.
The if . . . then statement is a direct descendant of those do-it-yourself style instruction manual texts. For example:
Problem: Your car's engine won't turn over
1) Check your battery for correct voltage.
2) If voltage is less then 11 volts then goto to step 13
3) Clean and tighten ground connection.
4) If this doesn't solve the trouble, continue to step 5.
5) Remove the starter.
6) Connect starter directly to battery
7) If starter does not spin then goto step 18
8) Check starter relay.
. . .
13) Charge battery
. . .
18) See chapter 4 on rebuilding the starter unit
Notice how in the above example how you are led smartly through the troubleshooting procedure. The steps containing the words if and then make it possible to intelligently work through a complex procedure. In the same way, the if . . . then statement in BASIC makes it possible to add a kind of intelligence to your programs.
Let's see how we can apply this. Suppose we want the computer to give us the option to display instructions about how to use our tax program. An easy way to add this ability would be to display instructions whenever a zero value is entered as our dollar amount.
Now, whenever input is used to get a number from the user, if the user doesn't type a number but only presses the [Enter] key, then Liberty BASIC substitutes zero for the variable. We can exploit this feature in our salestax.bas program. By checking to see if the variable equals zero after the input statement, we can decide whether or not to display instructions.
Here's what our new program looks like:
[start]
print "Type a dollar and cent amount."
input "(Press 'Enter' alone for help) ?"; amount
if amount = 0 then goto [help]
let tax = amount * 0.05
print "Tax is: "; tax; ". Total is: "; tax + amount
goto [start]
[help]
print "This tax program determines how much tax is"
print "due on an amount entered and also computes"
print "the total amount. The tax rate is 5%"
goto [start]
Notice the line "if amount = 0 then goto [help]" in the program above. When Liberty BASIC execute this line, it checks to see if the variable is equal to 0. If the variable equals zero, then the goto[help] statement in the line is executed. Liberty BASIC then begins executing the instructions after the [help] branch label. The if...then statement reads exactly like it executes.
Actually, the goto part of the if . . . then statement is optional. Either of these two forms is acceptable:
if amount = 0 then goto [help]
- or -
if amount = 0 then [help]
Comparing numbers - The = (equality) operator is only one of several that can be used to make decisions in an if . . . then statement. We can use the if . . . then statement and the ( =, <>, <, >, <=, >= ) operators to determine whether:
a = b a is equal to b
a <> b a is unequal to b
a < b a is less than b
a > b a is greater than b
a <= b a is less than or equal to b
a >= b a is greater than or equal to b
For example, instead of checking to see if amount was equal to 0 in the above program, we could have checked to see whether it was less than 0.01 (or one cent). For example:
if amount < 0.01 then goto [help]
When you run the program above you will probably notice that the things displayed sort of run together. There are things that we can do to neaten up the appearance of a BASIC program. We can add extra blank lines between our printed output to break things up. This is done by using an empty print statement, one for each blank line. We can also clear the window at an appropriate time with the cls statement. Both of these techniques are applied to our tax program in the listing below:
[start]
print "Type a dollar and cent amount."
input "(Press 'Enter' alone for help) ?"; amount
if amount = 0 then [help]
let tax = amount * 0.05
print "Tax is: "; tax; ". Total is: "; tax + amount
goto [start]
[help]
cls
print "SALESTAX.BAS Help"
print
print "This tax program determines how much tax is"
print "due on an amount entered and also computes"
print "the total amount. The tax rate is 5%."
print
input "Press [Enter] to continue."; dummyVariable
print
goto [start]
Notice the line 'input "Press [Enter] to continue."; dummyVariable' in the listing. In this example, we are using an input statement to halt the program, so the instructions can be read. When [Enter] is pressed as instructed, dummyVariable receives the value of what is entered. In this case, only [Enter] is pressed, so dummyVariable gets a value of zero for its data. It really doesn't matter what dummyVariable's data is since we don't use the variable in any calculations elsewhere (hence the name dummyVariable).
Copyright (C) 2005 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/