Results 1 to 11 of 11

Thread: for vs for each

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    19

    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?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: for vs for each

    Quote Originally Posted by jmsrickland View Post
    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.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: for vs for each

    Quote Originally Posted by jmsrickland View Post
    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

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  10. #10
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    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

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: for vs for each

    Quote Originally Posted by Bobbles View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width