Control Flow in VBScript or UFT
This post talks about control flow statements in VBScript, control flow statements in UFT, control flow in VBScript, control flow in UFT. 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.
Flow control in VB script
The order of execution is known as flow in VB script. The flow gets executed top to bottom, in a sequential way and keep executing it until the end reaches.
Control of flow in VBScript or UFT
The logic that allows us to manipulate the execution of the code in our script is called control flow.
Control flow is divided into below section
1. Branching/comparison statements
This branching statement provides scripts with intelligence. Expressions/ conditions are the ones to be tested to get a True/ False outcome.VB script supports if else, endif, select branching is a technique by which depending on some condition few statements gets executed and some are not.
1.1- if end if
1.2— if else endif
1.3– select case
Looping is a technique which allows us to execute the same lines of code or block of code over and over until loop condition terminates it.
The loop is useful when
- We want to repeat a block of code until termination condition(typically a Boolean condition yields true or false.)
- We want to repeat a block of code for a predefined finite number of times.
2.1– For next[ for a predefined number of times]
2.2– Do while loop[ for a specific condition is true]
2.3– Do loop until[for a specific condition to be true]
2.4– For each next[For Each…Next Iterates through properties of an object ]
2.5—Do loop while [for a specific condition to be true]
2.6– Do until loop[for a specific condition to be true]
2.7– While -Wend
Comparison statements allow us to perform conditional logic tests between two expressions or values.
Note – It is always better to write start and end block of branching and looping before actual code block implementation.
Space or White space is used for human consumption of the code. If we do not provide space machine can still understand the code. In that case, human readability will go down.
General indentation rule is to provide white spaces if the next line is a subcode of the above code.
If( condition) then ---- do something ---- do something ---- do something end if
it will visually suggest the coding structure.
Select case— it is upgraded version of multiple if–else statements. It provides an output based on a list of possible matching expressions.
Dim userNumber Dim reply userName= Inputbox("Enter your number") reply= "the number is "& userNumber." msgbox reply
if the user does not put any value still the message box is going to display” the number is” string and userNumber is blank hence it won’t print anything.
A better approach
dim userNumber dim reply userNumber= Inputbox(" enter your number") If(userNumber="") Then reply="Please put some number" msgbox reply else reply="your number is"&userNumber msgbox reply End if
Details of If in VBScript or UFT
If — it compares two expressions or values and either perform some set of statements or skip some statements. Also If tests the expression then keyword execute the true condition execute true condition else executes the false part.
if [expression] Then Statements End if
If works on boolean value hence all the expression used in if condition must boolean output. The code resides inside if- else endif is called a block of code. Where If is the starting point of the block and Endif is the end point of if block. Block of code is often referred to as a group of statements.
Variations of if
Case 1 of If the variation
Instead of a Boolean expression, we can use mathematical expression too.
If(3-2)=1 then Statements End if
Case 2 If the variation
If a function returns a Boolean value that can be used as if.
If isString(myvar) then Statements End if
Case 3 If the variation
We can multiple tests inside of if. They are valid as long as they are yielding Boolean value.
If( isNumeric(myvar1) and (isNumeric(myvar2) Then Statements End if
Case 4 If the variation
We can use NOT statement to invert the expression.
If(NOT (isNumeric(myvar)) Then Statements End if
We can upgrade if block by attaching an else block. Else block get executed when if block is evaluated as false.
If isDigit(myvar) then Statements Else Statements End if
Else If block
The Else if block is a modified multi way switch. Else If has two variants.
- Else If with optional else
- Plain Else if without else.
Else if with optional else
If isDigit(myvar) Then Statements ElseIf isDate(myvar) Then Statements Else Statements End If
Else If without else
If isDigit(myvar) Then Statements ElseIf isDate(myvar) Then Statements End If
Nesting of If Else
we can nest if else inside another if else block. There is no rule to create nested if else as Long as the logic is correct and the code is manageable. Going too deep may raise logic issues. It may create less readability and hard maintainability.
If( accountNo.IsValid( )) If IsEmployee(accountNo) then If IsVendor(accountNo) then msgbox "bonus is 5%" else msgbox "not a vendor anymore" End If If IsRegular(accountNo ) then msgbox "bonus is 7%" Else msgbox "not a valid regular employee" End if Else msgbox "account no does not belong to a valid employee" Else msgbox "not a valid account no" End if
If isNumeric( myvar) then If( myvar less than 25) then Statements Else if((myvar less than 40) and(myvar greater than 26)) then Statements End if Else statements End if
Select case- switch
Instead of using multiple if else statement, we can use switch statements to implement multiple paths. It can test multiple conditions at a single go. Select end select block is an upgraded version of series of if end if statements.