Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » Conditional Statements in VB Scripting

We can control the flow of our script with conditional statements and looping statements.

Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:

1) If... Then...Else Statement
2) Select Case Statement

8.1 Making Decisions Using If...Then...Else

The If...Then...Else 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.

If...Then...Else statements can be nested to as many levels as you need.

8.1.1 Running a Statement if a Condition is True (single statement)

To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement.

Dim myDate
myDate = #2/13/98#
If myDate < Now Then myDate = Now 8.1.2 Running Statements if a Condition is True (multiple statements) To run more than one line of code, we must use the multiple-line (or block) syntax. This syntax includes the End If statement. Dim x x= 20 If x>10 Then
msgbox "Hello World"
msgbox "x value is: "&x
msgbox "Bye Bye"
End If

8.1.3 Running Certain Statements if a Condition is True and Running Others if a Condition is False

We can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.
Example:

Dim x
x= Inputbox (" Enter a value")
If x>100 Then
Msgbox "Hello World"
Msgbox "X is a Big Number"
Msgbox "X value is: "&X
Else

Msgbox "X is a Small Number"
Msgbox "X value is: "&X
End If

8.1.4 Deciding Between Several Alternatives

A variation on the If...Then...Else statement allows us to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so we can control program flow based on different possibilities.
Example:
Dim x
x= Inputbox (" Enter a value")

If x>0 and x<=100 Then Msgbox "Hello World " Msgbox "X is a Small Number" Msgbox "X value is "&x Else IF x>100 and x<=500 ThenMsgbox "X is a Medium Number" Else IF x>500 and x<=1000 Then Msgbox "Hello World war2" Msgbox "X is a Large Number" Else Msgbox "Hello Sir" Msgbox "X is a Grand Number" End If End If End If 8.1.5 Executing a certain block of statements when two / more conditions are True (Nested If...) Example: Dim no, grade no=Inputbox ("Enter a no") grade =Inputbox ("Enter a Grade") If no>60 Then
If grade = "First" Then
msgbox "Hello World this is a first class degree "


Else if grade = "Second" Then

msgbox "Hello World this is a Second class degree "

Else If grade = "Third" Then


msgbox "Hello World this is a Third class degree "


End If
End If
End If
End If

8.2 Making Decisions with Select Case

The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable.
Example:

Option explicit
Dim x,y, Operation, Result
x= Inputbox (" Enter x value")
y= Inputbox ("Enter y value")
Operation= Inputbox ("Enter an Operation")

Select Case Operation

Case "add"
Result= cdbl (x)+cdbl (y)

Msgbox "Addition of x,y values is "&Result

Case "sub"
Result= x-y

Msgbox "Substraction of x,y values is "&Result

Case "mul"
Result= x*y

Msgbox "Multiplication of x,y values is "&Result

Case "div"
Result= x/y

Msgbox "Division of x,y values is "&Result

Case "mod"
Result= x mod y

Msgbox "Mod of x,y values is "&Result

Case "expo"
Result= x^y

Msgbox"Exponentation of x,y values is "&Result

Case Else

msgbox "Wrong Operation"

End Select

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply