Conditional Statements:


*      You can control the flow of your script with conditional statements and looping statements.

*      Using conditional statements, you can write VBScript code that makes decisions and repeats actions.

*      The following conditional statements are available in VBScript:

  1. If … Then … Statement
  2. If … Else … Then Statement
  3. If … Else If … Else … Then Statement or Nested If
  4. Select … Case … Statement

If … Then Statement:

Description:

*      The If ... Then statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run.

*      Usually the condition is an expression that uses a comparison operator to compare one value or variable with another.

 Syntax:

If Expression Then

          Statement1
          Statement2
          ……………..
          ……………..
          StatementN

End If

Example:

If strName = “Testing” Then

          MsgBox(“Testing”)

End If

If … Else … Then Statement:

Description:

*      The If ... Else … Then statement is used to evaluate whether a condition is True or False and, depending on the result, it will select one branch of statements

*      If Expression is true, it will select first branch of statements and if Expression is False, it selects second set of statements

Syntax:

If Expression Then

          Statement1
          Statement2
          ……………..
          ……………..
          StatementN

Else

          Statement1
          Statement2
          ……………..
          ……………..
          StatementN

End If

Example:

If Num1 > Num2 Then

          MsgBox(“Num1 is Big”)

Else

          MsgBox(“Num2 is Big”)

End If

If … ElseIf … Else … Then Statement or Nested If:

Description:

*      Deciding Between Several Alternatives

*      A variation on the If ... ElseIf … Else … Then statement allows you to choose from several alternatives.

*      Adding ElseIf clauses expands the functionality of the If...Then...Else statement so you can control program flow based on different possibilities.

*      You can add as many ElseIf clauses as you need to provide alternative choices.

*      Extensive use of the ElseIf clauses often becomes cumbersome.
*      A better way to choose between several alternatives is the Select Case statement.

Syntax:

If Expression Then

          Statement1
          Statement2
          ……………..
          ……………..
          StatementN

Else If Expression Then

          Statement1
          Statement2
          ……………..
          ……………..
          StatementN

Else

          Statement1
          Statement2
          ……………..
          ……………..
          StatementN

End If

Example:

If value = 0 Then

                    Message = “zero”

ElseIf value = 1 Then

                    Message = “one”

Else

                    Message = "Value out of range!"

End If 

Select Case Statement or Nested If:

Description:

*      Executes one of several groups of statements, depending on the value of an expression.

*      The Select Case structure provides an alternative to Nested If for selectively executing one block of statements from among multiple blocks of statements.

*      A Select Case statement provides capability similar to the Nested If statement, but it makes code more efficient and readable.

*      A Select Case structure works with a single test expression that is evaluated once, at the top of the structure.

*      The result of the expression is then compared with the values for each Case in the structure.

*      If there is a match, the block of statements associated with that Case is executed.

Syntax:

Select Case Expression

Case Expression1

                   Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

Case Expression2

                   Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

Case Expression3

                   Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

Case Expression4

                   Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

Case Else

          Statement1
                   Statement2
                   ……………..
                   ……………..
                   StatementN

End Select

Example:

Select Case strColor

Case "Red"

                             MsgBox (“Red”)

Case "Blue"

                             MsgBox (“Blue”)

Case "Black"

                             MsgBox (“Orange”)

Case "Blue sky"

                             MsgBox (“Black”)

Case "Yellow"

                             MsgBox (“Green”)

Case Else

                              MsgBox (“Check the Option you Provided or Spelling”)

End Select 

No comments:

Post a Comment