VBScript provides two main types of conditional statements for controlling program flow: the If...Then statement and Select Case statement. The If...Then statement allows executing different blocks of code based on conditional checks, and can include ElseIf and Else clauses. The Select Case statement chooses between multiple blocks of code based on the value of an expression. VBScript also supports looping structures like For...Next, Do...Loop, and While...Wend to repeat blocks of code. These conditional and looping statements allow inserting verification points and error handling in scripts.
Overview of decision-making in VBScript. Key terms: If...Then, Select Case. Examples provided for conditional checks.Demonstrates various selection statements, including nested if and Select Case. Key examples of conditions with outputs.Introduction to loops in VBScript. Key terms: For...Next, Do...Loop. Examples show how loops can repeat code and include exit conditions.
Explains advanced looping mechanisms like Do Until, For Each...Next, and While...Wend. Includes practical examples of their usage in VBScript.
Control Flow FunctionsIn VbScript
Conditional statements allows us to make decisions and to control the flow of execution of a
script or one of its sections and accordingly repeat actions.
Usage of VBScript Conditional statements in QTP:
I) We use conditional statements to insert verification points.
II) We can user conditional checks for error handling purpose also.
VBScript mainly provides following 2 types of decision making statements :
I) If ...Then Statement.
II) Select Case Statement.
Simple If ( Single line without block) statement is Condition is True:
Dim myDate
myDate=#10/10/2014#
if myDate < date then myDate =date+2 ' where current date is 10/13/2014
msgbox myDate
'O/P - 10/15/2014
Execute block of statements using Simple If
Dim myDate
myDate=#10/10/2014#
if myDate < date then
myDate =date+2 ' where current date is 10/13/2014
msgbox myDate
msgbox "Mindfire solutions"
End If
'O/P - 10/15/2014
Mindfire solutions
IF ...Then...Else
Dim a,b
a=5
b=20
2.
If a >b Then
Msgbox "a is Greater"
Else
msgbox "b is Greater"
End If
If...Then...ElseIf..Else Statement
Dim a
a = -5
If a > 0 Then
Msgbox "a is a POSITIVE Number"
ElseIf a < 0 Then
Msgbox "a is a NEGATIVE Number"
Else
Msgbox "a is EQUAL than ZERO"
End If
nested if statements
Dim a
a = 23
If a > 0 Then
Msgbox "The Number is a POSITIVE Number"
If a = 1 Then
Msgbox "The Number is Neither Prime NOR Composite"
Elseif a = 2 Then
Msgbox "The Number is the Only Even Prime Number"
Elseif a = 3 Then
Msgbox "The Number is the Least Odd Prime Number"
Else
Msgbox "The Number is NOT 0,1,2 or 3"
End If
ElseIf a < 0 Then
Msgbox "The Number is a NEGATIVE Number"
Else
Msgbox "The Number is ZERO"
End If
'O/P - The Number is a POSITIVE Number
The Number is NOT 0,1,2 or 3
Select Case statement
Executes one of several groups of statements, depending on the value of an expression.
3.
Dim num1, num2,operation
num1=5
num2=10
operation= Lcase(InputBox ("Enter an operation"))
Select Case operation
Case "add"
msgbox "res:" &(num1+num2)
Case "sub"
msgbox "res:" &(num1-num2)
Case "div"
msgbox "res:" &(num2/num1)
Case Else
msgbox "res:" &(num1*num2)
End Select
Using Control Structures to Make Code Repeat
For…Next
The first structure is often referred to as the For…Next loop. The syntax for this structure is
For counter = start to finish [Step step]
...code that gets repeated
Next
Example:
' increment loop by 2
Counter = 1
Result = 0
For Counter = 1 to 5 step 2
Result=Result + Counter
msgbox Result
Next
4.
'Example with ExitFor
For Counter=1 to 5
If Counter=3 Then
Exit for
End If
msgbox Counter
Next
Do…Loop
The first loop structure you can use is the Do While…
Loop structure. The basic syntax for this structure is
Do While <Condition>
….Code with in the loop for repetitive activity
Loop
Example:
Counter = 1
Do While Counter < 4
Total =Inputbox("Please enter the total marks in numbers")
If Total < 30 Then
MsgBox "Fail"
ElseIf Total >=30 and Total <=100 then
Msgbox "Pass"
Else
Msgbox "Invalid Marks"
End If
5.
Counter = Counter+ 1
Loop
Exit Do While loop:
i = 0
Do While i <= 100
If i > 10 Then
Exit Do ' Loop Exits if i>10
End If
msgbox("The Value of i is : " &i)
i = i + 2
Loop
There is an another piece of DO ..Loop. Below is the syntax:
Do
….Code with in the loop for repetitive activity
Loop Until condition
Example:
Counter = 1
Do
Total =Inputbox("Please enter the total marks in numbers")
If Total < 30 Then
MsgBox "Fail"
ElseIf Total >=30 and Total <=100 then
Msgbox "Pass"
Else
Msgbox "Invalid Marks"
End If
Counter = Counter + 1
6.
Loop until Counter> 4
Exit Do Until loop:
i = 0
Do
i = i + 2
If i > 10 Then
Exit Do ' Loop Exits if i>10
End If
msgbox("The Value of i is : " &i)
Loop Until i > 100
For Each...Next statement - runs code for each item in a collection or each element of an array
Example:
Dim birds(2)
birds(0)="parrot"
birds(1)="Crow"
birds(2)="Sparrow"
For Each x In birds
msgbox(x)
Next
While...Wend: Executes a series of statements as long as a given condition is True.
Example:
Dim Counter
Counter = 0 ' Initialize variable.
While Counter < 20 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
msgbox Counter
Wend ' End While loop when Counter > 19.