By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

Loop ?

P: n/a
I have the following loop the length contains somewhere around 1000+ items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is now
even though I have issues the elem.click event it processes all 1000+ items.
Once I find what I am looking for and act on it, I want the loop to be done.
Just not sure how to accomplish that.

S
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
Exit For
End If
End If
Next elemIndex

"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
I have the following loop the length contains somewhere around 1000+ items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is
now even though I have issues the elem.click event it processes all 1000+
items. Once I find what I am looking for and act on it, I want the loop to
be done. Just not sure how to accomplish that.

S

Nov 21 '05 #2

P: n/a
When you know you don't want to continue looping add "Exit For".

By the way, is "len" the name of one of your variables? "Len" is a VB 6.0
function that still is accessible in VB.NET and so you should probably not
use it as a variable name.
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
I have the following loop the length contains somewhere around 1000+ items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is
now even though I have issues the elem.click event it processes all 1000+
items. Once I find what I am looking for and act on it, I want the loop to
be done. Just not sure how to accomplish that.

S

Nov 21 '05 #3

P: n/a
Personaly (I know you didn't ask but I love to nose in :) ),

I don't like to exit for loops aheadof time. For loops are for (no pun
intended) when you want to loop a known number of times, period.

I would work on a way using a while loop, flag and a counter...

Warning.. not tested... off the top of my head code...

Do
If (elemCounter < (len - 1)) Then
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
foundIt = True

End If

End If
elemIndex += 1

End If
Loop While (foundIt = False)
"Scott M." <s-***@nospam.nospam> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
When you know you don't want to continue looping add "Exit For".

By the way, is "len" the name of one of your variables? "Len" is a VB 6.0
function that still is accessible in VB.NET and so you should probably not
use it as a variable name.
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
I have the following loop the length contains somewhere around 1000+
items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is
now even though I have issues the elem.click event it processes all 1000+
items. Once I find what I am looking for and act on it, I want the loop
to be done. Just not sure how to accomplish that.

S


Nov 21 '05 #4

P: n/a
Ray,

I don't agree with you completly. However when it becomes nested than you
are right in my opinion.

Although than I use the "Do Until" which describes better direct at the
start what you are doing without first searching for the end of the routine.

Just my thought,

Cor
Nov 21 '05 #5

P: n/a
I disagree with you on this for several reasons.

1. You way takes more lines of code, therefore more places to make mistakes.

2. For loops are to count a number of known iterations, which is what he
has. The for loop has a set exit time. If it didn't, then it would just be
like a while loop. You will notice in your untested code, you would loop
endlessly if the item is never found. Yes, you could add another check to
make sure it doesn't, but there again, your making the code more complex for
no benefit.

3. You give no reason why it is bad to exit the loop ahead of time.

Chris

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:u6****************@TK2MSFTNGP10.phx.gbl...
Personaly (I know you didn't ask but I love to nose in :) ),

I don't like to exit for loops aheadof time. For loops are for (no pun
intended) when you want to loop a known number of times, period.

I would work on a way using a while loop, flag and a counter...

Warning.. not tested... off the top of my head code...

Do
If (elemCounter < (len - 1)) Then
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
foundIt = True

End If

End If
elemIndex += 1

End If
Loop While (foundIt = False)
"Scott M." <s-***@nospam.nospam> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
When you know you don't want to continue looping add "Exit For".

By the way, is "len" the name of one of your variables? "Len" is a VB
6.0 function that still is accessible in VB.NET and so you should
probably not use it as a variable name.
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
I have the following loop the length contains somewhere around 1000+
items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is
now even though I have issues the elem.click event it processes all
1000+ items. Once I find what I am looking for and act on it, I want the
loop to be done. Just not sure how to accomplish that.

S



Nov 21 '05 #6

P: n/a
<<
For loops are for (no pun
intended) when you want to loop a known number of times, period.

A known number of times, or a known number of
elements?

Roger

<<
For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)

Nov 21 '05 #7

P: n/a
Thanks,

I agree that using the do while would best suite my situation.

Steve

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:u6****************@TK2MSFTNGP10.phx.gbl...
Personaly (I know you didn't ask but I love to nose in :) ),

I don't like to exit for loops aheadof time. For loops are for (no pun
intended) when you want to loop a known number of times, period.

