Please visit our sponsors !
VBScript Conditional Statements
Examples
The if...then...else statement
This example demonstrates how to write the if...then..else statement.
The if...then...elseif... statement
This example demonstrates how to write the if...then...elseif statement.
The select case statement
This example demonstrates how to write the select case statement.
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In VBScript we have three conditional statements:
- if...then...else statement - use this statement if you want to
select one of two sets of lines to execute
- if...then...elseif statement - use this statement if you want to
select one of many sets of lines to execute
- select case statement - use this statement if you want to select one of many sets
of lines to execute
If....Then.....Else
You should use the If...Then...Else statement if you want to
- execute some code if a
condition is true
- select one of two blocks of code to execute
If you want to execute only one statement when a condition is true, you
can write the code on one line:
if i=10 Then msgbox "Hello"
|
There is no ..else.. in this syntax. You just tell the code to
perform one action if the condition is true (in this case if i=10).
If you want to execute more than one statement when a condition is true,
you must put each statement on separate lines and end the statement with the
keyword "End If":
if i=10 Then
msgbox "Hello"
i = i+1
end If
|
There is no ..else.. in this syntax either. You just tell the
code to perform multiple actions if the condition is true.
If you want to execute a statement if a condition is true and execute another
statement if the condition is not true, you must add the "Else"
keyword:
if i=10 then
msgbox "Hello"
else
msgbox "Goodbye"
end If
|
The first block of code will be executed if the condition is true, and the other block
will be executed otherwise (if i is not equal to 10).
If....Then.....Elseif
You can use the if...then...elseif statement if you want to select one of many blocks of
code to execute:
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
You can also use the SELECT statement if you want to select one of many blocks of
code to execute:
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
|
This is how it works: First we have a single expression (most often a
variable), that is evaluated once. The value of the expression is then compared with the
values for each Case in the structure. If there is a match, the block of code
associated with that Case is executed.
Jump to: Top of Page
or HOME or
Printer friendly page
Search W3Schools:
What Others Say About Us
Does the world know about us? Check out these places:
Dogpile
Alta Vista
MSN
Google
Excite
Lycos
Yahoo
Ask Jeeves
We Help You For Free. You Can Help Us!
W3Schools is for training only. We do not warrant its correctness or its fitness for use.
The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 1999-2002 by Refsnes Data. All Rights Reserved
|