Using Liberty Basic

Want to learn to program using Liberty Basic? Already programming in Liberty Basic but want to learn more? Follow this experienced programmer (Java) as he teaches himself this wonderful programming language.

 

Sunday, April 30, 2006

Simple Math

In this post, we will take a look at how to get Liberty Basic to perform simple math operations. It is REALLY easy.

Addition is performed using the + operator.

Subtraction is performed using the - operator.

Multiplication is performed using the * operator.

Division is performed using the / operator.

Here is an example...


' Simple Math In Liberty Basic
' Author: Eddie Meyer
' Date: 30th April 2006

num1 = 12
num2 = 7

' Let's do some addition
result = num1 + num2
print "Result of addition: "; result

' Now let's do some subtraction
result = num1 - num2
print "Result of subtraction: "; result

' Now let's do some multiplication
result = num1 * num2
print "Result of multiplication: "; result

' Finally, let's do some division
result = num1 / num2
print "Result of division: "; result

end


Running this program prints the following results


Result of addition: 19
Result of subtraction: 5
Result of multiplication: 84
Result of division: 1.71428571


Note: You can also put several operators on one line. Just like the math you were taught at school... the rules of precendence will apply. For example, expressions are evaluated right to left and the multiplication and division operations will be evaluated before the addition and subtraction operations. Of course, anything in brackets, get's evaluated first. Here are a couple of examples...


result = (4+2)*6-2 ' result will be 34

result = 4+2*6-2 ' result will be 14


One last note. Dividing a number by zero (0) is illegal in Liberty Basic. You will want to stop your program from attempting to do this. An attempt by your program to divide a number by zero will cause Liberty Basic to report a runtime error and your program will stop running. You have been warned :-).

That's it for today. I hope you enjoyed this post.

Eddie

Thursday, April 27, 2006

Getting User Input

If you have been following me so far, we now know how to store data in our programs as well as how to output that data to the screen. The next logical step for us is to be able to receive data from the user, and know how to store that data as variables in our programs.

As this blog progresses, we'll move on to creating windows programs that use the various windows gadgets you are all used to seeing as part of a windows program. I'm talking about things like textboxes, buttons, menus, lisboxes etc. When we discuss these components we'll also discuss how to get user input through them. For right now, however, we are dealing with the 'main window' that gets displayed by Liberty Basic as the default user interface for our Liberty Basic programs.

So, how can we get any input from the user via this 'main window'? Here is an example program...


' =============================
' Getting User Input
' =============================
' Program to demonstrate receiving some user input.
' Author: Eddie Meyer
' Date: 27th April 2006

print "This program will ask you for some information."
print "After each prompt, please enter the requested information"
print "and then hit 'Enter'."
print
input "Please enter your full name: "; FullName$
input "Please enter your age: "; Age
print
print "Your name is "; FullName$; " and you are "; Age; " years old."

end


When you run this program the following is printed...


This program will ask you for some information.
After each prompt, please enter the requested information
and then hit 'Enter'.

Please enter your full name:


The program then waits until you enter your name and hit the 'Enter' (Return) key. Actually, technically, the program only waits until you hit the 'Enter' key. Basically, anything you type before hitting the 'Enter' key is stored in the variable FullName$. If you didn't enter anything and just hit the enter key, nothing (the empty string ""), will be stored in the FullName$ variable. Anyway, after hitting the 'Enter' key, the program then continues running. It then displays the following (see below) and again waits for user input.


Please enter your age:


This time, anything entered before the 'Enter' key is hit will be placed in the Age variable. Note: In this case the variable in question is expected to have a value that is a number. Do you remember why? It is because the variable Age was defined without a trailing dollar sign ($). If it had been defined with a trailing dollar sign ($), then we (and the Liberty Basic compiler) would know to expect a string. You may ask why I am pointing this out. I'm mentioning this because users don't always do what we expect them to. For example, a 30 year old user using our program might type in "thirty" instead of the numerical 30 that we are expecting. If they did do this, the Age variable would be set to contain the default value of 0 (because the user didn't enter a number at all... or at least, they didn't enter a number in numerical form). Interestingly, if the user typed in a string that started with a number, that number at the beginning of the string would be used. For example, if the user entered an age of "9.5inches", the value 9.5 would be stored in the Age variable. It is good practice to always check the validity of the input into your program. In our case, we probably would want to check that the user did actually enter a name and that they also entered a valid number as their age. This issue of input validation is a subject in it's own right. I'm sure I will tackle it in a later post. Leave me a comment if this is something you would be interested in me covering.

