www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons VBScript Else If and Elseif Statements Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14

In the previous lesson we learned that the If statement allows us to execute some piece of code depending upon the result of some condition being true.

However, sometimes we have two pieces of code and we wish to execute one if the condition is true and the other if the condition is false.

In such situtations we use the If Else statement. The If Else is an extension to the If statement that allows us to execute a piece of code if the condition is true, in case the condition is flase a different block of code gets executed.

Here is an example:

<script type="text/vbscript">
Dim a
Dim b
a = 30
b = 20
If a < b Then
document.write("A is less than B")
Else
document.write("A is greater than B")
End If
</script>

Here is the output:

A is greater than B

The result of the If condition above a > b evaluates to false and therefore the Else part of the code gets executed that generates the above output.

There is another variation of the If statement called Elseif. We use Elseif when we need to check multiple conditions.

Here is an example:

<script type="text/vbscript">
Dim a
Dim b
a = 30
b = 20
If a < b Then
document.write("A is less than B")
Elseif a > b Then
document.write("A is greater than B")
Else
document.write("A and B are equal")
End If
</script>

Here is the output:

A is greater than B

The Elseif also takes a condition. As a is greater than b in our above code the Elseif part get executed. Note that we may have as many lines of code in any or all of the code blocks above as we need to execute.


Next >>> Lesson No. 10: VBScript Select Statement




Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.