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.

 

Thursday, June 08, 2006

Forum: Liberty BASIC for QBASIC Programmers

Hi all,

I stumbled across this forum today Liberty BASIC for QBASIC Programmers.

At time of writing, users don't seem to have posted to this forum recently. Perhaps the forum is at least still being read by people from time to time -- I hope so. I'm writing a post about this forum because I do think there are some good (and simple) example programs on it. Even if usage of this forum doesn't pick up, I do think it is still useful as a place of reference for people trying to learning Liberty Basic.

Have fun.

Eddie

Tuesday, June 06, 2006

I've now purchased Liberty Basic 4.03 GOLD.

Hi all,

This is just to let you know that I've now bought a gold license for Liberty Basic 4.03. I encourage readers of this blog to also purchase a copy (or upgrade if you are using an earlier version).

As I'm sure you are all aware, you can get the latest version of Liberty Basic from http://www.libertybasic.com.

Anyway, I'll continue with my regular posts covering how to use Liberty Basic in due course.

Thanks

Eddie

Tuesday, May 30, 2006

Catching the 'Divide By Zero' Error

In my earlier post 'Simple Math' (dated 30th April 2006), I mentioned that your program would crash if you try to divide something by zero. Correction, your program will crash if you divide something by zero and you don't catch the 'Divide By Zero' error.

Here is an example program that shows you how to catch the 'Divide By Zero' error.



' ==================================
' Catching a Divide By Zero Error
' ==================================
' Description:
' This program demonstrates the use
' of Liberty Basic's 'On Error'
' construct to catch the 'Divide By Zero'
' error condition.
'
' Author: Eddie Meyer
' Date: 29th May 2006

' Constants
' ===========
' The value of the following costant was
' taken from Alyce Watson's ebook (Liberty
' BASIC 4 Companion). Apparently, 11 is
' the error code for the 'Divide By Zero'
' error condition.
Const.DivideByZero = 11

' Variables
' ============

' The first number entered by the user
Num1 = 0
' The second number entered by the user
Num2 = 0

' The result after dividing Num1 by Num2
Result = 0

' Set the 'On Error' handler
On Error Goto [TellUserOff]

' Output some explanatory text to the user
print "This program will ask you for two numbers."
print "It will then display the result of..."
print "Num1 / Num2"
print ""
print "Note: This program will tell you off if you"
print "specify zero (0) as the second of the two"
print "numbers."
print ""

' Get the two numbers
input "Please enter a number: "; Num1
input "Please enter another number: "; Num2
print ""

' Perform the calculation
Result = Num1 / Num2

' Output the result.
print "Num1 divided by Num2 equals "; Result
Goto [end]

[TellUserOff]
' Sanity check - double check that the error
' in question was the 'Divide By Zero' error.
if Err = Const.DivideByZero then
print "You can't divide a number by zero! I"
print "told you this before. What's wrong"
print "with you?"
end if

[end]
end


A crucial point to note about the above program is that we have set an 'On Error' handler. The 'Divide By Zero' error condition is just one kind of error that can potentially occur while your program is running. Another common one is the 'File Not Found' error (which occurs if your program tries to open a file that doesn't exist). Each of the different error conditions that can be handled in your program has a unique error code associated with it. As should be clear from the program above, the error code for the 'Divide By Zero' error is 11. Just for your information, the error code for the 'File Not Found' error is 53. As part of the 'On Error' construct, you need to specify somewhere for the code to go to if an error arises. In our example above, we specified that if an error occurs, we want program execution to jump to the lable named 'TellUserOff'. The rest of the code should be pretty self-explanatory.

I hope you found this post useful.

Please leave me some comments to let me know what you think of it.

Thanks

Eddie

Wednesday, May 24, 2006

Looping using the 'For' loop.

Seeing as I have just given an example of how to use the 'do while' loop, I may as well quickly give an example of how to use the 'for' loop. I will use the same example of needing to read in 10 numbers and then print out the sum.

Here is the program using the 'for' loop.


' ==================================
' Demonstration Of The 'For' Loop
' ==================================
' Description:
' This program demonstrates the use of
' the 'for' loop to read in 10 numbers
' from the user. The sum of the 10
' numbers is calculated and output at
' the end of the program.
'
' Author: Eddie Meyer
' Date: 24th May 2006

' Variables
' ============

' The number of items to read in
NumItemsToRead = 10

' The current total
Total = 0

' Loop until we have read in the required
' number of items.
for i = 1 to NumItemsToRead
if i = 1 then
input "Please enter a number: "; InputNumber
else
input "Please enter another number: "; InputNumber
end if

' Update the current total
Total = Total + InputNumber
next

' Print out the total
print
print "The sum of the "; NumItemsToRead; " numbers you entered is ";
print Total; "."

end


Points to note about the 'for' loop are...

1) The use of the 'for' and 'next' keywords.

2) The variable that will be used as the counter.

