71
Abid
6y

Two kind of programmers

Comments
  • 11
    for(int i=1;i<=n;i++) printf(arr[i-1]);
  • 7
    @SukMikeHok
    superman = 0;
    pizza:
    do_shit(superman);
    if (++superman == motorbike) goto gayporn;
    goto pizza;
    gayporn:
  • 4
    collection.foreach(i => {
    // do stuff
    })
  • 1
    n.times do

    puts 'I like Ruby'

    end
  • 0
    Really? Anyone uses first version?
  • 0
    @Taqriaqsuk I'd prefer it to using i+1 in the loop body.
  • 3
    for(int i=n;i<0;i--)
    {
    printf("%i", (n-i));
    }
  • 0
    @ilPinguino ok, fair point. Sounds like you need foreach loop though.
  • 1
    @Taqriaqsuk Not neccessarily - let's say I need to output a table in PHP in an application designed for end users.

    I wouldn't want the row numbers to start with 0 so as not to confuse the users (let's face it, the majority of people starts to count at 1). If nothing else requires i to start at 0, I'd of course start it at 1.

    Though I always prefer using a while or foreach loop if it improves readability.
  • 2
    Int i = n
    while (--i != -1)
    printf("bitch lasagna\n");
  • 2
    @ilPinguino I still prefer idiomatic loops like each_with_index, but I get your point
  • 0
    more like two kind of use cases
  • 1
    @Gregozor2121 found a bug in your loop. ^^
  • 2
    for i in 0..<n

    Ranges ftw!
  • 2
    for (i in 0 until n)

    (Kotlin)
  • 0
  • 2
    Oh, and since noone added it...

    MOV 1, ECX

    MOV 5, EDX

    loop:

    ADD 1, ECX

    CMP ECX, EDX

    JLE loop

    From the top of my mind, might be wrong.
  • 1
    You forgot ++i
  • 1
    @Gregozor2121 should be i>0 and not i<0. A for loop runs while the condition is fulfilled, not until the condition is fulfilled.

    For negative n, this is not an issue, but then it relies on signed integer overflow for termination, which is undefined behaviour.
  • 0
    I like this better

    for(i=0;i<n;i=-~i)
  • 0
    @halfflat Hopefully I won't have to 😀
  • 0
    @Lensflare im more a "for(i in (1..10))" guy
  • 1
    I always prefix when I can 🙃
    ++i saves CPU cycles and memory
  • 0
    @D3add3d me too. Also, it's like a watermark for me because no one else in the company does that 😄
  • 0
    @Lensflare interesting 🤔
    it was like the 4th thing they taught us at uni... do your colleagues have uni degrees?
  • 0
    @D3add3d Some of them have uni degrees. I have uni degree myself but I wasn't told about the memory and performance implications of preincrement vs. postincrement there. I learned that from the internet by myself. I also do remember that the difference is miniscule at best. 😁
  • 1
    @D3add3d not for basic data types, i.e. not at all in C. In C++, only for object types / iterators.
  • 1
    And even in C++ iterators, it doesn't make a difference in the last statement of "for" because it's enclosed in sequence points. The compiler will generate the same machine code with optimisation.

    Incrementing iterators within a loop body is where it can make a difference.
Add Comment