VBScript
  1. VBScript intro
  2. VBScript basics
  3. VBScript variables
  4. VBScript procedures
  5. VBScript popup boxes
  6. VBScript conditions
  7. VBScript loops
  8. VBScript arrays
  9. VBScript strings
  10. VBScript date & time
  11. VBScript for IE 11
  12. VBScript summary

VBScript conditional logic

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

This tutorial 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 together 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 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

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
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information