VBSCRIPT CONDITIONAL STATEMENTS


Conditional statements are used to perform different actions for different decisions.
In VBScript we have four conditional statements:
  • If statement - executes a set of code when a condition is true
  • If...Then...Else statement - select one of two sets of lines to execute
  • If...Then...ElseIf statement - select one of many sets of lines to execute
  • Select Case statement - select one of many sets of lines to execute
The If...Then statement examines the truthfulness of an expression.
SYNTAX:
If ConditionIsTrue Then Statement
Therefore, the program will examine a Condition. This condition can be a simple expression or a combination of expressions. If the Condition is true, then the program will execute the Statement.
There are two ways you can use the If...Then statement. If the conditional formula is short enough, you can write it on one line, like this:
If Condition Then Statement
If there are many statements to execute as a truthful result of the condition, you should write the statements on alternate lines. .
one very important rule to keep is to terminate the conditional statement with End If.
example:
If Condition Then
    Statement
End If
example:
If Condition Then
    Statement1
    Statement2
    Statementn

End If
Example:
If i=10 Then response.write("Hello")
Example:
If i=10 Then
response.write("Hello")
i = i+1
End If
If...Then...Else statement -:


The If...Then statement offers only one alternative: to act if the condition is true.
Whenever we would like to apply an alternate expression in case the condition is false, use the If...Then...Else statement. T
Syntax:
If ConditionIsTrue Then
    Expression1
Else
    Expression2
End If

Example:
i=hour(time)
If i < 10 Then
response.write("Good morning!")
Else
response.write("Have a nice day!")
End If



If...Then...ElseIf statement -:
The If...Then...ElseIf statement acts like the If...Then...Else, except that it offers as many choices as necessary.
Syntax:
If Condition1 Then
    Statement1
ElseIf Condition2 Then
    Statement2
ElseIf Conditionk Then
    Statementk
End If

The program will first examine Condition1. If Condition1 is true, the program will execute Statment1 and stop examining conditions. But if Condition1 is false, the program will examine Condition2 and act accordingly. Whenever a condition is false, the program will continue examining the conditions until it finds one. Once a true condition has been found and its statement executed, the program will terminate the conditional examination at End If.
Example:

Example

i=hour(time)
If i = 10 Then
response.write("Just started...!")
ElseIf i = 11 Then
response.write("Hungry!")
ElseIf i = 12 Then
response.write("Ah, lunch-time!")
ElseIf i = 16 Then
response.write("Time to go home!")
Else
response.write("Unknown")
End If



Select Case statement :
If we have a large number of conditions to examine, the If...Then...Else will go through each one of them, which could take long (although usually transparent to the user).
VBScript offers the alternative of jumping to the statement that applies to the state of the condition.
Syntax of the Select Case is:
Select Case Expression
    Case Expression1
        Statement1
    Case Expression2
        Statement2
    Case Expressionk
        Statementk
End Select

The interpreter will examine the Expression and evaluate it once. Then it will compare the result of this examination with the Expression of each case. Once it finds one that matches, it would execute the corresponding Statement.
If no match is found between the Expression and one of the Expressions, use a Case Else statement at the end of the list. The statement would then look like this:
Select Case Expression
    Case Expression1
        Statement1
    Case Expression2
        Statement2
    Case Expressionk
        Statementk    Case Else
        Statementk
End Select

Example:

Example

d=weekday(date)
Select Case d
  Case 1
    response.write("Sleepy Sunday")
  Case 2
    response.write("Monday again!")
  Case 3
    response.write("Just Tuesday!")
  Case 4
    response.write("Wednesday!")
  Case 5
    response.write("Thursday...")
  Case 6
    response.write("Finally Friday!")
  Case else
    response.write("Super Saturday!!!!")
End Select



No comments:

Post a Comment