Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: for i

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default for i

    A little ways back we got into discussing what the most efficient way to iterate using 'for i' was, and it appeared to me that we settled on something like so:

    Code:
    for (var i = some.length-1; i > -1; --i)
    or:

    Code:
    for (var i = some.length-1; i >= 0; --i)
    I just realized that this should mean the same thing:

    Code:
    for (var i = some.length-1; i+1; --i)
    and it worked!
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    for (var i = some.length; i; --i)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    What's wrong with for (var i=0; i < some.length; i++)?

    Oh, and for (var i = some.length-1; i+1; --i) will skip the last value.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    How much savings are we really talking here though, at the expense of legibility IMO? As long as you remember to cache any objects in the conditional part of the loop, I think that should suffice in most cases right?

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Well, I've wished for a long time that there was a better loop. "For" is misleading, and, I'd say, only useful in limited circumstances. It's nice to have a default variable for counting repetitions, but when this isn't needed, the bulk of the statement seems to be excessive. I also like for loops for some other situations, that might not be readily apparent. Replacing many while loops with for loops can be nice, such as (though this is PHP) for ($i=0; $row=mysql_fetch_array($result);$i++) { echo 'Entry '.$i.': '.$row[0]; }, and other situations.
    However, there should just be a basic loop-- repeat(5) { }, which is what I'd think "For" would mean... for(5) {...}
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Python has no for loop like this, just a for/in loop.
    Oh, and for (var i = some.length-1; i+1; --i) will skip the last value.
    No it won't. It's less efficient than a normal comparison though, since i + 1 here is more-or-less equivalent to i + 1 != 0, an addition and comparison rather than just a comparison.
    for (var i = some.length; i; --i)
    for(var i = some.length; i--; ) if you want to abuse the for structure Due to the wacky values of i during execution, though, most of these are usually impractical.
    How much savings are we really talking here though
    Not much really.
    at the expense of legibility IMO
    The for loop as a whole is very difficult to read. The only reason we can understand "normal" for loops so easily is because we've come to recognise the idiom, and the reason these loops are hard to read is because they don't fit it so we have to actually read them.
    there should just be a basic loop-- repeat(5) { }
    You can implement it as a function:
    Code:
    function do_times(numTimes, func) {
      while(numTimes--)
        func();
    }
    Bit of extra function cruft:
    Code:
    do_times(5, function() {
      do_something();
    });
    Last edited by Twey; 12-01-2007 at 12:57 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    And somehow making a function, making an inner function and calling that is easier than just a for loop? I didn't say they were hard.... just that it would be nice to have a basic looping method based on numerical values, not just conditions. repeat(5), rather than any conditions, counting, etc.

    I think you meant do_times(5, 'function') {. (in addition to not closing the parentheses, you used a function call rather than the name as the string to later be executed in the do_ function. Or, maybe not. I think I missed something.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I didn't say they were hard.... just that it would be nice to have a basic looping method based on numerical values, not just conditions. repeat(5), rather than any conditions, counting, etc.
    Which is exactly what I just wrote
    I think you meant do_times(5, 'function') {. (in addition to not closing the parentheses, you used a function call rather than the name as the string to later be executed in the do_ function. Or, maybe not. I think I missed something.
    No I didn't, and yes you did, although I'm not entirely sure what. Executing strings is bad form.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The efficiency of using the prefixed decremented loop instead of the appended incremented loop is only significant when there are a large number of items to loop through and/or a large number of loopings.

    However, when writing scripts, it is often unknown how many items there will be, and sometimes the number of executions of looping constructs is also unknown.

    Using, as put forth by someone else in this thread:

    Code:
    for (var i = some.length; i; --i)
    For the usual sort of array looping done (checking each item in the array), with that the variable i will never be able to equal 0, which will leave off the first element in 'some', and check a non-existent element:

    some[some.length]
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The efficiency of using the prefixed decremented loop instead of the appended incremented loop is only significant when there are a large number of items to loop through and/or a large number of loopings.
    E.G. around 10,000 on modern CPUs
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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