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

  1. Conditional Statements
  2. Loop Statements

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:
Example:
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:
Example:
It is to execute more than one statement when a condition is true.

IF – ELSEIF STATEMENT: (More than two conditional)

SYNTAX:
Example:

SELECT CASE STATEMENT:

It’s an alternative to IF-THEN-ELSE. It makes code more efficient and readable.

SYNTAX:
EXAMPLE:
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

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar

Leave a Comment

API Testing eBook

DOWNLOAD FOR FREE