Anyway, I think that's it for now. I hope you enjoyed this post.

Thanks

Eddie

P.S. One other point to note about my program in this post. There are a couple of lines that only contain the 'print' command and nothing else. These are used just to print out a new line for spacing purposes. If you look at the example output I provided above, you should be able to see what I mean (e.g. there is a blank line between the header information and the first question the program asks).

Tuesday, April 25, 2006

Variables

Variables are used in programming languages to reference pieces of data. For example, you may have a variable called age, to store data about a person's age... or you may have a variable called IQ, to store data about someone's IQ. Equally, you can use variables to store things like someone's eye color e.g. "brown". What data you store in your program is up to you and dependant on the program you want to write. The names of your variables are also up to you, but it is good programming practice to give the variables in your programs names that describe the data they store, as per my examples above.

As far as data types are concerned, Libery Basic provides two types of variable... 1) Strings... 2) Numbers...

For those of you that don't know, a string is a sequence of characters. Examples of strings are... "Fred", "Wilma", "Fred and Wilma", "Apples", "Oranges" as well as "59 apples and 39 oranges". Note that strings can contain punctuation marks, spaces, numbers and other symbols like the dollar sign ($) or special characters like the copyright sign (©). A string is specified in a program by enclosing a sequence of characters in double quotes ("), e.g. "This is a string".

Now, what about these numbers??? Many programming languages make a distinction between integers and decimals. Integers are whole numbers (i.e. numbers without a decimal point) and decimals are, well, numbers that do have a decimal point. Anyway, Liberty Basic likes to keep things simple and does not make you distinguish between these two types of numbers in your programs. Example values for a 'number' variable are therefore... 20, 30, 40, 9.5, 10.8 and 63.5291... you get the picture.

So, how do you define and use these variable thingies. It's simple. Take a look at the example below.


' =======================
' Using Variables
' =======================
' Program to demonstrate using variables.
' Author: Eddie Meyer
' Date: 25th April 2006

' Setup a 'name' variable
name$ = "Eddie Meyer"

' Setup an 'age' variable
age = 30

' Use both of these variables in a couple of
' print statements
print "Hello "; name$; "."
print "Apparently you are "; age; " years old."


Running this program, prints the following on the screen.


Hello Eddie Meyer.
Apparently you are 30 years old.


Now, there are a couple of things to note.

1) Variable names must always start with a letter (never a number).
2) After the fist character in the variable name, numbers may be used.
3) String variables must always end with a dollar sign ($).
4) Number variables must never end with a dollar sign ($) (because if the variable ended in a dollar sign, Liberty Basic would expect a string value and not a number).
5) You can use the semi-colon character (;) alongside your 'print' commands so that you can print multiple things on one line.

OK, I think that's enough for now. I hope you enjoyed this post. Feel free to leave some comments to let me know what you think.

Eddie

Monday, April 24, 2006

Commenting Your Code

As I am sure you are aware, it is extremely recommended for you to comment the code you write (both so that others can understand your code more easily, and also so that you can make sense of your own code more quickly when you come back to it after a break of more than a couple of months).

So, how do you comment your code in Liberty Basic? It's simple, all you have to do is prefix your comment with an apostrophe ('). If your comment spans multiple lines, make sure each line has it's own apostrophe to indicate the start of the comment on that line.

Let's demostrate this by way of example. How about we add some comments to the "Hello World" program we created in the last post. Here is what our new version of the program might look like.

' =================================
' Simple Hello World Program
' =================================
' Author: Eddie Meyer
' Date: 24th April 2006

print "Hello World!"
end


Note: A comment doesn't have to start at the beginning of a line. The following comment is equally valid.

print "Hello World!" ' This is a comment.
end


