## IfThen Else

There are several ways to implement this code structure. The most basic uses the two required keywords If and Then.

If (condition) Then Code statement

In the example above, the code statement following Then will execute if condition evaluates as true; otherwise code execution proceeds with the next statement. The entire structure takes just one line of code. It's convenient when you have just one brief code statement that needs to be executed if the condition is true. Multiple statements can be entered on the same line if you separate them with colons (:), but then your code may be hard to read. If you need more than one code statement executed, then for the sake of readability, you should use the block form of If/Then.

If (condition) Then

'Block of code statements End If

Again, the condition must be true or the block of code statements will not execute. When using more than one line in the program editor for If/Then, you must end the structure with End If.

The following procedure is a simple number-guessing game where the computer comes up with a number between 0 and 10 and asks the user for a guess. Three If/Then structures are used to determine what message is output to the user depending on their guess.

Private Sub NumberGuess()

Dim userGuess As Integer Dim answer As Integer answer = Rnd * 10

userGuess = Val(InputBox("Guess a number between 0 and 10.", "Number Guess")) If (userGuess > answer) Then MsgBox ("Too high!") MsgBox ("The answer is " & answer) End If

If (userGuess < answer) Then MsgBox ("Too low!") MsgBox ("The answer is " & answer) End If

If (userGuess = answer) Then MsgBox ("You got it!") End Sub

A random number generated by the Rnd function returns a random number of type single between 0 and 1. The random number is multiplied by 10 and assigned to the variable answer to make it fall between 0 and 10. Using an integer data type for the variable answer ensures that the calculated value is rounded and stored as an integer.

The If/Then structures each use one condition that compares the values stored in the userGuess and answer variables. Only one of these conditions can be true, and the message box in the If/Then structure with the true condition executes.

Previously, you saw the = operator used as an assignment operator. For example, a value is assigned to a variable.

In the context of conditional expressions, the = operator is a comparison operator. Using the same character for more than one type of operation is known as overloading an operator.

If you know you want one block of code executed when a condition is true and another block of code executed when the same condition is false, then use the Else keyword.

If (condition)

'This block of code executes if the condition is true

Else

'This block of code executes if the condition is false. End If

The If/Then structures in the number guess procedure can also be written as follows, where <> is the "not equal" operator (see Table 3.1):

Else

This time, instead of using additional If/Then statements, the keyword Else is used to direct the program to another block of code that is executed if the condition (userGuess <> answer) evaluates to false.

There is no limit on the number of conditions you can use with an If/Then code structure. The condition

If (userGuess <> answer) Then can also be written as

Where the logical operator Or is used in the expression for the conditional. Thus, if only one conditional evaluates as true, then the expression returns true and the logic is maintained. You can use more than two conditionals if needed; however, your code will get harder to read as the number of conditionals in one line of code increases. You will see an excessive use of conditionals in the Poker Dice project at the end of this chapter.

There are numerous possibilities for achieving the same logic when using If/Then/Else and conditionals. You can also nest the If/Then/Else code structure if you want to. The procedure below outputs a short message to the user depending on the current time and day of the week. After some variable declarations, a few familiar date functions are used to determine the current time and day of the week.

Private Sub myTime() Dim time As Date Dim theHour As Integer Dim theDayOfTheWeek As Integer time = Now theHour = Hour(time)

theDayOfTheWeek = Weekday(time) If (theHour > 8) And (theHour < 17) Then

If (theDayOfTheWeek > 0) And (theDayOfTheWeek < 6) Then MsgBox ("You should be at work!")

Else

MsgBox ("I love weekends.") End If

Else

MsgBox ("You should not be at work!") End If End Sub

The first If/Then/Else structure is checking if the time of the day is between 8:00 A.M. and 5:00 P.M., since the variable theHour holds an integer value between 0 and 23. If the expression is true then another If/Then/Else structure will execute. This If/Then/Else structure is nested in the first one and is checking the value for the day of the week. If the day of the week is Monday through Friday, then a message box is used to display the string "You should be at work!". (Remember that it had to be between 8:00 A.M. and 5:00 P.M. to get to this point.) Otherwise, the nested If/Then/Else outputs the message "I love weekends." If the time of day is not between 8:00 A.M. and 5:00 P.M., then the string "You should not be at work!" is displayed in a message box.

There is no limit to the number of nested If/Then statements you can use; however, after three or four levels, keeping track of the logic can be difficult and your program may be difficult to read and debug.

It ¡s a good idea to indent your code with each Level of Logic. You will find your programs much easier to read and debug if indented properly.

Another option regarding If/Then/Else structures is the ElseIf clause. The ElseIf clause is used like the Else clause with a conditional expression. You must also include Then when using ElseIf. The following example uses a series of ElseIf clauses to display the day of the week in a message box.

If (theDayOfTheWeek = 0) Then

MsgBox ("It's Sunday!") ElseIf (theDayOfTheWeek = 1) Then

MsgBox ("It's Monday!") ElseIf (theDayOfTheWeek = 2) Then

MsgBox ("It's Tuesday!") Elself (theDayOfTheWeek = 3) Then

MsgBox ("It's Wednesday!") Elself (theDayOfTheWeek = 4) Then

MsgBox ("It's Thursday!") Elself (theDayOfTheWeek = 5) Then MsgBox ("It's Friday!")

Else

MsgBox ("It's Saturday!") End If

There is no limit to the number of ElseIf clauses that can be used; however, ElseIf cannot be used after an Else clause. You can also nest more If/Then/Else structures inside an ElseIf clause.

## Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook