UFT/QTP – VBScript Tutorial Part -2

Operators

VBScript has a full range of operators, including arithmetic operators, comparison operators, concatenation operators, logical operators.

Operator Precedence
When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. You can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained.
When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. Arithmetic and logical operators are evaluated in the following order of precedence.

Arithmetic Comparison Logical
Description Symbol Description Symbol Description Symbol
Exponentiation ^ Equality = Logical negation Not
Unary negation Inequality <> Logical conjunction And
Multiplication * Less than < Logical disjunction Or
Division / Greater than > Logical exclusion Xor
Integer division \ Less than or equal to <=

 

Logical equivalence Eqv
Modulus arithmetic Mod Greater than or equal to >=

 

Logical implication Imp
Addition + Object equivalence Is
Subtraction
String concatenation &

Conditional Statements

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

  • If Then Statement
  • If Else Statement
  • If Elseif Statement
  • Select Case statement

VBScript If Then Statement – If you want to execute some code when a specific condition is true, use If then  statement.

Example –

SomeDate = #08/08/2016#
‘Check if the given date was in the past
If SomeDate < Now Then
msgbox “The given date is in past”
End If

You can check multiple conditions within a single If statement using And & Or operators.

‘Find if a given number is given number if even and is less than 100

Dim iNum
iNum = 40
If (iNum mod 2 = 0) and (iNum < 100) Then
msgbox iNum & ” is a even number less than 100″
End If

VBScript If Then Else Statement – The If Then Else statement evaluates whether a condition is true or false, and then based upon the result certain set of statements are run.

Example –

SomeDate = #08/08/2016#
‘Check if the given date was in the past
If SomeDate < Now Then
msgbox “The given date is in past”
Else
msgbox “The given date is not in past”
End If

VBScript If Then Elseif Statement – Using this statement multiple conditions are evaluated and appropriate block of if/else statements are executed.

Example –

‘Code to check multiple conditions using Else If statement
Dim CurrMonth

‘Find the month from today’s date
CurrMonth = Month(Now)

If CurrMonth = 1 Then
msgbox “Month is January”
ElseIf CurrMonth = 2 Then
msgbox “Month is February”
ElseIf CurrMonth = 3 Then
msgbox “Month is March”
‘….. so on ….
End If

VBScript Select Case Statement  – Select Case statement works on a single expression which is evaluated at the beginning on the Select Case statement. The result of the expression is then compared with the values for each Case in the structure. Select Case statement is similar to If Then Else but it makes the code more readable.

Example –

Dim iWeek

iWeek= 3

Select Case iWeek
Case 1
msgbox “Today is Sunday”
Case 2
msgbox “Today is Monday”
Case 3
msgbox “Today is Teusday”
Case 4
msgbox “Today is Wednesday”
Case 5
msgbox “Today is Thursday”
Case 6
msgbox “Today is Friday”
Case 7
msgbox “Today is Saturday”
Case Else
msgbox “Invalid Number”
End Select

Looping Statements

A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in VBScript.

VBScript provides the following types of loops to handle looping requirements.

  • For Loop
  • For Each Loop
  • While Wend Loop
  • do while loop
  • do until loop

Loop Control Statements – VBScript supports the Exit control statements. like Exit For, Exit Do Statements.

For Loop – A For loop is used for special situations when you need to do something over and over again until some condition statement fails.

Syntax

For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next

Example –

Dim indexA, indexB, highB
highB = 5
For indexA = 1 to 3
For indexB = highB to 1 Step -1
msgbox indexB & ” ”
Next
Next

For Each Loop – For Each Loop is useful when you want to go through every element in an array, but you do not know how many elements there are inside the array.

Syntax

For Each element In group
[statements]
[Exit For]
[statements]
Next

Example –

Dim fso, f, f1, fc, s
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & “<BR>”
Next

msgbox s

While Wend Loop – While Wend Loop is a simple loop that keeps looping while condition is true.

Syntax

While condition
Version [statements]
Wend

Example –

Dim Counter
Counter = 0   ‘ Initialize variable.
While Counter < 10   ‘ Test value of Counter.
Counter = Counter + 1   ‘ Increment Counter.
msgbox Counter
Wend   ‘ End While loop when Counter > 9.

Do While Loop – The do While loop is used when we want to repeat a set of statements as long as the condition is true.

Syntax

Do [While condition]
[statements]
[Exit Do]
[statements]
Loop

Example –

index = 0
Do While index <= 10
document.write(index & ” “)
index = index + 1
Loop

Do Until Loop – A Do Until loop is used when we want to repeat a set of statements as long as the condition is false.

Syntax

Do [Until condition]
[statements]
[Exit Do]
[statements]
Loop

Example –

Dim index
index = 0
Do Until index > 10
document.write(index & ” “)
index = index + 1
Loop

Leave a Reply

Your email address will not be published. Required fields are marked *