QTP Tutorial #10 – Writing Loop and Conditional Statements – VB Script Basics Part 2

VB Scripting Basics – Writing Loop and Conditional Statements for Building the Programming Logic.

=> Click Here For The QTP Training Tutorials Series

=> Also read the complete free VBScripting Tutorial series here

In the last VB Scripting article, we saw some of the basic features of the VB script. We are going to learn a few more programming concepts before we conclude our introduction series.

Writing Loop and Conditional Statements

Conditional Statements

#1) If Statement

If (condition)..Then
{Statement or a block of statement}
{Statement or a block of statement}
End if

VB Script tutorials

This is the typical syntax to write this statement.

  • The if..then..else statements can be nested to any number of levels.
  • Alternately, the else block can be used or not.
  • Elseif is another variation that can be used when choosing one of the multiple options.

if x=0 then
Msgbox “value=0”
Elseif x=1 then msgbox “value=1”
Elseif x=2 then msgbox “value=2”
Else msgbox “value not found”
End if

#2) Select Statement

This is to choose one out of the many options depending on the condition that comes satisfied. The condition is evaluated once and based on the value it attains one of the following blocks of code gets chosen to be run.

Select Case (expression)
Case “case1”
{Block 1}
Case “case 2”
{Block 2}
Case Else
{Else block}
End Select

Looping Statements

There are 4 kinds of loop statements:

#1) Do…loop
This is used when a statement or a block of statements need to be executed while or until a said condition is true. Let us first  look at the syntax of this:

Syntax 1:
Do (While | Until) condition
{Statement or statements}
[Exit Do]
{Statement or statements}

Syntax 2:
{statement or statements]
[Exit Do]
{statement or statements]
Loop [{While | Until} condition]

Condition: could be a numeric or string component that either attains a true or false value. If the condition is null it is treated as False.
Observe the ‘Exit do’ in the above.

There is also a slight difference between syntax 1 and syntax 2.

In the case of syntax 1, the statements within the do loop do not get executed unless the condition becomes true or holds true.

For syntax 2, the statements within the loop get executed at least once and then the check is performed on the condition.

Exit Do: In order to avoid infinite loops we will have to force the loop to exit. Exit Do is a statement that is used in such circumstances.

#2) While…wend

{Statement or block of statement}

It is self-explanatory from the syntax that the statements nestled under the while block get executed as long as the condition holds true.

Although this statement is available, it is not very flexible so it is recommended to use the Do…Loop statement.

#3) For…Next
This is the statement that you would use when you want a statement/set of statements to run a certain number of times while a certain counter gets incremented or decremented.

For counter = start To end [Step step]
[Exit For]

  • As you can see from the syntax above, there is a ‘Step’ clause to this statement. This clause is optional.
  • The step clause if not specified, the for loop steps one counter forward by default
  • The step can be used to increment or decrement the counter value
  • Exit For is similar to “Exit Do”, it can be used to come out of the For block and execute the statement that follows.
  • Any number of Exit For statements can be used within one block of For statement
  • It is usually used with an if..then statement to make sure some condition that would lead to infinitely looping is true and in case if it does, the For statement has a way to exit.
  • Any number of For statements can be nested within a For.


For i=1 to 10
If x=true then
Exit for
End if

Example of a positive step:
For i = 2 To 12 Step 2
total = total + k

Example of a negative step:
For i = 12 To 2 Step -2
total = total + k

#4) For each…next

This is similar to ‘For …next’. This is used for collection objects or arrays. This statement runs a statement or a set of statements for every object or item in an array instead of the number of times specified. As the ‘For…next’ statement Exit is used to exit before the looping is through, like in case of an error. Also, any number of For Each statement can be nested within each other.

For Each element In group
[Exit For]
Next [element]

  • The element is the variable that is used to iterate through the elements in the array or collection object
  • the group stands for the name of the collection object or array

Note:  We have not discussed collection objects so far in our series, but a collection object is nothing but an object which is a set of related items (objects, these might be of the same type or could be of different types)

Best Practices For Code Writing In VB Script

  • At the beginning of every program, write down a brief description of what the program does
  • Provide comments for every variable used during its declaration to briefly describe what this variable is going to do
  • Keep the code modular, as much as possible
  • Have the main program readable and have all the logic segregated in terms of function, so it makes them easy to read and maintain.
  • The segregation of code into functions will also increase reusability
  • An agreed naming convention has to be used to maintain consistency
  • Comments – Provide comments to make the code more understandable
  • Indentation – Make sure that you indent the lines of code to clearly understand the sequence of execution
  • Option Explicit is to be declared so that you don’t run into an issue when you have a spelling mistake in a variable name
  • Watch out for infinite loops


This concludes our brief introduction to VB Script. As already mentioned, this is in no way a complete guide to learning the scripting language, but enough to get us through writing beginner to moderate level QTP programs.

There is one topic of functions that we did not cover here but that exclusion was deliberate. It is because functions are a full length and a very important topic which we will discuss in detail in the coming articles.

The next article in our QTP training series is going to be about the Expert view and we will try to include some checkpoints too. Please feel free to post your questions.

=> Visit Here For The QTP Training Tutorials Series

=> Also read the complete free VBScripting Tutorial series here

Recommended Reading

13 thoughts on “QTP Tutorial #10 – Writing Loop and Conditional Statements – VB Script Basics Part 2”

  1. Hi, I’m getting emails from readers that they are not able to post comments on this article (admin comments are working). I’m working on resolving this.


  2. The comment and CSS loading issue has been fixed now. Unfortunately we lost all social shares. But you should not face any issue for loading page and posting the comments. Thanks.

  3. @Senthil: QTP does provide a list of functions/operations available for a certain object when you are trying to insert or edit a step. But the decision of what to record is upto the tester

  4. herr you made one point –> Option Explicit is to be declared so that you don’t run into issue when you have a spelling mistake of a variable name

    i am unable to understand this point.. how the option Explicit will help u with spelling mitake issue

  5. i am trying to automate a functionality which has 5 similar forms, but the links to these forms are images.i want to use datatable and write a for loop, which can click on image true value or text on the image and validate these forms one by one, these forms have text fields and dropdown menu’s and submitt button,pretty much easy forms, but i am unable to get the logic for the for loop in vb script, can anyone please help. Thanks in advance .

  6. Hi…vijay i want to know if condition and for,while ,do-while statements with simple & understandable examples

  7. Hello everyone
    I am trying to slow down vbscript loop so that i can execute output of each loop. Anybody can help me out please.

    here is what I am trying

    for i=blxArray(blobIndex) to brxArray(blobIndex)
    SetVariable “BMX”, z
    SetTimedVariable “Trigger”, 1720, triggerperiod

  8. Write a script for infinite loop? if infinite times msgbox window came then how to solve? Because it click infinite times.

  9. If we are enter any year in the calendar year .
    If every month has 14 th day of Wednesday .
    In one year how many months in mentioned day.

    how write the program in QTP.

    can anyone please help


Leave a Comment