Conditions
"I will do that on one condition!"
You may have heard a similar statement before. Well, thinBasic can be told to do something based upon one or more conditions. In this lesson we will discuss two ways to do that.
IF ... Then ... Else
Commonly referred to as simply IF statements, this programming concept allows certain sections of code to be executed based upon "IF" something is true or not. Consider the following example:
X = 5
IF X = 5 THEN MSGBOX 0, X
In this case, the value "5" will be displayed in a Message Box. However, if the condition is changed:
X = 5
IF X = 6 THEN MSGBOX 0, X
There is no Message Box displayed because X does not equal 5. In fact, the program doesn't seem to do anything because the required condition is not met.
Multiple statements can be executed with an IF statement. Notice the following example:
X = 5
IF X = 5 THEN
Y = 3 + 3
MSGBOX 0, Y
END IF
Two different sets of statements can be executed based upon the outcome of an expression using the ELSE keyword. Consider the following example:
X = 5 'If you change the value of X to something other than 5, the second message box will be displayed.
IF X = 5 THEN
MSGBOX 0, "X equals 5"
ELSE
MSGBOX 0, "X does not equal 5"
END IF
If you need an alternate statement executed only if the original condition is not met and a second condition is met, you can use ELSEIF. Here's an example:
RANDOMIZE
DIM X AS NUMBER VALUE ( RND * 500 ) + 1
DIM Msg AS STRING
IF X <= 10 THEN
Msg = X & ": The number <= 10"
ELSEIF X <= 20 THEN
Msg = X & ": The number > 10 and <= 20"
ELSE
IF X < 50 THEN
Msg = X & ": The number is > 20 and less than 50"
ELSEIF X < 100 THEN
Msg = X & ": Greater than 49 and less than 100"
ELSE
Msg = X & ": The number is 100 or greater"
END IF
END IF
MSGBOX 0, Msg
There are a couple new keywords in this example. The first one, RANDOMIZE, basically tells the computer that a random number is going to be used soon, allowing the random number generator to get ready. Truly random numbers are hard to come by. Computers have to be given specific instructions to come up with something that even comes close to random. So, a predefined set of random numbers is created based upon the initial value set by RANDOMIZE. The second keyword, RND, tells the computer to come up with a random number based upon the value set by RANDOMIZE. This initial seed value is based upon the computer's time, which increases the randomness of the sequence used by RND. If a particular sequence of random numbers is what the program needs every time it runs, a seed can be specified with the RANDOMIZE keyword, as follows:
RANDOMIZE (10) 'Any number or even a variable can be used here.
SELECT CASE
Okay, back to the topic at hand. If multiple values for the condition are possible, a combination of IF statements along with ELSE and ELSEIF could be used to obtain the required result. But, there is another way that is often easier: SELECT CASE. Here's the syntax:
SELECT CASE SelectExpression
'NO CODE is permitted here
CASE test
{statements}
CASE test
{statements}
CASE ELSE
{statements}
END SELECT
Here's a working example:
Dim X As Long
X = InputBox$("Type in a number", "Number Generator")
Select Case X
Case 1
MsgBox 0, "You chose 1."
Case 2
MsgBox 0, "You chose one more than 1."
Case 3
MsgBox 0, "Hat Trick!"
Case 4
MsgBox 0, "2 times 2"
Case Else
MsgBox 0, "You didn't choose any numbers I was ready for."
End Select
|