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.

 

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

2 Comments:

  • Eddie, I think you're on your way toward writing what could ultimately result in a very concise and comprehensive tutorial of Liberty BASIC; one that could also serve as an effective instructional text on various general aspects of programming. Seems the concepts covered would be easy to grasp for those learning, as the posts thus far are clear, well written, and thoroughly explanatory.

    -Cassio

    By Anonymous Anonymous, at 5:52 PM  

  • Hi Cassio,

    Thank you for your kind comments. I appreciate your feedback very much.

    I'm glad you like what I have written so far. I hope others feel the same way. I'll try to keep up the same level of writing.

    Thanks

    Eddie

    By Blogger Edward Meyer, at 9:25 PM  

Post a Comment

<< Home