VBScript allows you to control how your scripts process data through the use of conditional and looping statements. By using conditional statements you can develop scripts that evaluate data and use criteria to determine what tasks to perform. Looping statements allow you to repetitively execute lines of a script. Each offers benefits to the script developer in the process of creating more complex and functional web pages.
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.
if statement :
if statement is used when you want to execute a set of code when a condition is true.
if...then...else statement :
if…then…else statement is used when you want to select one of two sets of lines to execute.
if...then...elseif statement :
if...then...elseif statement is used when you want to select one of many sets of lines to execute.
select case statement :
select case statement is used when you want to select one of many sets of lines to execute
If....Then
If you want to execute only one statement when a condition is true, you can write the code on one line:
if i>0 Then msgbox "Positive Number"
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>0 Then
msgbox " Positive Number"
i = i+1
end If
If....Then….Else
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>0 then
msgbox "Positive Number"
else
msgbox "Negative Number"
end If
The first block of code will be executed if the condition is true, and the other block will be executed otherwise.
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 percentage>=80 then
msgbox "Your Grade is A."
elseif percentage>=70 then
msgbox "Your Grade is B."
elseif percentage>=60 then
msgbox "Your Grade is C."
else
msgbox "Your Grade is D."
end If
Select Case
The Select Case statement provides an alternative to the If..Then..Else statement, providing additional control and readability when evaluating complex conditions. It is well suited for situations where there are a number of possible conditions for the value being checked. Like the If statement the Select Case structure checks a condition, and based upon that condition being true, executes a series of statements.
Example
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.