I would work on a way using a while loop, flag and a counter...

Warning.. not tested... off the top of my head code...

Do
If (elemCounter < (len - 1)) Then
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
foundIt = True

End If

End If
elemIndex += 1

End If
Loop While (foundIt = False)
"Scott M." <s-***@nospam.nospam> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
When you know you don't want to continue looping add "Exit For".

By the way, is "len" the name of one of your variables? "Len" is a VB
6.0 function that still is accessible in VB.NET and so you should
probably not use it as a variable name.
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
I have the following loop the length contains somewhere around 1000+
items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is
now even though I have issues the elem.click event it processes all
1000+ items. Once I find what I am looking for and act on it, I want the
loop to be done. Just not sure how to accomplish that.

S



Nov 21 '05 #8

P: n/a


"Chris" <No****@NoSpam.com> wrote in message
news:Op****************@TK2MSFTNGP10.phx.gbl...
I disagree with you on this for several reasons.

1. You way takes more lines of code, therefore more places to make
mistakes.

We are all entitled to our opinions, but I am from the school that says,
even if it is more lines, if the intent is clearer the that means it is
easier to maintain and fix in the future.
2. For loops are to count a number of known iterations, which is what he
has. The for loop has a set exit time. If it didn't, then it would just
be like a while loop. You will notice in your untested code, you would
loop endlessly if the item is never found. Yes, you could add another
check to make sure it doesn't, but there again, your making the code more
complex for no benefit.

This was the exact reason for my warning... "Warning.. not tested... off the
top of my head code..." It was off the cuff, done in the news editor, not
the code editor, not compiled or tested, and it was 12:21am...

Yes, you have a defined number of things to loop through, but the intent of
the code is not to always run that number of times. The intent is to look
through as many as needed and stop looking when the one you look for is
found. The for loop is analogous to looking through a deck of 52 cards
looking for the ace of spades and even if you find it on the third card you
keep looking. Yes, you can exit the for loop early, but to me that induces
the chance that if the code needs to be changed in the future errors might
creep in.

I am also a firm believer in the idiom of single exit points for functions.
I hate people that do things like this:

Public Function Foo(thingToWatch) as Boolean

'Looks for thing one and returns True when found
Select Case thingToWatch
Case ThingOne
return True

Case ThingTwo
return False

Case Default
return False

End Select

End Function

To me, exiting a for loop is the same nasty looking thing.

3. You give no reason why it is bad to exit the loop ahead of time.

Boy I tell you the web has let me down on this one. I have had several
documents over the years that have documented 'proper' styles for looping
structures and I have adopted them. This was even a very nitpicky area for
my teachers in college. Looking at the possible exit points in the code,
execution starts at the top and flows downwards. Decision statements can
branch out of a structure, loops simply repeat things. Decisions structures
branch, loops iterate and let the code flow.

Be damned if I can find a reference that is 'webable' right now...

Again, I guess it is more of a matter of taste and style.
Chris

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:u6****************@TK2MSFTNGP10.phx.gbl...
Personaly (I know you didn't ask but I love to nose in :) ),

I don't like to exit for loops aheadof time. For loops are for (no pun
intended) when you want to loop a known number of times, period.

I would work on a way using a while loop, flag and a counter...

Warning.. not tested... off the top of my head code...

Do
If (elemCounter < (len - 1)) Then
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
foundIt = True

End If

End If
elemIndex += 1

End If
Loop While (foundIt = False)
"Scott M." <s-***@nospam.nospam> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
When you know you don't want to continue looping add "Exit For".

By the way, is "len" the name of one of your variables? "Len" is a VB
6.0 function that still is accessible in VB.NET and so you should
probably not use it as a variable name.
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
I have the following loop the length contains somewhere around 1000+
items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
If elem.tagName = "A" Then
If elem.innerText = "1." Then
elem.click()
End If
End If
Next elemIndex

Once I find the item I am looking for I want to end the loop. As it is
now even though I have issues the elem.click event it processes all
1000+ items. Once I find what I am looking for and act on it, I want
the loop to be done. Just not sure how to accomplish that.

S



Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.