473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop ?

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
8 1664
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
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
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
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
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
<<
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
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


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2910
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
2
2657
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
3503
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
2553
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
2
19286
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.