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