| Liberty BASIC - Help Online |
An Introduction to BASIC
BASIC (Beginners All purpose Symbolic Instruction Code) was created in the 1960's as an easy to learn programming language for computers. Because of BASIC's simple form and because it was an interpreted language and gave the programmer instant feedback, it became the most popular programming language when microcomputers made their debut, and it has a large following even today.
This tutorial introduces the first principles of Liberty BASIC, but doesn't provide a thorough description of all language features. For more on the full language and command set, refer to the documentation included with your copy of Liberty BASIC.
Salestax.bas, a simple BASIC program.
Now let's create a very simple program to introduce you to the simplest of BASIC's features. We want a BASIC program that:
1 - Asks for a dollar and cent amount for goods ;
2 - Calculates a 5% sales tax amount for the dollar and cent figure ;
3 - Displays the tax amount the total amount
INPUT - First we need an instruction for the computer that gets information from the user. In BASIC there are several ways to do this but we will choose the input command for our program. In this case input would be used like so:
input "Type a dollar and cent amount ?"; amount
^------this is a variable
This line of BASIC code will display the words, "Type a dollar and cent amount ?", and the computer will stop and wait for the user to type something in. When the [Enter] key is pressed, the typed information will then be stored in the variable* amount.
*variable - In programming, you must assign each bit of data (or information) a unique name. This combination of a name and its data is called a variable because the data part can vary each time the program is used. When you edit a program, you choose a name for each variable. You pick the name for each variable to best fit the kind of data it represents. BASIC doesn't care what names you assign to your variables, except you can't use the names of BASIC commands or functions (called reserved words). Choose names that make it easy for anyone to understand what the BASIC program code means and does. When running a program, BASIC uses the data part of the variable in its calculations. BASIC uses the name of the variable to fetch its data part or to store new data in that variable. Variable data can change many times during the execution of a BASIC program.
CALCULATION - Now we need to calculate the tax for the data in our amount variable:
let tax = amount * 0.05
This line of code creates a new variable called "tax" to hold our computed tax data. The BASIC command, "let", tells BASIC to calculate the arithmetic on the right side of the = and set the data of the variable "tax" equal the results of the equation. The "let" word is optional (and most programmers leave it out) but I use it here as an example of BASIC syntax. It could have been coded as follows:
tax = amount * 0.05
Now you may be wondering what is that funny little '*' (called asterisk). Since there are no formal arithmatic symbols on a typewriter keyboard, most programming languages use * to denote multiplication, / for division, and the addition and subtraction symbols get lucky and are + and - (what else?).
OUTPUT - Now that we have calculated our tax amount, we will display it with:
print "Tax is: "; tax; ". Total is: "; tax + amount
The print command displays the information to the screen. The line of code above shows how print is used to display several items of data, each separated by a ';' (semicolon).
The items are:
"Tax is: "- This displays on screen as is, but without the quotation marks ;
tax- This displays the value of the variable tax ;
". Total is: "- This also displays on screen as is, but without quotation marks ;
tax + amount- This displays the sum of the two variables, tax and amount
These will all be displayed on the same line. The semicolons are not displayed. Each print command is followed by a carriage return. The result might look like this:
Tax is: 0.05. Total is: 1.05
Now let's run the program. Type or cut/paste the following program into the Liberty BASIC editor so that it looks as shown.
input "Type a dollar and cent amount?"; amount
let tax = amount * 0.05
print "Tax is: "; tax; ". Total is: "; tax+amount
Now run the program by clicking on the Run menu and clicking on the Run item (or press Shift+F5).
And here is a sample run:
Type a dollar and cent amount?1.20
Tax is: 0.06. Total is: 1.26
Now let's save our program. Select the File menu and choose Save As. Now type the name salestax.bas and click on OK.
Copyright (C) 2005 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/