• No results found

It is com mon to want to do one thing un der certain circumstances and something else un der other circumstances. If it is raining, we will play in side. Otherwise, we will play out side. We like to do this in VBA as well. We might say:

If rain ing = True Then PlayInside

Else

PlayOutside End If

TheIf statement asks a question. If the answer is yes, we do the first thing. If the answer is no, we do what co mes af ter theElse. The above code is exactly the same as the English sentences:

If it is rain ing Then We will play in side Oth er wise

We will play out side

The question can be any thing that re turns aTrue orFalse an swer. We might compare the value of a vari able to something. For ex ample:

If numCorrect > 6 Then

MsgBox("You got a lot of ques tions right.") Else

MsgBox("You can do better than that.") End If

In this case, if the variablenumCorrect (pre sumably that was used by some other procedures to count the num ber of questions that were an swered correctly) is greater than 6, a MsgBox will pop up say ing “You got a lot of questions right.” If the vari ablenumCorrect is not greater than 6 (it is 6 or less), then theMsgBox

will say “You can do better than that.”

This can be extended to check more than one thing usingElseIf. You might say: if it is rain ing, we will play in side; if it is snow ing, we will build snowmen; oth erwise, we will play baseball.

If rain ing = True Then PlayInside

ElseIf snow ing = True Then BuildSnowmen

Else

PlayBaseball End If

In this case, we ask one ques tion. If the answer is yes, we do the first thing. If the answer is no, we ask a second ques tion. If the answer to the second ques tion is yes, we do the second thing. If the answer to the first question is no, and the an - swer to the second ques tion is no, we do the third thing. Note, we can ask as many questions as we want by putt ing more and moreElseIf state ments. Imag- ine a grad ing pro gram that converts num bers to letter grades:

Sub WhatsMyGrade() If gradeNum >= 90 Then MsgBox("You got an A") ElseIf gradeNum >= 80 Then MsgBox("You got a B") ElseIf gradeNum >= 70 Then MsgBox("You got a C") ElseIf gradeNum >= 60 Then MsgBox("You got a D") Else

MsgBox("You got an F") End If

End Sub

This assumes that a variable namedgradeNum has been given a value some- where else. It then asks the question, is this grade greater than or equal to 90? If the answer is yes, it pops up a box with the message “You got an A,” and it stops. However, if the an swer is no, it asks the next question: is this grade greater than or equal to 80? If the answer to this ques tion is yes, it pops up a box with the mes- sage “You got a B,” and it stops. It keeps asking questions as long as the an swers are no. If all the answers are no, it reaches the Else statement and pops up a box with the message, “You got an F.”

Note that you can do more than one thing in re sponse to a yes an swer. You might, for ex ample, pop up aMsgBox and then move to the next slide un der one condition, but pop up a dif ferentMsgBox and then move to the previous slide un - der a dif ferent condition:

If gradeNum >= 90 Then MsgBox ("You got an A.")

ActivePresentation.SlideShowWindow.View.Next Else

MsgBox ("You need to work harder.")

ActivePresentation.SlideShowWindow.View.Pre vious End If

Because you can do sev eral things in re sponse to a yes an swer, you can do sev - eral complicated things. The above ex ample uses two simple statements, but you can have as many statements as you want. Some of these state ments might be complicated structures like loops (see the next section) and other If state ments. When you put anIf block in side an If block, it is called a nested If. If the an - swer to your question is yes, you might want to ask other questions:

If gradeNum >= 90 Then MsgBox ("You got an A.") If previousGradeNum >= 90 Then

MsgBox ("Good job. Two A grades in a row!") End If

ActivePresentation.SlideShowWindow.View.Next Else

MsgBox ("You need to work harder.")

ActivePresentation.SlideShowWindow.View.Pre vious End If

Pay careful attention to the way this example is in dented. Al though you don’t have to type it in dented in this way, it is much easier to un derstand with the in dent- ing. You can see that the question is asked: Is gradeNum greater than 90? Every- thing be tween the firstIf and theElse is in dented to show that it is what to do if the answer is yes. Part of what to do is to ask an other question. That question asks ifpreviousGradeNum also is greater than 90. This question will only get asked ifgradeNum is greater than 90. The in denting helps to see the nesting. It is partic- ularly help ful if the nestedIf block is more com plicated, with its own Else, for ex am ple. TheElse should always be lined up with theIf with which it goes.

TheIf statement is very pow erful. It is one of the things that al lows for in - teraction. Without con ditional statements, every user would do ex actly the same thing as the previous user.

Loop ing

If statements allow you to make choices based on whether or not a condi- tion is true. Loop ing al lows you to do something over and over again. How many times is based on a con dition, that is, a question like what you ask in an If state - ment. This is known as the stop ping con dition. In some types of loops (such as a

While loop), this question is phrased as a keep go ing ques tion, and in other types of loops (such as a For Next loop), the con dition is based on how many times you say you want to loop. However the question is phrased, the loop needs to know when to stop.