Micro Focus QTP (UFT) Forums
Infinite FOR loop issue - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Infinite FOR loop issue (/Thread-Infinite-FOR-loop-issue)



Infinite FOR loop issue - Sivapratha - 03-28-2013

Hi all,

How to set dynamic end value in 'For...Next' loop?

I am using the following script, but it ends once 'i' reaches '5'

Code:
Dim i,j
j = 5
For i=1 To j
Msgbox ("I value = "&i&vbcrlf&"J value = "&j)
j=j+1
next



RE: Infinite FOR loop issue - basanth27 - 03-28-2013

if value reaches desired one, set Exit for.
Code:
For i=1 To j
Msgbox ("I value = "&i&vbcrlf&"J value = "&j)
j=j+1
if i=5 then
Exit for
End If
next



RE: Infinite FOR loop issue - Sivapratha - 03-28-2013

Hi...

I DO NOT want to Exit the For Loop when "i" reaches "5".
My query is to create infinite loop...
Even without the "Exit For" statement, my code exits... This is my concern,
please clarify.


RE: Infinite FOR loop issue - basanth27 - 04-01-2013

As far as i have learned,
For Loop in Vbscript is a conditional statement which iterates based on the condition specified. Unlike C & Java the variable in the iteration is not updated during execution.

The loop is not exiting because i value has reached 5 but because you have specified only 5 iterations.
Code:
j=5
for i = 1 to j
j=j+1
Next
In this case if you give j=10, it will exit after 10 iterations. Now, the increment done on j is not being updated to the variable on the conditional statement.

You could do this in Java or C without specifying the end , for eg :
Code:
for(i=0;;i++)
this will keep executing till it reaches the maximum value for the defined datatype. There is no such thing called Infinity.

As far as i know, in vbscript, you could use the while loop to create an unconditional iteration.

For eg:
Code:
i=2
Do
j=j+1
msgbox j
Loop Until i=1

Probably, if the For Loop in vbscript could be pushed to be so, i would really like to learn about it.