All about loop in UFT

The loops are used to execute a set of statements desired no. of times. Few loops are executed till the time some given conditions in those loops are satisfied or not satisfied.

There are 4 types of loop provided as mentioned below.

  • .. For Loop
  • .. For Each Loop
  • .. Do While Loop
  • .. Do Until Loop
  • ..While Wend Loop

Let’s understand how to use them.

For Loop: This loop executes the code inside it for the no. of times mentioned in it. Let’s see with an example.

1st way of using For loop:

It will print 1 2 3 4 5 6 7 8 9 10

2nd way of using For loop:

By using Step command.

3rd way of Using For loop: Run the For loop in reverse order.

For each loop: We have one loop called ‘For each’ loop.This can be used in the scenarios like fetching each value from an array or a collection of objects.

Do While Loop: This loop executes a set of statements while it finds the condition true. Loop will executes until condition is true, and it will stop once condition becomes false.This can be performed in 2 ways.

1. Condition will be evaluated before entering the loop: In the below example, condition will be checked 2 times. First while entering the loop and second after incrementing the counter.

2. Condition will be evaluated at end of the loop: It will execute the loop only once. It increments the counter and then check the condition. As ‘i’ becomes 10, conditions will be false.

Do Until Loop: This loop is similar to Do while loop, but the difference is that code will be executed until the condition becomes true and in while loop code executes until condition becomes false. In other words, it will come out of the loop once condition is true. Like ‘Do while’, it is possible to put the condition at the start and end of the loop.

1. Condition will be evaluated at end of the loop:

2. Condition will be evaluated before entering the loop:

While wend Loop: This loop executes the code until condition becomes false. In the below code, the value of ‘i’ is assigned as 9. First time, loop finds the condition true( i=9). But inside the loop ‘i’ value incremented by 1 and becomes 10. So, in the second iteration of the loop ,condition becomes false. Control will come out of the loop.

Ask Question
If you have any doubt, you can ask question by going to ‘Features –>Q&A forum –> Ask Question’.




Error handling in UFT - Part 2
Working with Excel
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *