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

XML Certification
Download XML editor
Custom Programming
 
Table of contents
VBScript Tutorial
VB HOME
VB Introduction
VB How to
VB Where to
VB Variables
VB Procedures
VB Conditional
VB Looping
VB Summary

Examples
VB Examples

References
VB Functions
VB Keywords

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Tutorials
W3Schools Forum

Helping W3Schools

 

VBScript Conditional Statements

previous next

Examples

The if...then...else statement
This example demonstrates how to write the if...then..else statement.

The if...then...elseif... statement
This example demonstrates how to write the if...then...elseif statement.

The select case statement
This example demonstrates how to write the select case statement.


Conditional Statements

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

In VBScript we have four conditional statements:

  • if statement - use this statement if you want to execute a set of code when a condition is true
  • if...then...else statement - use this statement if you want to select one of two sets of lines to execute
  • if...then...elseif statement - use this statement if you want to select one of many sets of lines to execute
  • select case statement - use this statement if you want to select one of many sets of lines to execute

If....Then.....Else

You should 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 the 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 this syntax 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:

if i=10 then
   msgbox "Hello"
else
   msgbox "Goodbye"
end If

The first block of code will be executed if the condition is true, and the other block will be executed otherwise (if i is not equal to 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:

if payment="Cash" then
   msgbox "You are going to pay cash!"
 elseif payment="Visa" then
   msgbox "You are going to pay with visa."
 elseif payment="AmEx" then
   msgbox "You are going to pay with American Express."
 else
   msgbox "Unknown method of payment."
end If


Select Case

You can also use the SELECT statement if you want to select one of many blocks of code to execute:

select case payment
 case "Cash"
   msgbox "You are going to pay cash"
 case "Visa"
   msgbox "You are going to pay with visa"
 case "AmEx"
   msgbox "You are going to pay with American Express"
 case Else
   msgbox "Unknown method of payment"
end select

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


Learn XML with <oXygen/> XML Editor - Free Trial!

oXygen - Probably The World's Best XML Editor   

oXygen helps you learn to define, edit, validate and transform XML documents. Supported technologies include XML Schema, DTD, Relax NG, XSLT, XPath, XQuery, CSS.

Understand in no time how XSLT and XQuery work by using the intuitive oXygen debugger!

Do you have any XML related questions? Get free answers from the oXygen XML forum and from the video demonstrations.

Download a FREE 30-day trial today!


 
WEB HOSTING
E Components
Ecommerce
Components
for ASP.NET
$15 Domain Name
Registration
Save $20 / year!
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap 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
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
Home HOME or Top of Page Validate   Validate   W3C-WAI level A conformance icon Printer Friendly  Printer Friendly

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.