Note: In this second example, since the comment occurs after the print command, the print command will still execute. Had the apostrophe been placed before the print command, the print command would become part of the comment and would therefore not execute.

That's it. Now we don't have any excuse not to comment the code we write. :-). I wonder what the next post might be about. Oh well, we'll have to wait and see.

Eddie

A "Hello World" Program

So, now it is time to start learning how to program in Liberty Basic. Traditionally the first program you write in a new language is a program to write "Hello World!" to the screen. So let's do that it Liberty Basic. The code goes something like this.

print "Hello World!"
end

Now that wasn't hard was it? I was suprised to find that running this program brought up a default window with a textbox... I'm used to simple programs like this outputing to a console window. In some ways, it is kinda nice that a real window is brought up. I encourage you all (if you are learning to program Liberty Basic along with me) to try entering this example program and running it yourself. For those of you who are curious, this is what the output should look like... (click on the image to view the picture full size)

Well, that's our first program written. I know it is possible to write programs that don't bring up this default window. I wonder if it is possible to bring up a console window instead (for those times when you actually want a console window). I'll leave this for another post though.

I hope you enjoyed.

Eddie

Sunday, April 23, 2006

I've Installed The Free Trial

Hi all,

Just wanted to let you know that I've installed the free trial. So far so good. Now I have to figure out how to start programming in this language. I'll let you know how it goes.

Thanks

Eddie.

Using Liberty Basic

Hi everyone.

So, this is my first post on this blog. I guess this is a good time to explain why I created this blog. It's simple really, this blog is going to document my experience using Liberty Basic. At the moment I am completely new to Liberty Basic, but, seeing as I am a software engineer by profession, I'm hoping that it won't be too hard for me to get the hang of it.

So why Liberty Basic? Now that is really good question. Let's see. Well, first and foremost, I was looking for a programming language I could use to develop small programs for the PC (windows). I program in Java for a living and I am very proficient in it, so Java was definately an option. The problem with Java for me was that I really want to be able to distribute my applications as an executable (.exe). I also want to develop GUI applications that have the windows look and feel. I simply feel that the Swing components I would have used in Java have a definate Java look to them. I want my applications to look like a windows program, not a Java program that happens to be running on windows. With these kind of thoughts in mind, I went in search of another programming language to use. I definately didn't want to pay the exhorbitant prices charged by Microsoft for their latest Visual C++, Visual Basic or whatever... and I equally didn't want to pay the similarly high price for Borland's equivalent programs like Delphi or C++ Builder. No, I was definately going to need to look for something smaller... and preferably something simple too. I did teach myself a little C, just because I felt that it's important to know, but I think C is a little too low level for being able to quickly develop desktop applications. I thought about using C++... but, to be honest, I just don't like C++. It's just not elegant enough for my liking. If I was going to use C++ though, I would definately use Devshed's Dev C++. Anyway, so this left me still looking for a good development language with which to create windows programs. After much hunting around, it became clear to me that the only way I was going to know whether I liked a language or not, was to give it a go. This is where I decided to make a choice an just go with it. If I liked it, great. If I didn't like it, hopefully I would learn why I didn't like it. This is where this blog comes in. I decided that I would give Liberty Basic a whirl and write about my experiences with it. If I do really well with Liberty Basic, I will let you in on the tips and tricks I learn along the way. If I find problems with Liberty Basic, I will document them here so that others may learn from my experience. If this is the case, I will pick another language and hope for a better experience next time. Either way, I decided it was time to take action... and give something a try.

Some of the reasons for me starting with Liberty Basic are...
  1. It seems to be frequently updated.
  2. It appears to have a strong user base.
  3. It claims to be easy.
  4. It's cheap.
  5. There seems to be quite a few plugins that have been developed by various users.
  6. It has a GUI builder.
  7. It can always be extended via C if need be.
Anyway, so that's the background for this blog. I hope you enjoy this journey with me. Please do leave some comments on this site so that I know people are reading this blog (even if the comment is... "I wish you good luck with your programming"... or... "Are you crazy... C++ is obviously the best language in existence", etc).

Thanks

Eddie.