The Conditional Statement


What is a Conditional Statement?

We use conditional statements in our everyday life without thinking about it. A conditional statement is a statement that must meet a certain criteria to be true. In English we can usually identify a conditional statement by the word if. For example we might say,

If the weather is nice then we will have a picnic.

The statement "we will have a picnic" is ONLY true if the condition of nice weather is met.

Not surprisingly, the Liberty BASIC code word for a conditional statement is called the IF Statement. Let us look at a short example:

input "What is your favourite number?"; number
if number = 7 then
print "That's my favourite number too!"
end if


This program asks the user for a number, then checks to see if the number is 7. If it is 7, it will print the message "That's my favourite number too!".

Let's look at another example:

input "What is your favourite number?"; number
if number = 7 then
print "That's my favourite number too!"
end if
print "Have a good day!"


This program asks the user for a number, if it is 7 it will print both the first and second messages to the screen. If the number is not 7, the program will jump down to the end if and only print out the second message to the screen.

What would happen if we wanted to print two different messages depending on the number entered?

The If Statement also has a sub-statement called Else. With Else we can include both a TRUE condition and a FALSE condition. Let us look at another example:

input "What is your favourite number?"; number
If number = 7 then
print "That's my favourite number too!"
Else
print "What a maci number."
End If


The above program will print the first message if the condition is TRUE (i.e. if the number is 7), otherwise if the condition is FALSE (i.e. if the number is not 7) the program will print the second message. The ELSE part of the IF...THEN...ELSE Statement only runs when the IF Condition is FALSE.

Besides just using an equal sign (=) you can use other logical comparisons to see if an expression is true. Have a look at the following table:

Symbol Indicates
= Is Equal To
< Is Less Than
> Is Greater Than
<> Is Not Equal To
<= Is Less Than OR Equal To
>= Is Greater Than OR Equal To