VBScript
VBScript introduction
VBScript basics
VBScript variables
VBScript procedures
VBScript popup boxes
VBScript conditions
VBScript loops
VBScript arrays
VBScript strings
VBScript date & time
VBScript summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

VBScript conditional logic

You can implement VBScript code which makes decisions based on certain things using conditional logic.

This lesson focuses on:

The if statement

The if statement tests if a certain condition is true or false and acts upon it accordingly.

Syntax:

if condition then

perform this action

end if

If the specified condition is true, then the code following the condition will be executed, otherwise it will not.

Example:

<script type="text/vbscript">
Dim aNumber 
aNumber= 5

if aNumber = 5 then

document.write("aNumber is equal to 5")

end if
</script>

Output:

aNumber is equal to 5

The else statement

If the condition in an if statement is true, then the code following the condition will be executed. But what if you wanted something to happen if it is false? What if you wanted one thing to happen if the condition is true, and something else to happen if the condition is false? This is where the else statement comes in. The else statement works in conjunction with the if statement and executes certain code if the condition in the if statement is false.

Syntax:

if condition then

perform this action

else

perform this action if the above condition is false

end if

If the condition in the if statement is false, then the action dictated by the else statement will be performed.

Example 1:

<script type="text/vbscript">
Dim plant 
plant = "cactus"

if plant = "cactus" then

document.write("Great choice!")

else

document.write("Still a good choice, though cactuses 
are better.")

end if
</script>

Output:

Great Choice!

Example 2:

<script type="text/vbscript">
Dim plant = "birch"

if plant = "cactus" then

document.write("Great choice!")

else

document.write("Still a good choice, though cactuses 
are better.")

end if
</script>

Output:

Still a good choice, though cactuses are better.

The elseif statement

The if statement tests a single condition and performs an action if that condition is true and the else statement performs an action if the condition in the if statement is false, but what if there are more than two possibilities? Surely, any condition can be only true or false, but what if you needed to test a variable for more than one value? This is where the elseif statement comes in. The elseif statement is used in conjunction with the if statement. Unlike the else statement, it does not specifically perform a certain action if the condition in the if statement is false, but rather it performs an action if the condition in the if statement is another specific value specified in the elseif statement itself.

Syntax:

if condition is one value then

perform this action

elseif condition equals another value then

perform this action

end if

Example:

<script type="text/vbscript">
Dim X 
X = 7

if X = 5 then

document.write("X is equal to 5")

elseif X = 7 then

document.write("X is equal to 7")

end if
</script>

Output:

X is equal to 7

In the above example, we are testing if the variable X is equal to 5, and if it is, print the message "X is equal to 5". We then use the elseif statement to test if the variable X is equal to 7, and if it is, print the message "X is equal to 7".

Using if, else, and elseif together

You can use the if, else, and elseif statements together when you want to check a variable for a certain value many times. If it is not any of the checked values then the code specified by the else statement will be executed.

Syntax:

if condition then

perform this action

elseif condition equals another value then

perform this action

elseif condition equals another value then

perform this action

elseif condition equals another value

perform this action

else

if the condition was not equal to any of the values tested in 
the if statement and all the elseif statements, then perform 
this action

end if

Example:

<script type="text/vbscript">
Dim X 
X = 5

if X = 9 then

document.write("X is equal to 9")

elseif X = 7 then

document.write("X is equal to 7")

elseif X = 3 then

document.write("X is equal to 3")

elseif X = 15 then

document.write("X is equal to 15")

else

document.write("X is not equal to 9, 7, 3, or 15. X is equal 
to " & X);

end if
</script>

Output:

X is not equal to 9. 7, 3, or 15. X is equal to 5

The Select statement

The Select statement is specifically designed for comparing one variable to a number of possible values. It can be thought of as a substitute for the if, elseif, else structure.

Syntax:


select case variable

case possible value
perform this action

case possible value
perform this action

case possible value
perform this action

case possible value
perform this action

case else
perform this action if none of the values match

end select

Example:

<script type="text/vbscript">
Dim X 
X = 7

select case X

case 1
document.write("X is equal to 1")

case 2
document.write("X is equal to 2")

case 3
document.write("X is equal to 3")

case 7
document.write("X is equal to 7")

case else
document.write("X is not equal to 
any of the values specified")

end select
</script>

Output:

X is equal to 7

In the above example, the variable X is initialized to 7, and is tested for a number of values. If X is equal to 1, the message "X is equal to 1" will be printed. If X is equal to 2, the message "X is equal to 2" will be printed. If X is equal to 3, the message "X is equal to 3" will be printed. If X is equal to 7, the message "X is equal to 7" will be printed. If X is equal to neither 1, 2, 3, or 7, the message "X is not equal to any of the values specified" will be printed.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments