Quackit Logo
FREE Hosting!
With every domain you register with ZappyHost you get FREE hosting.
Create free Flash websites

VBScript If Statements

Print Version

VBScript If statements are a handy tool for any VBScript programmer. In fact, If statements are a universal to all programming languages (that I'm aware of).

An If Statement allows you to tell your program to do something only if a condition is true.

For example, you might have a website that asks the user what color their hair is. You could write code that only executes if their hair is say, black. OK, that's a pretty lame example but you get the idea!

Example If Statement

In this example, we'll stick with the lame subject of hair color, but to keep it simple, we won't ask for user input. We'll just set a variable with a hair color values.

<script type="text/vbscript">
  Dim hairColor
  hairColor = "Black"
  If hairColor = "Black" Then
    document.write("Same color as my cat!")
  End If
</script>

This results in:

Same color as my cat!

If Else Statement

An If Else statement is an extension to the If statement. This allows you to tell the program to do something else in the event that the condition is not true.

<script type="text/vbscript">
  Dim hairColor
  hairColor = "Blue"
  If hairColor = "Black" Then
    document.write("Same color as my cat!")
  Else
    document.write("Oh well, whatever...")
  End If
</script>

This time the value of hairColor is blue. Because it is not black, the Else part of the statement is executed.

This results in:

Oh well, whatever...

Elseif Statement

You can add an Elseif to test for another condition if you like. You could do as many of these as you like too.

<script type="text/vbscript">
  Dim hairColor
  hairColor = "Blue"
  If hairColor = "Black" Then
    document.write("Same color as my cat!")
  Elseif hairColor = "Blue" Then
     document.write("Same color as my parrot!")
  Else
    document.write("Oh well, whatever...")
  End If
</script>

This time the 'Elseif' code is executed because the value of the hairColor variable is blue.

This results in:

Same color as my parrot!

Enjoy this website?

  1. Add this page to your Favorites
  2. Link to this page (copy/paste into your own website or blog):
  3. Help support Quackit by making a donation

Oh, and thank you for supporting Quackit!

Need Website Content?
Get unique, quality digital content for your website.
  • 270+ Website Templates
  • 800+ Flash Templates
  • 25,000+ Images, Logos
  • 30,000+ e-Books
  • 15,000+ Scripts
  • 27,000+ Animated GIFs
  • 21,000+ Ringtones
  • ...and much more!
Get your content now!
$1.99 .info domain names!
© Copyright 2000 - 2010 Quackit.com