52

After Windows/Linux and atom/sublime and vim/emacs and all those other reasons we fight...
for(i = 0 ; i < 10 ; i++)
or
for(i =0 ; i <= 9 ; i++)
for 10 iterations?

Comments
  • 3
    FreeBSD, vim, < , indentation doesn't matter as long as it's consistent (i.e. use something like astyle)

    There argument settled. Anyone who disagrees is wrong.
  • 25
    for(i = 0; i < 0xA; i++)
  • 13
    For(var i = 0; i<5; i++){
    For(var j = 0; j<2; j++) { }
    }
  • 1
    IntStream. range(0,10).forEach(i ->...);
  • 31
    i = 0;
    (Code)
    i = 1;
    (Code)
    i = 2;
    (Code)
    i = 3;
    (Code)
    i = 4;
    (Code)
    i = 5;
    (Code)
    i = 6;
    (Code)
    i = 7;
    (Code)
    i = 8;
    (Code)
    i = 9;
    (Code)
  • 13
    < always please.
    More efficient to check for one condition than two.
  • 14
    for i in range(10):
    # code
  • 1
  • 3
    void count(int i, int max){
    if(i==max){
    profit();
    return;
    }
    //Do some magic
    magic(i);
    //End magic
    count(i + 1, max);
    }
  • 1
    for i=1,10 do
    -- code
    end
  • 2
    If its hardcoded number then ofc <, if its stuff like maxIndex or smth then it may be n <= 10
  • 2
    Depends on the context, or more specific, depends on what makes more sense if you would translate it to plain English. If the requirements say up to and including x, then <= x, if the requirements say up to x, then < x.
  • 2
    for(i = 1; i <= 10; ++i){ // ... }
  • 5
    for(i = 0; i < Math.sqrt(100); i++)
  • 0
    @rcampos welcome to devRant!
  • 1
    < , one less check every loop
  • 1
  • 2
    function loop(i) {
    if (i === 0) return;
    loop(--i);
    <Code>
    }
    loop(10);
  • 2
    @zfor my eyes hurt...
  • 1
    for(var i=array.length-1; i>=0; i--) {

    // optimized for performance.
    // only checks length once

    }
  • 6
    Are you guys kidding me? <= and < will result in precisely the same test, if your compiler is only semi-decent.

    It is important to use < for readability, since you can write exactly the same expression as for the length of the array, without arithmetic. And also simply because this is the universally idiomatic way.
  • 0
    Also, what @Hel8y if it's not an array. Only beware of MAX_INT
  • 3
  • 0
    for i in 0 ... 9 {}
    or
    for i in 0 ..< 10 {}
    🍎
  • 0
    You are kidding. I don't believe anyone somehow can support the 9...
  • 1
    //LINQ:
    .FirstOrDefault();
    //Fuck second
  • 1
    @YousifMansour @magusd not true, they both translate to a single assembly op
    @elazar took my words
  • 0
    @matanl oh. I dont know much about assembly. What's the thing they're translated to and why is it the same?
  • 2
    @YousifMansour
    blt: branch if less than
    ble: branch if less than or equal
    But the compiler can generate whatever it likes, as long as the behavior is the same, so you can't know for sure what will the machine code be.
  • 3
    For(int i = 0; i != rand(); i++)
    # code
    # pray you get a 10

    Just realized my keyboard doesn't have a less than sign after the update.... Wtf
  • 0
    @elazar ok shouldn't the second one take more time? Because it might need check for both conditions? Im still a student and haven't taken the computer hardware course.
  • 2
    for (i = 0b1000000000; i != 0; i >>= 1) {}
  • 0
    @YousifMansour it's not two conditions, it only sounds this way in English. You can say that <= is "< or =" but it is equally valid to say that < is "<= and not =". It's a matter of definition, and neither is difficult for hardware.
  • 2
    @elazar @YousifMansour for example, if hardware implements a<b by executing a-b and checking whether the result is negative (MSB is one) then it will take 1 operation, but checking whether the result is non-positive (MSB is one or answer is zero) will take 2 operations
  • 2
    @jerichoi224 "pray to get a 10" 😂
  • 1
    @oscarascal 😉
  • 0
    @dzil123 i mean... on one side yes, for python, but on the other side no... just no...
Add Comment