- VBScript Tutorial
- VBScript - Home
- VBScript - Overview
- VBScript - Syntax
- VBScript - Enabling
- VBScript - Placement
- VBScript - Variables
- VBScript - Constants
- VBScript - Operators
- VBScript - Decisions
- VBScript - Loops
- VBScript - Events
- VBScript - Cookies
- VBScript - Numbers
- VBScript - Strings
- VBScript - Arrays
- VBScript - Date
- VBScript Advanced
- VBScript - Procedures
- VBScript - Dialog Boxes
- VBScript - Object Oriented
- VBScript - Reg Expressions
- VBScript - Error Handling
- VBScript - Misc Statements
- VBScript Useful Resources
- VBScript - Questions and Answers
- VBScript - Quick Guide
- VBScript - Useful Resources
- VBScript - Discussion
Switch Statements in VBScript
When a user wants to execute a group of statements depending upon a value of an expression, then he can use Select Case statements. Each value is called a Case, and the variable being switched ON based on each case. Case Else statement is executed if test expression doesn't match any of the Case specified by the user.
Case Else is an optional statement within Select Case, however, it is a good programming practice to always have a Case Else statement.
Syntax
The syntax of a Select Statement in VBScript is −
Select Case expression Case expressionlist1 statement1 statement2 .... .... statement1n Case expressionlist2 statement1 statement2 .... .... Case expressionlistn statement1 statement2 .... .... Case Else elsestatement1 elsestatement2 .... .... End Select
Example
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim MyVar MyVar = 1 Select case MyVar case 1 Document.write "The Number is the Least Composite Number" case 2 Document.write "The Number is the only Even Prime Number" case 3 Document.write "The Number is the Least Odd Prime Number" case else Document.write "Unknown Number" End select </script> </body> </html>
In the above example, the value of MyVar is 1. Hence, Case 1 would be executed.
The Number is the Least Composite Number
vbscript_decisions.htm
Advertisements