VBScript Conditional Statements
Conditional Statements
Conditional statements are used to perform different actions for different decisions.
In VBScript we have four conditional statements:
- If statement - executes a set of code when a condition is true
- If...Then...Else statement - select one of two sets of lines to execute
- If...Then...ElseIf statement - select one of many sets of lines to execute
- Select Case statement - select one of many sets of lines to execute
If...Then...Else
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 a 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 the example above 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:
Example (IE Only)
<html> <body>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i < 10 Then
document.write("Good morning!")
Else
document.write("Have a nice day!")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html> |
Try it yourself »
|
In the example above, the first block of code will be executed if the condition is true, and the other block
will be executed otherwise (if i is greater than 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:
Example (IE Only)
<html> <body>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i = 10 Then
document.write("Just started...!")
ElseIf i = 11 then
document.write("Hungry!")
ElseIf i = 12 then
document.write("Ah, lunch-time!")
ElseIf i = 16 then
document.write("Time to go home!")
Else
document.write("Unknown")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html> |
Try it yourself »
|
Select Case
You can also use the "Select Case" statement if you want to select one of many blocks of
code to execute:
Example (IE Only)
<html> <body>
<script type="text/vbscript">
d=weekday(date) Select Case d
Case 1
document.write("Sleepy Sunday")
Case 2
document.write("Monday again!")
Case 3
document.write("Just Tuesday!")
Case 4
document.write("Wednesday!")
Case 5
document.write("Thursday...")
Case 6
document.write("Finally Friday!")
Case else
document.write("Super Saturday!!!!")
End Select
</script>
</body>
</html> |
Try it yourself »
|
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.
Make your web applications look like a million bucks
|
|
Most web applications today use boring methods to present data to their viewers using grids or simple HTML tables. FusionCharts induces "life" into the web applications by converting monotonous data into lively charts, gauges & maps.
FusionCharts works with all technologies like ASP, ASP.NET, PHP, ColdFusion, Ruby on Rails, JSP, HTML pages etc.
and connects to any database to render animated & interactive charts. It takes less than 15 minutes and no expertise
whatsoever to build your first chart and just a glance of it to captivate your audience. This fact is endorsed by our
12,000 customers and 150,000 users which include a majority of the Fortune 500 companies.
And yeah, your applications could look like a million bucks by spending just $69.
So go ahead, download your
copy of FusionCharts and start "wow-ing" your customers now!
|
 |
W3Schools' Online Certification Program
The perfect solution for professionals who need to balance work, family, and career building.
More than 4000 certificates already issued!
|
The HTML Certificate documents your knowledge of HTML, XHTML, and CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|