3. VbScript | If – ElseIf – Else -Then Statements

Function: Option Explicit

I want to begin by talking about a function called:

The Vb Programmer should add this to the very beginning or top of his script to enable the Vb Script to check itself that all the variables are defined, spelled correctly, and that you aren’t re-using them. This is valuable because if you have an error, the VbScript will tell you which line of code the error is at and…

What does Option Explicit do? VbScript Tutorials

… find variables that aren’t previously Declared. This is important because a miss-spelled variable will appear as a separate-new variable to the VbScript. But is not the variable that you may have meant to use. You can declare the variables with Dim & the variable next to it:

What does Option Explicit do? Part 2

If – ElseIf – Else -Then Statements

Every time you open an If statement, you want to end that if with End If.

If you have more than 2 options for your If statement, then you add ElseIf for every additional option. So say you have 3 If statements. It would look like this:

Syntax: Note that the single quote ‘ comments out what comes after it.

For each If statement, there must be a Then after it to complete the clause.

You also have the option of using Else instead of ElseIf at the end, if you want the last condition to trigger only if nothing else triggers.

More on Syntax Errors:

ElseIf Syntax Errors in VbScript


An Else example:

Output:
finito

ElseIf examples with AND & OR Functions: OR

Output:
Untitled

AND example:

Output:
yesno


What's Your Opinion?