-
Nov 9th, 2015, 08:07 AM
#1
Thread Starter
Junior Member
for vs for each
Is there really a difference between these two? So, for
x(1, 2, 0 ,-1)
are
Code:
for each k in x
k = k + 1
print k
next
and
Code:
for i = 1 to 4
x(i) = x(i) + 1
print x(i)
next
the same?
-
Nov 9th, 2015, 08:24 AM
#2
Re: for vs for each
There may be a difference... I can't remember off the top of my head if they would be treated as value variables, in which case, no, the two are not the same, or as reference, in which case they would be treated the same. I want to say the values get copied from x to k, so once it prints k, it's value is lost and the original array values remain intact. On the other hand since the second loop does a direct assignment on the array itself, the values there are affected. Best way to find out would be to experiment some and print out or review the contents of the array, after each loop.
-tg
-
Nov 9th, 2015, 11:50 AM
#3
Re: for vs for each
Another thing there is that the second loop would fail as the array in question would be 0-3 rather than 1 to 4
-
Nov 9th, 2015, 12:39 PM
#4
Re: for vs for each
Well one obvious difference is that in the For Each......Next loop you don't need to know the number of elements while in the For....Next loop you do
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 9th, 2015, 12:45 PM
#5
Re: for vs for each
Originally Posted by jmsrickland
Well one obvious difference is that in the For Each......Next loop you don't need to know the number of elements while in the For....Next loop you do
You do not actually need to know the number of elements nor even the starting point as you can use LBound() and UBound() for that when working with arrays.
-
Nov 9th, 2015, 12:57 PM
#6
Re: for vs for each
the bounds need to be known... whether you know them at design or run time is largely academic... sort of it is that for a For loop to work, you need the lower (starting) value and the upper (ending) value of the loop range.
-tg
-
Nov 9th, 2015, 01:05 PM
#7
Re: for vs for each
One restriction with using For/Each with arrays is that the array cannot contain UDTs and the variable being used in the loop must be Variant, not Object nor any other data type
-
Nov 9th, 2015, 02:14 PM
#8
Hyperactive Member
Re: for vs for each
Originally Posted by jmsrickland
Well one obvious difference is that in the For Each......Next loop you don't need to know the number of elements while in the For....Next loop you do
... best response, in my opinion. We should take x(1, 2, 0 ,-1) as a mathematical set that hypothetically could be infinite
-
Nov 9th, 2015, 03:29 PM
#9
Re: for vs for each
while the OP example only shows an array, the same question could apply to collections and objects containing collections of objects, where sometimes it is required to loop the collection in reverse order, afaik, not possible using for each
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 9th, 2015, 07:09 PM
#10
Re: for vs for each
In the OP both chunks of code end with -
Next
And in a recent thread (different author) I saw another example of that.
Should it not say -
Next k
Even if only for commenting purposes, and future debugging
-
Nov 9th, 2015, 07:12 PM
#11
Re: for vs for each
Originally Posted by Bobbles
Should it not say -
Next k
Even if only for commenting purposes, and future debugging
Personal preferences. Makes readability better if having loops within loops. But for a single loop, I never do it and generally never do unless I have a very complex multi-loop structure, simply because clear indenting negates an ambiguity.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|