VBScript Tutorial

Example

If...then..else statements
This example demonstrates how to write if...then..else statements.
If...then..elseif statements
This example demonstrates how to write if...then...elseif... statements.
Select case statement
This example demonstrates how to write a select case statement.

Conditional statements

Frequently, when we write code, we need to perform different operations based on different judgments. We can use conditional statements to accomplish this work.

In VBScript, we can use three conditional statements:

if statement
If you want to execute a series of codes when the condition is true, you can use this statement.
if...then...else statement
If you want to execute one of two code sets, you can use this statement.
if...then...elseif statement
If you want to select one of multiple code sets to execute, you can use this statement.
select case statement
If you want to select one of multiple code sets to execute, you can use this statement.

If....Then.....Else

In the following situations, you can use the If...Then...Else statement:

  • Execute a block of code when the condition is true
  • Select one of the two code blocks to execute

If you need to execute only one line of code when the condition is true, you can write the code on one line:

if i=10 Then msgbox "Hello"

In the above code, there is no .else.. statement. We simply made the code execute one operation when the condition is true (when i is 10).

If we need to execute more than one statement when the condition is true, we must write one statement per line and then use the keyword "End If" to end this statement:

if i=10 Then
   msgbox "Hello"
   i = i+1
end If

In the above code, there is no .else.. statement. We simply made the code execute multiple operations when the condition is true.

If we want to execute a statement when the condition is true and another statement when the condition is not true, we must add the keyword "Else":

if i=10 then
   msgbox "Hello"
else
   msgbox "Goodbye"
end If

The first block of code will be executed when the condition is true, and the second block of code will be executed when the condition is not met (when i is not equal to 10).

If....Then.....Elseif

If you want to select one of multiple code sets to execute, you can use the if...then...elseif statements:

if payment="Cash" then
   msgbox "You are going to pay cash!"
 elseif payment="Visa" then
   msgbox "You are going to pay with visa."
 elseif payment="AmEx" then
   msgbox "You are going to pay with American Express."
 else
   msgbox "Unknown method of payment."
end If

Select Case

If you want to select one of multiple sets of code to execute, you can use the SELECT statement:

select case payment
 case "Cash"
   msgbox "You are going to pay cash"
 case "Visa"
   msgbox "You are going to pay with visa"
 case "AmEx"
   msgbox "You are going to pay with American Express"
 case Else
   msgbox "Unknown method of payment"
end select

The working principle of the above code: First, we need a simple expression (often a variable), and this expression will be evaluated once. Then, the value of the expression will be compared with the value in each case, and if a match is found, the code corresponding to the matched case will be executed.