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:
Dim i 'Loop through the numbers For i=1 to 10 Print i Next
2nd way of using For loop:
By using Step command.
Dim i 'Loop through the numbers For i = 1 To 10 Step 2 Print i Next
3rd way of Using For loop: Run the For loop in reverse order.
Dim i 'Loop through the numbers For i = 10 to 1 step -1 Print i Next
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.
Dim arr(3) arr(0) = "Name" arr(1) = "City" arr(2) = "Country" 'Loop through the numbers For each val in arr Print val Next
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.
Dim i ' i is the counter 'First it will check the condition if i is 10, it enters the loop and increment 'the counter , then again it will check condition is true or false Do While i<10 i=9 i = i+1 print i Loop
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.
Dim i Do i=9 i = i+1 print i Loop While i<10
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:
Dim i i=9 ' It will run the code 2 time and print 10, 11 as output Do i = i+1 print i Loop Until i>10
2. Condition will be evaluated before entering the loop:
Dim i i=9 ' condition will be evaluated at start of the loop Do Until i>10 i = i+1 print i 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.
Dim i i=9 While i=9 ' condition should be false to exit the loop print i i=i+1 ' 10 Wend