Saturday, May 26, 2007

VBScript Conditional Statements

Three types of conditional statements are there in VBScript.

if...then...else statement

Example of if without else:

1)

if i=2 Then msgbox "Hello World"

2)

3)

More than one statement can be executed for truth condition by putting the statements on separate line.

if i=2 Then

msgbox "Hello World"

i = i+1

end If

Example of if...then...else

1)

if i=2 then

msgbox "Hello World"


else

msgbox "Thank You"

end If

2)

if...then...elseif statement


1)

if fee="Cash" then

msgbox "pay cash!"

elseif fee="Visa" then

msgbox "pay with visa."

elseif fee="American Express" then

msgbox "pay with American Express."

else

msgbox "Unknown method of Payment."

end If

2)

select case statement

1)

select case fee

case "Cash"

msgbox "pay cash"

case "Visa"

msgbox "pay with visa"

case "American Express"

msgbox "pay with American Express"


case Else

msgbox "Unknown method of fee"

end select


2)

3)

A single expression (usually variable) is evaluated once and its value is then compared with the values for each case. If there is a match, the block of code associated with that case is executed. If there is no match then Else case is executed.