Looping Statements VBScript | Automation Testing QTP/UFT

Looping Statements VBScript:

Control statements are subdivided in to two types.

  1. Conditional Control Statements
  2. Looping Control Statements

Here we are going to see Looping Statements VBScript. In the previous post we talked about Conditional Statements.

LOOPING STATEMENTS VBScript:

Looping control statements allow to run 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

SIMPLE WHILE STATEMENT: (Pre-conditional looping statement)

It executes only when a condition is True.

SYNTAX:

Example:

DO-WHILE STATEMENT: (Post-conditional looping statement)

It gets execute at least one time even if the condition is False because the condition in the while statement gets checked at the end of the first iteration.

SYNTAX:

Example:

DO-WHILE STATEMENT: (PRE CONDITIONAL DO-WHILE)

It gets executed the set of statements only when the condition in the while statement holds true.

SYNTAX:

Example:

change the value of a and try how it works.

Incase if the program is keep on running. You could end the task by navigating the following path:
Task Manager – Processes – Apps – Microsoft Windows Based Script Host

DO-UNTIL STATEMENT:

It gets executed until the condition becomes true.

SYNTAX:

Example:

How to Exit a “DO” Loop
Use the keyword EXIT to come out of the loop:
In the above example, the code executes until the value of “a” is greater than “10”. Let’s see how to exit from the loop when the value of a reaches “5”

FOR LOOP:

There are two types of for loops: 1. Incremental and 2. Decrement

INCREMENTAL:
SYNTAX:

[step 1] is not mandatory for one step increment

DECREMENTAL:
SYNTAX:

[step -1] is mandatory here

Example:

Example:

Example:

How to Exit a “FOR” Loop

Use the keyword EXIT to come out of the loop:

Syntax:

Example:

FOR EACH STATEMENT:

repeats a block of code for each element of an array.

SYNTAX:

Example:

VBScript Series:

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

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment

Share via
Copy link
Powered by Social Snap