Sometimes a programmer would like a statement, or group of statements to execute only if certain conditions are true. There may be a different statement, or group of statements that are to be executed when the condition is false. An expression that can be evaluated to true or false is called a Boolean expression. (Named after a mathematician named Boole.)
Examples of Boolean Expressions with relational operators: (X and Y are variables)
X = 5 |
(True if X has a value equal to 5) |
X > Y | (True if X is greater than Y; example X is 7, Y is 3) |
X < Y | (True if X is less than Y; example X is 2, Y is 2.5) |
X <> Y | (True if X is not equal to Y; example X is 5, Y is 6) |
X >= Y | (X is greater than or equal to Y; example X is 4, Y is 3 or 4) |
X <= Y | (X is less than or equal to Y; example X is 4, Y is 4 or 5) |
The relational operators can also be used to compare strings: s1<s2 means s1 comes before s2 alphabetically. "a" < "A". Equality is case sensitive.
s1= "NO" |
(True if s1 has a value equal to "NO") |
s1 > s2 |
(True if the string s1 is greater than the string s2; example s1 is "B", s2 is "A") |
s1 < s2 |
(True if s1 is less than txt; example s1 is "ready", s2 is "set")
|
s1 <> s2 |
(True if s1 is not equal to s2; example s1 is "YES", s2 is "yes") |
s1 >= s2 |
(s1 is greater than or equal to s2; example s1 is "45", s2 is "400")
Strings are compared starting in left most position, 1st postion is same, "5">"0" |
s1 <= s2 |
(s1 is less than or equal to s2; example s1 is "Ann", s2 is "Anna")
s1 is "ann", s2 is "ANN" |
The IF Statement
If you want to execute one or more statements when a Boolean expression is true use the format shown below:
If Boolean exp. Then
statement 1
statement
...
Else
false statement 1
false statement
...
End If
The Else section is optional;. If there are no statements to execute when the Boolean expression is false, you may leave out the else section.