Liberty BASIC - Help Online

Goto - Doing Something More Than Once
 
Assuming that salestax.bas does what we want (see previous section), it still only does it once.  Each time you want to use this handy little program you have to run it again.  This can get to be tedious and even error prone (say rubber baby buggy bumpers  ten times fast).  What we need is a way for our program to go to the beginning and do it over.  In BASIC (and in some other languages) the command for doing this is called goto (surprise!).
 
Knowing that we have to goto some place is not enough.  We also need to know where to go. When you hop into your car in a foreign country looking for a food market, you at least know what you are looking for.  Liberty BASIC can't ask for directions, so you need to be very precise.
 
The mechanism that Liberty BASIC uses to mark places that we can goto is called a branch label.  This is a lot like a mailing address.  When you send a letter or package, you mark it with a known mailing address (hopefully).  There is a house or building somewhere marked with that address, and that is where your parcel goes to.  So in the same way, you mark the place in your BASIC program where you want it to continue running with a branch label (a mailing address of sorts).
 
There are two ways to define a branch label in Liberty BASIC.  You can use any valid integer number as a branch label, or you can use an easier to remember type which uses letters.
 
Examples of integer branch labels:
 
10150759005400etc...
 
Examples of alphanumeric (using letters and numbers) branch labels:
 
[start]  [loopBack]  [getResponse]  [point1]  etc...
 
Examples of unacceptable branch labels:
 
[loop back]no spaces allowed
startmust use brackets
(point1)only use square brackets
 
Since no spaces are allowed in the alphanumeric branch labels, it works well to capitalize the first letter in each word when multiple words are used in a branch label.  For example [gettimedresponse] is valid, but [getTimedResponse] is much more readable.
 
So let's pick a branch label for our salestax.bas program.  Since we are going to do it over again from the start, we could pick from several reasonable branch label names like perhaps [start], [begin], or [go].  We will use [start] for our program.
 
Let's add the branch label as shown:
 
[start]
    input "Type a dollar and cent amount "; amount
    let tax = amount * 0.05
    print "Tax is: "; tax; ". Total is: "; tax + amount
 
 
Now we need our goto line.  Now that we have our branch label, the correct format for goto is goto [start]. And here's what our program looks like when both a branch label and a goto:
 
[start]
    input "Type a dollar and cent amount "; amount
    let tax = amount * 0.05
    print "Tax is: "; tax; ". Total is: "; tax + amount
    goto [start]
 
Now let's try running this program.  It runs over and over and over, right?  This programming format is called an unconditional loop because it always loops back to repeat the same code no matter what.  When we are finished with it, we can close it like any other Windows program by double-clicking on the system menu box.
 
Next Section: IF . . . THEN - Adding Smarts to Our Tax Program


Copyright (C) 2005 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/