Control Statements VBScript | Automation Testing QTP/UFT
What are Control Statements in VBScript?
A control statement is a statement that decides whether to execute another statement or decides which of the two statements to execute.
There are two types of Control Statements in VBScript
What is a Statement?
Each and every line in VBScript
Let’s see both Conditional control statements and Looping control statements below
What are Conditional Control Statements in VBScript?
CONDITIONAL CONTROL STATEMENTS VBScript:
Used to execute single statements or set of statements based on conditions.
The following Conditional statements are available in VB Script:
IF STATEMENT: executes a set of statement when a condition is true
IF – ELSE STATEMENT: select one of two sets of statements to execute
IF – ELSEIF STATEMENT: select one of many sets of statements to execute
SELECT CASE STATEMENT: select one of many sets of statements to execute
Let’s see the syntax of each condition with an example below:
IF STATEMENT: (One conditional statement)
To execute only one statement when a condition is true:
SYNTAX:
IF THEN set of statements END IF
Example:
Dim a,b a=8 : b=6 if a>b then msgbox “a is greater than b” End if
There is no “Else” in the above example. It performs only one action when a condition is true
IF – ELSE STATEMENT: (Two conditional statement)
SYNTAX:
IF THEN set of statements ELSE set of statements END IF
Example:
Dim a,b a=3 : b=6 if a>b then msgbox “a is greater than b” Else Msgbox “Stay tuned to Software Testing Material” End if
It is to execute more than one statement when a condition is true.
IF – ELSEIF STATEMENT: (More than two conditional)
SYNTAX:
IF THEN set of statements ELSEIF Then set of statements ELSE set of statements END IF
Example:
Dim a,b a=8 : b=6 if a>b then msgbox “a is greater than b” Elseif a=b then Msgbox “a is equals to b” Else Msgbox “a is not equals to b” End if
SELECT CASE STATEMENT:
It’s an alternative to IF-THEN-ELSE. It makes code more efficient and readable.
SYNTAX:
SELECT CASE EXPRESSION CASE EXPRESSION1 set of statements CASE EXPRESSION2 set of statements CASE ELSE set of statements END SELECT
EXAMPLE:
InputValue = Inputbox(“Enter the value: red or green or yellow”) Select case lcase(InputValue) Case “red” Msgbox “stop” Case “green” Msgbox “go” Case “yellow” Msgbox “wait” Case else Msgbox “Invalid” End Select
NOTE:
In select case, the subdata type of “main expression” and “sub expression” should be same “same data type”
Data will always compare with equal relationship operator
Here the expressions are case sensitive. Eg: “Red” is not equal to “red”
In the above example, we used string function (lcase) to convert the input value to lowercase
What are Looping Control Statements in VBScript?
LOOPING CONTROL STATEMENTS VBScript:
Looping control statements allow running a group of statements repeatedly when the condition is true.
Following looping control statements are available in VBScript:
• While…Wend statement – It executes only when a condition is true. Use the Do-Loop statement instead of this.
• Do – Loop While statement – loops while a condition is true
• Do While – Loop statement – It executes one time even if the condition is False
• Do – Loop Until statement – loops until a condition is true
• For…Next statement – runs code a specified number of times
• For Each…Next statement – runs code for each item in a collection or each element of an array
Click here to see looping control statements in detail
Related posts:
- Arrays VBScript | Automation Testing QTP/UFT
- VBScript for Automation (QTP/UFT) Testing – Part 1
- VBScript for Automation (QTP/UFT) Testing – Part 2
- VBScript for Automation (QTP/UFT) Testing – Part 3
- VBScript for Automation (QTP/UFT) Testing – Part 4
- VBScript for Automation (QTP/UFT) Testing – Part 5