3) The start value of the counter and the end value of the counter.

As you can see, the 'for' loop is really simple to use and ideal for cases where you just want to iterate over a piece of code a certain number of times. It is pretty common practice to use simple variables names with for loops (like i in my example above). Normally, you are encouraged to use very descriptive names for your variables, but in the case of the 'for' loop, we know the variable is just going to be some kind of counter so we can make an exception here and keep it simple. I used a variable called i because this seems to have developed into the most commonly used variable name for 'for' loops. By sticking with convention, other programmers will find it easier to read and maintain my code (should they need to). Note: the letter 'i' can at least be seen to be an abbreviation of the word 'index'. :-).

There you go. You have now officially been introduced to the three main types of loops provided by Liberty Basic.

Have fun with your programming.

Eddie

Looping using the 'Do While' loop

In the last post we saw how we could use the 'while' loop to execute a section of code multiple times. Our example was that of needing to read in 10 numbers and printing out the sum of those numbers. In this post we'll see how we could have implemented the same functionality using the 'do while' loop. There was nothing intrinsically wrong with our earlier implementation using the 'while' loop, I'm just using the same functional requirements to illustrate the use of the 'do while' loop also.

Here is our example program demonstrating the use of Liberty Basic's 'do while' loop.


' =======================================
' Demonstration Of The 'Do While' Loop
' =======================================
' Description:
' This program demonstrates the use of
' the 'do while' loop to read in 10 numbers
' from the user. The sum of the 10
' numbers is calculated and output at
' the end of the program.
'
' Author: Eddie Meyer
' Date: 24th May 2006

' Variables
' ============

' The number of items to read in
NumItemsToRead = 10

' We are currently reading in item #CurrentNum
CurrentNum = 1

' The current total
Total = 0

' Loop until we have read in the required
' number of items.
do
if CurrentNum = 1 then
input "Please enter a number: "; InputNumber
else
input "Please enter another number: "; InputNumber
end if

' Update the current total
Total = Total + InputNumber

' Update the counter
CurrentNum = CurrentNum + 1
loop while CurrentNum <= NumItemsToRead

' Print out the total
print
print "The sum of the "; NumItemsToRead; " numbers you entered is ";
print Total; "."

end


The main points to note about the 'do while' loop are...

1) The 'do', 'loop' and 'while' keywords.

2) The fact that when you use the 'do while' loop as shown, you guarantee that your loop will always execute at least once. This is because the check of whether to execute another iteration of the loop is placed after the main body of the loop. Compare this with the regular 'while' loop that performs the check first and only executes the loop body afterwards. To my mind, this difference of whether the loop will always run at least once or not, is the main distinction between the two loops -- If you need your loop to always execute at least once, use the 'do while' loop, otherwise, use the regular 'while' loop.

I hope that makes some sense.

Eddie

Thursday, May 11, 2006

Looping using a 'While' loop

Sometimes you need your code to do something repeatedly. Let's say you are writing a program to calculate the total of 10 numbers. Rather than writing 10 lines of code that read in a number and 10 lines of code that add the latest number to the current total, you could use a loop instead. Liberty Basic offers three kinds of loops... 1) The 'while' loop, 2) the 'do while' loop, and 3) the 'for' loop. In this post we will take a look at the 'while' loop. In later posts we can tackle the 'do while' loop and the 'for' loop. Don't think I am leaving these other two loops for later because they are harder than the 'while' loop. The 'do while' loop and the 'for' loop are just as easy to understand and use. I just like tackling one thing at a time. The 'while' loop is a fine place to start when you want to start introducing loops into your programs. Anyway, so here is how it goes. The example program below reads in 10 numbers and calculates the sum of all the numbers entered in.


' =====================================
' Demonstration Of The While Loop
' =====================================
' Description:
' This program demonstrates the use of
' the while loop to read in 10 numbers
' from the user. The sum of the 10
' numbers is calculated and output at
' the end of the program.
'
' Author: Eddie Meyer
' Date: 11th May 2006

