Bookmark and Share

VBScript ElseIf

In the previous lesson you learned how to create an If Statement and make use of the Else clause. However, an If Statement is not always enough for a programmer's needs.

Advertise on

VBScript ElseIf: Uses

The previous lesson contained an example that let you execute one block of code if the temperature was above a certain temperature and another block of code if it was below a certain temperature. How would we check for another temperature range? For this kind of programming solution you need to use the ElseIf statement.

The basic idea of the ElseIf statement is to create an If Statement within another If Statement. This way you can check for many different cases in a single If Statement clause.

Our following example shows how you would check for two additional temperature ranges to make our program more robust!

VBScript Code:

<script type="text/vbscript">
Dim temperature
temperature = 65
If temperature > 70 Then
	document.write("Wear a T-Shirt!")
ElseIf	temperature > 60 Then
	document.write("Wear a hat!")
ElseIf	temperature > 50 Then
	document.write("Wear a long-sleeved shirt!")
	document.write("Wear a coat!")
End If



Wear a hat!

Our temperature was set to 65 and failed the first If Statement condition because 65 is not greater than 70. However, on the first ElseIf Statement we found that 65 was greater than 60 and so that block of code document.write("Wear a hat!") was executed. Because there was a success the If Statement then finishes and our VBSCript would begin again on the line of code following End If

Bookmark and Share

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials