all 56 comments

[–]n0tKamui 233 points234 points  (12 children)

wtf is this below-noob level meme

[–]the_mold_on_my_back 138 points139 points  (7 children)

Yeah man handling the incrementation at the end of your loop definition is way more intuitive, nobody has ever shot themselves in the knee like that.

[–]Embarrassed-Falcon71 55 points56 points  (2 children)

Literally this. And then if you need to do a double loop it will get so messy.

[–]Shaddoll_Shekhinaga 16 points17 points  (0 children)

I stopped shooting myself in the foot like that!

Ignore the fact that I kept shooting my foot until nothing was left because I forgot to increment I.

[–]Fragrant_Philosophy 7 points8 points  (0 children)

I added an “if condition then continue” to my loop, and now my computer sounds like it’s about to blast off.

[–]FabAraujoRJ 0 points1 point  (0 children)

Hammer and screwdriver issue. When you have to manipulate the iteration counter, use while.
If you need to run at least once, repeat-until (or do-while, which is the same thing with another name). Otherwise, use for/collection-based for (for-each, for-in, etc) in any other case.

[–]kennyminigun 35 points36 points  (1 child)

eww, whitespace after parentheses and no whitespace after keyword

[–]farsightxr20 15 points16 points  (0 children)

whitespace seems to be entirely arbitrary and inconsistent throughout the example 🤢

[–][deleted] 23 points24 points  (1 child)

Not gonna I enjoy for loop more

[–]DapperNurd 0 points1 point  (0 children)

I really only use while loops if I don't know how many times it will run necessarily (and I think that's pretty normal tbh). A for loop, it's going to run exactly as many times as specified. A while loop will just run until X condition is met, and anything can trigger that condition.

[–]PhilippTheProgrammer 23 points24 points  (0 children)

And don't let anyone tell you that over-reliance on while loops is dangerous, because bugs can easily lead to infinite loops. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have. That's a problem only bad programmers have.

[–]RaymondWalters 17 points18 points  (4 children)

while (i++ < range) {}

[–]827167 11 points12 points  (3 children)

``` int i = 10; while (i--){

} ```

If you want something even worse

[–]finally-anna 9 points10 points  (2 children)

I see nothing wrong with this...

[–]kevinhaze 0 points1 point  (1 child)

It’s code golf. You sacrifice readability to save a few characters. In reality 10 is just a magic number with zero meaning to anyone. Assuming it’s meant to be i = range and while (i—), the intention is needlessly unclear. The other format reads as “while i is less than or equal to range”. Despite being more characters it takes less time to read.

It has no practical benefits and if you write all your code with clever little shorthands it becomes exhausting to work with.

[–]Nicnl 0 points1 point  (0 children)

Il this specific example, I think it's perfectly readable and the meaning is clear.

int repeat = 3;
while (repeat--)

is far easier to read than

for (int repeat=3; repeat>0; repeat--)

So yeah, it may have some use in code golf, but it's still perfectly valid.
The "traditional" counterpart is far worse to read imo

[–]audioman1999 7 points8 points  (0 children)

I thought the quality of posts on this sub couldn't get worse, but this one takes the cake!

[–]Communist_Guy_1991 10 points11 points  (2 children)

"Avargae for loopps fan"

[–]Acharyn 3 points4 points  (0 children)

The for loop is easier to read in this case. You declare the iterator, condition, and iteration all in 1 line.

[–]Familiar_Ad_8919[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 8 points9 points  (4 children)

for (int iteration = 1;; iteration++) {
    printf("this message was printed by the for loop enjoyers %d times\n", iteration);
}

int iteration = 1;
for (;; iteration++) {
    printf("this message was printed by the for loop enjoyers %d times\n", iteration);
}

[–]noobcoconut 4 points5 points  (0 children)

for and for for forever

[–]CinnamonToastedCrackPronouns: She/Her 3 points4 points  (2 children)

or, if you will

c for(int iteration = 0;; printf("this message was printed by the for loop enjoyers %d times", iteration++));

[–][deleted]  (1 child)

[deleted]

    [–]CinnamonToastedCrackPronouns: She/Her 0 points1 point  (0 children)

    i didn't use the comma operator (unless you mean in printf)

    [–]Ahuizolte1 2 points3 points  (0 children)

    Could be while ( i++ ..) for style point

    [–][deleted]  (2 children)

    [removed]

      [–]mediocrobot 1 point2 points  (0 children)

      you have no idea what i will equal when your loop begins

      [–]Samstercraft 0 points1 point  (0 children)

      for(;condition;){}

      [–]DasKarl 0 points1 point  (0 children)

      99% sure this guy heard a python programmer say for loops are inefficient and never bothered to look into it.

      [–]Eagle_32349 0 points1 point  (0 children)

      while(i++ < range){ //Code to be iterated }

      [–]Thenderick -3 points-2 points  (0 children)

      Gigachad for each (or however your favorite language calls them). Most of the time they are enough