' Variables
' ============

' The number of items to read in
NumToRead = 10

' We are currently reading in item #CurrentNum
CurrentNum = 1

' The current total
Sum = 0

' Loop until we have read in the required
' number of items.
while CurrentNum <= NumToRead
if CurrentNum = 1 then
input "Please enter a number: "; InputNumber
else
input "Please enter another number: "; InputNumber
end if

' Update the current total
Sum = Sum + InputNumber

' Update the counter
CurrentNum = CurrentNum + 1
wend

' Print out the total
print
print "The sum of the "; NumToRead; " numbers you entered is ";
print Sum; "."

end


Points to note are...

1) The 'while' keyword to signify the start of the loop
2) The expression after the 'while' keyword. Basically, the loop will execute as long as this expression remains true.
3) The 'wend' keyword to signify the end of the while loop.

Also (as a side note for newbies), notice how I got the program to make a distinction between the case when we are reading in the first number compared to the case when we are reading in any of the subsequent numbers. For the first number the program prints "Please enter a number: " and for the other times it prints "Please enter another number: ".

Tip: When writing your while loops, make sure that there will be a time when the loop will exit. Otherwise the loop will go on forever... or at least until the program or computer crashes. In our case we used a counter called CurrentNum that increments until it reaches a value of 11. When CurrentNum reaches a value of 11, the expression CurrentNum <= NumToRead is not true any more, and therefore the loop stops executing (doesn't perform any more iterations). Note, that if we forgot to add the line CurrentNum = CurrentNum + 1, the value of CurrentNum would have always remained as 1 and the loop would have continued indefinately (until the program crashed).

Anyway, I think that is enough for one post. I hope you enjoyed it.

Eddie

Tuesday, May 02, 2006

Decisions Using The 'If' Statement

Very early on in your programming, you are going to need to get the computer to do one thing if a certain condition is true, but do something else if it is not true. Let's take a look at how we might do this in Liberty Basic.

First, let's think of a very simple example we can use to illustrate this concept. How about a program to work out how much a salesperson should get paid based on the number of items he/she sold. Let's say that company X pays it's salespeople $15 for every DVD player they sell. In addition to this, let's say that the company pays an additional $250 to salespeople that sell more than 1,000 DVD players in that calendar month.

Here is the example program... (as you can see, the decision logic is implemented using the 'if', 'then', 'else' and 'end if' keywords)...


' Program to determine how much to pay a salesperson
' working for company x. Salespeople at this company
' earn $15 for every DVD player they sell and receive a
' bonus of $250 if they sell more than 1000 units.

' Author: Eddie Meyer
' Date: 30th April 2006

print "How many DVD players did you sell this month?"
input "> "; NumItemsSold
Salary = NumItemsSold * 15

' Now add the $250 for those that sold more than
' 1000 DVD players.
if (NumItemsSold > 1000) then
Salary = Salary + 250
print "Congratulations, you will be getting a bonus this month."
else
print "Sorry, no bonus for you this month."
end if

' Print out the salary due to the saleperson.
print "This month you will be paid $"; Salary; "."

end


I'm sure you were able to understand how the 'if' statement worked. The 'else' clause is optional. For example, if we didn't want to display a message to those people that aren't going to get a bonus, we could have written the program as follows...


' Program to determine how much to pay a salesperson
' working for company x. Salespeople at this company
' earn $15 for every DVD player sold and receive a
' bonus of $250 if they sell more than 1000 units.

' Author: Eddie Meyer
' Date: 30th April 2006

print "How many DVD players did you sell this month?"
input "> "; NumItemsSold
Salary = NumItemsSold * 15

' Now add the $250 for those that sold more than
' 1000 DVD players.
if (NumItemsSold > 1000) then
Salary = Salary + 250
print "Congratulations, you will be getting a bonus this month."
end if

' Print out the salary due to the saleperson.
print "This month you will be paid $"; Salary; "."

end


There, that wasn't too hard was it. It may not look or feel like much, but the ability to get the computer to do different things based on different variables is a very fundamental part of programming. Obviously, the more complex the program, the more decisions the computer has to make... but the syntax of the 'if' statement remains the same... you will just have considerably more of them.

Anyway, that's it for today.

I hope you enjoyed this post.

Eddie

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