Liberty BASIC Tutorial
Liberty BASIC Tutorial
The BASICS Liberty BASIC is an easy to learn Programming Language Developed by Shoptalk Systems It has many features borrowed from liberty basic, shoptalk system's popular commercial product It is very similar to another language known as QBASIC It has a very good advantage over most other basics, like QBASIC, Quich BASIC and GW basic, this is the advantage of being able to write windows programs as well as text mode programs. All you need to get started is to download the Liberty basic editor
Top Of Page
A simple Text Program We will now learn the basic syntax for writing text mode programs
First run the just basic editor that is located in the start menu in the just basic folder Now start a blank page and we are ready to begin The first command which will be used over and over even in you later programs is very important but simple This command is named 'Print' and in text mode programs it it simple to use. The print command sends text to the screen Example:
All text that is to be sent to the screen or contained in a variable must be enclosed in quotation marks
Top Of Page
Getting Input from the user and using variables Every computer program ive ever seen usually at some point nedds to ask the user to type in something This can be done using a new statement known as input it is similar to the print statement but has something added to the end. A program Using input
input "Please type a number"; number print "You typed number"; number
This might seem difficlult but i will make it easier by steping through it step by step Line 1 - input "please Type a number"; number
input - This is the part that tells basic that it is going to get input "Please type a number" - this is a literal, this gets written on the screen before the user types ; number - this tells basic that the what the user types in gets put in a variable called number, you have to use a semicolen to seperate a literal from a variable
Line 2 - Print "You typed "; number print - as we know, print is used to send text to the screen "you typed" - This prints "you typed" ;number - this prints the contents of the variable number which is what we typed before and the semicolon is used to seperate the literal from the variablle
you wolud not need a semicolon if you just wanted to print the contents of the variable without any other text Example: print number Also you do not have to tye variables, you can declare them in you code Example
Mathmatics With the numeric variables we have used so far we have not done anything to them in this section i will show you how to use mathmatical operations
Example
Here is an example program that shows all of the things we have learned so far
input "Please type a dollar and cent amount"; amount tax = amount * 0.05 total = tax + amount print "The tax is "; tax ;" and the total is"; total
Top Of Page
Comments To better help you understand your own programs, you can add commentary to them that tells whoever is reading it how it works There are two ways to do this The first is rem Example:
rem this is a tax program input "Please type a dollar and cent amount"; amount tax = amount * 0.05 rem calculate the tax total = tax + amount rem display the total print "The tax is "; tax ;" and the total is"; total
Or you can use a method which many prefere which is to add a ' the advantage of this is that you can put this on the end of a line of code. Eg.
print "Hello" ' writes hello you cannot do this with rem when just basic encounters a comment it skips it so you can wite anything you want and the compiler wont care You might not need comments yet but they will be really helpfull when you write bigger programs
Top Of Page
String Variables Liberty basic not only lets you ask the user for a number but also has ways of using words as well. This is done in the same way you ask for a number, all you have to do is add a dollar sign $ on the end of the variable name and it becomes a string Example:
input "type a word"; word$ print "you wrote "; word$ You can also use a plus symbl + to add strings together, name$ = name1$ + name2$ In this example, if name1$ was "Brian" and name2$ was "Smith", the contents of name$ would be "BrianSmith"
You should remember if adding names etc. like this you should add a space in between. Example:
Input "Type your first name"; first$ Input "Type you second name"; last$ full$ = first$ + " " + last$ print "Your full name is "+ full$
Top of Page Branch Labels, Goto and Gosub When Writting our programs we will often need to jump to certain points and return. This can be done with branch labels and a command called goto. Branch labels must be written like this [baranchname] A program can have as many or little brach labels as you want. When you want to jump to a cretain point type goto [branch] this will skip to the point of the program called branch Example of a branch label using program: [start]print "hello" goto [label] print "hello 2" [label] print "hello 3" In this example the program will not print hello 2 because it automaticly jumps to [label] and skipps the print "Hello 2" line. Ther is also a command called gosub, the gosub command skipps to line and when it sees return it jumps back to where it was called. Example: [start] print "hello 1" gosub [part] print "hello 2" [part] print "hello 3"
return
By doing this the program will print in this order hello 1 hello 3 hello 2 It does this because when it sees the line gosub [part], it goes to part and when it sees return it goes back to after the gosub command. Note: Be carefull with gosub. if you use goto and the program encounters a return statement, there will be an error. Only put return in branch labels that are gosubed to not goto.
Top of Page Functions in liberty BASIC In liberty basic you can manipulate numbers and strings in ways that can be used to change the content. Liberty BASIC Includes many predefined functions that you can use. Different functions are used to return different values, like variables functions with the dollar sign ($) on the end return a string And functions without a dollar sign return a numeric value Functions will normally have their name then in brackets the variables and information it needs to make its calculation Here is a short list of functions to get you started. LEN (string$) Len is short for length and is used to see how long the string is in characters Usage: Input type some text; text$ length = len(text$) print length in this example, the user types something in which is then stored in a variable called text$ then the length of text$ specified by len(text$) is stored in the varible length, finally the relult is printed on screen Left$(string$) This function returns the first characters in the string specified by the number. Usage:
Input Please enter a string :; text$ Input how many characters to the left? number Print left$(text$, number) You do not have to use a variable to specify how many characters also You can just type the number if wanted Print left$(text$, 3) Right$(string$) This function is the opposite of left$, it returns the characters to the right of the string Usage: Input Please enter a string :; text$ Input how many characters to the right? number Print right$(text$, number) Mid$(string$) This returns the of characters in the string in the middle Usage: Input Please enter a string :; text$ Input how many characters to the left? ; number Input how many characters to the right? ; number2 Print mid$(text$, number, number2) VAL(string$) The val function returns (if the string in question contains a number or numbes) the numbers in the string Eg. If the user types 234 into a variable that is a string, val would return a numeric version that can be used in mathematical calculations. Usage: Input type some numbers and they will be put in a string; number$ Print the numeric version is ; val(number$) There are also functions for dealing with numbers Int(number) This function returns the integer value of a number. (An integer is a number without a decimal point and following numbers) Eg int(5.5) would return 5 Usage: Input Type a number with decimals; number Print the integer version is; int(number) There are many more functions, for a complete list, see the liberty basic help file
Top of Page
Printing Text OK. Now i'll show you how to send text to the printer. This can be done as simple as sending text to the screen. For this you must use the LPRINT command. Just use it like this:
lprint "hello i'm being printed" If you would like to change the font that the text is printed in, you have to write the font name into as special variable. This is PrinterFont$. Please note that it must be in capital or it will not work. Change it like you would any other variable.
PrinterFont$ = "Arial_Black 20 bold" See that the fiont name has to have _ insted of a space. You do not have to just include one atribute. you can include any or all three if you want. The attributes ar : Bold, Italic and Underlined.