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

2 Comments:

  • I believe the latest version of Liberty Basic (version 4.03) has a way for you to catch 'divide by zero' errors in your program so that you can act upon them instead of just having to accept that your program will crash. Having said this, I haven't learnt how to do this yet. :-)

    Eddie

    By Blogger Edward Meyer, at 3:19 PM  

  • This is done by using the ON ERROR GOTO statement. This is pretty close to a standard BASIC construct.

    -Carl Gundel, author of Liberty BASIC

    By Blogger Carl Gundel, at 8:17 PM  

Post a Comment

<< Home