w3schools
Search W3Schools :  
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
ADVERTISEMENT

Best Web Hosting
FREE Web Hosting
Web Hosting UK

VBScript Tutorial

VB HOME
VB Introduction
VB How to
VB Where to
VB Variables
VB Procedures
VB Conditional
VB Looping
VB Summary

VBScript Examples

VB Examples

VBScript References

VB Functions
VB Keywords

 

VBScript Conditional Statements

previous next

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 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.


previous next


Altova® XMLSpy® - The industry-leading XML editor!

Altova XMLSpy

Whether you're new to XML or already an advanced user, the user-friendly views and powerful entry helpers, wizards, and debuggers in XMLSpy are designed to meet your XML and Web development needs from start to finish.

  • XML editor
  • Graphical XML Schema / DTD editors
  • XSLT 1.0/2.0 editor, debugger, profiler
  • XQuery editor, debugger, profiler
  • XBRL validator & taxonomy editor
  • Support for Office Open XML (OOXML)
  • Graphical WSDL editor & SOAP debugger
  • Java, C#, C++ code generation
  • And much more!

Download a free trial today!

  Altova XMLSpy

 
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Get a Freelancer
Download XML editor
FREE Flash Website
FREE Web Templates
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display
W3Schools.com HOME | TOP | PRINT | FORUM | ABOUT
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2009 by Refsnes Data. All Rights Reserved.