You can use Do...
Repeating Statements While a Condition is True
Use the While keyword to check a condition in a Do...myNum
is set to 9 instead of 20, the statements inside the loop will never run. In the ChkLastWhile procedure, the statements inside the loop run only once because the condition is already False.
Sub ChkFirstWhile()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
MsgBox "The loop made " & counter & " repetitions."
End Sub
Repeating a Statement Until a Condition Becomes True
There are two ways to use the Until keyword to check a condition in a Do...
Sub ChkFirstUntil()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
Sub ChkLastUntil()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox "The loop made " & counter & " repetitions."
End Sub
Exiting a Do...Loop Statement from Inside the Loop
You can exit a Do...
In the following example, myNum
is assigned a value that creates an endless loop. The If...Then...Else statement checks for this condition, preventing the endless repetition.
Sub ExitExample()
Dim counter, myNum
counter = 0
myNum = 9
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
If myNum < 10 Then Exit Do
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
3 comments:
First of all. Thanks very much for your useful post.
I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for software testing community.
There is a good Software Testing resource site, Have alook. http://qualitypointtech.net/ebook/index.php
Hi,
I'm wondering if you can help out with this... I have a DO While loop in my QTP code, and every time it loops and doesn't meet the exit criteria, it reports a failure to QTP results. This hasn't happened to me in the past, but for some reason just started. I'm wondering why this is happening? Here is the code, pretty basic:
Do Until OracleFormWindow("Requests").OracleTextField("Phase").Verify("Completed")
Wait(5)
OracleFormWindow("Requests").OracleButton("Refresh Data").Click
Loop
So every time it enters the loop and Phase does not equal Completed, it reports a failure to the QTP test results. Weird. Is there a setting in QTp or the Action that is doing this?
I also posted in the QTP forums but haven't heard anything yet.
Thanks for any help you can offer,
Mitch
Oops, Do Until, sorry. But you get the picture.. :)
Post a Comment