Micro Focus QTP (UFT) Forums
How to exit from a for loop - 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: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: How to exit from a for loop (/Thread-How-to-exit-from-a-for-loop)



How to exit from a for loop - silpavinod - 10-16-2012

Code:
Browser("Link Express - Booking").Page("Link Express - Booking").Link("Booking").Click
Browser("Link Express - Booking").Page("Link Express - Booking").Link("Create").Click
Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").Highlight

Set odesc=Description.Create
odesc("micclass").value="WebElement"
odesc("html tag").value="SPAN"
odesc("class").value="rtPlus"
Set val= Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").ChildObjects(odesc)
Msgbox val.count
For i=0 to val.count-50
    val(i).highlight
    val(i).click
'a=val(i).GetROProperty("innertext")
'Msgbox a

Set odesc1=Description.Create
odesc1("micclass").value="WebElement"
odesc1("html tag").value="SPAN"
odesc1("class").value="rtIn"
Set val1= Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").ChildObjects(odesc1)
Msgbox val1.count

For  j=0 to val1.count-270
a=val1( j ).GetROProperty("innertext")
Msgbox a
val1(j).Highlight
Ifa="Beer"Then
    Msgbox "Beer"
    Val1(j).click
    Exit For
    Exit For
    else
    Msgbox "Failed"
End If
Next
Next


This is my code .... here the prblm is if a= "beer" it should select and exit the loop

I am able to select but i am not able to exit frm loop

help me....


RE: How to exit from a for loop - ssvali - 10-16-2012

Put the second exit for after the first next statement

Code:
Browser("Link Express - Booking").Page("Link Express - Booking").Link("Booking").Click
Browser("Link Express - Booking").Page("Link Express - Booking").Link("Create").Click
Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").Highlight

Set odesc=Description.Create
odesc("micclass").value="WebElement"
odesc("html tag").value="SPAN"
odesc("class").value="rtPlus"
Set val= Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").ChildObjects(odesc)
Msgbox val.count
For i=0 to val.count-50
    val(i).highlight
    val(i).click
'a=val(i).GetROProperty("innertext")
'Msgbox a

Set odesc1=Description.Create
odesc1("micclass").value="WebElement"
odesc1("html tag").value="SPAN"
odesc1("class").value="rtIn"
Set val1= Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").ChildObjects(odesc1)
Msgbox val1.count

For  j=0 to val1.count-270
a=val1( j ).GetROProperty("innertext")
Msgbox a
val1(j).Highlight
Ifa="Beer"Then
    Msgbox "Beer"
    Val1(j).click
    Exit For
    
    else
    Msgbox "Failed"
End If
Next
Exit For
Next



RE: How to exit from a for loop - agarwl.anurag - 10-17-2012

Hi,

You can modify the code like this:
Code:
Browser("Link Express - Booking").Page("Link Express - Booking").Link("Booking").Click
Browser("Link Express - Booking").Page("Link Express - Booking").Link("Create").Click
Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").Highlight

Set odesc=Description.Create
odesc("micclass").value="WebElement"
odesc("html tag").value="SPAN"
odesc("class").value="rtPlus"
Set val= Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").ChildObjects(odesc)
Msgbox val.count
For i=0 to val.count-50
    val(i).highlight
    val(i).click
'a=val(i).GetROProperty("innertext")
'Msgbox a

Set odesc1=Description.Create
odesc1("micclass").value="WebElement"
odesc1("html tag").value="SPAN"
odesc1("class").value="rtIn"
Set val1= Browser("Link Express - Booking").Page("Link Express - Booking").WebElement("FMCG Food and Drink Drink").ChildObjects(odesc1)
Msgbox val1.count

For  j=0 to val1.count-270
a=val1( j ).GetROProperty("innertext")
Msgbox a
val1(j).Highlight
Ifa="Beer"Then
    Msgbox "Beer"
    Val1(j).click

       [undefined=undefined] var=1[/undefined]
    Exit For

    else
    Msgbox "Failed"
End If
Next
[undefined=undefined]If var=1 then
    Exit For
End if[/undefined]
Next

Thanks
Anurag