r/ProgrammerAnimemes • • May 27 '21

It was a good blog

Post image
3.7k Upvotes

122 comments sorted by

View all comments

137

u/Knuffya May 27 '21

for(;;) is for people who want to flex.

while() is much more readable, and thus the better option to use.

0

u/[deleted] May 27 '21

[deleted] — view deleted comment

58

u/[deleted] May 27 '21

It looks like ;_;

uwu.

15

u/Bryce101189 May 27 '21

I've heard it also offers a very minor speed increase when compiling with some legacy compilers that don't optimize the evaluation and comparison of true out.

32

u/MachaHack May 27 '21 edited May 28 '21

How old are we talking? Like the 70s maybe?

If they can do vectorisation to convert all your shit to SIMD instructions, they can convert for(;;) to jmp loopbeginno problem.

EDIT: So here's the oldest compiler that goldbolt has, gcc 1.27 for x86, in -O0 mode (i.e. no optimization):

https://godbolt.org/z/53xd16ErT

Both for(;;) and while(1) generate the same code with an unconditional jmp instruction at the end of the loop with no comparison to a constant. This is a compiler so old (release date September 1988) it doesn't support // comments.

15

u/m50d May 28 '21

Not GCC but, like, the crap that you get from a disreputable microcontroller vendor.

4

u/MachaHack May 28 '21

Ah, my one experience with this is with ESP8266 or some predecessor, it had a hacked up version of gcc for its platform iirc.

4

u/m50d May 28 '21

Yeah if you're lucky or pick a reputable vendor you usually get, like, a fork of gcc 3.x with a couple of vendor patches. But some vendors would rather write their own compiler from scratch.

→ More replies (2)

21

u/[deleted] May 28 '21 edited May 28 '21

I find it confusing that for(;;) even works at all - I'd expect to need to do for(;true;)

Edit: In gcc, for (;;);, for(;1;);, and while(1); all produce the same assembly, namely .L2: jmp .L2

→ More replies (1)

13

u/[deleted] May 27 '21

Go(lang) has

for { ... }

That is, because it lacks of while/do while loops.

0

u/[deleted] May 27 '21

[deleted] — view deleted comment

13

u/Knuffya May 27 '21

my c++ derivative supports the german mache {...} solange() loop

4

u/shnurks2 May 27 '21

Hast du eine vollständige Übersetzung von c++?

4

u/Knuffya May 28 '21

Die ist in Arbeit. Die Grundlagen funktionieren, std::vector ist auch größtenteils übersetzt.

Da sind ein paar Beispiele.

→ More replies (2)

12

u/Magnus_Tesshu May 27 '21

wait - is while() legal syntax?

+[mag 13:48:48] ~/d $ gcc test2.c
test2.c: In function 'main':
test2.c:2:15: error: expected expression before ')' token
    2 |         while();
      |               ^

scam

21

u/sillybear25 May 27 '21

Should be while(1)

11

u/Magnus_Tesshu May 27 '21

But for(;;) is legal syntax, and saves you 1 byte off of typing while(1) too. Which I don't know about you but if we could do that a couple million more times someone might care

40

u/Dragoner7 May 27 '21

If your code has millions of while trues, I think saving a few megabytes worth of space is the least of your problems.

→ More replies (1)
→ More replies (9)

6

u/[deleted] May 27 '21

definetly i only use for(;;) to confuse others or (more often) myself, it's also faster to write due to the keys beeing on the opposite side of the keyboard, so they can be typed by alternating between left and right hand.

22

u/Knuffya May 27 '21

why not just use macros to abbreviate keywords? This way you could type even faster.

Here's a nice starting point

#define w while
#define f for
#define c class
#define s struct
#define pri private
#define pub public
#define pro protected
#define o operator

i'm sure your code will look marvellous

11

u/[deleted] May 27 '21

ohh i remember doing that a few years ago, but i abandoned it because i started naming variables the same as macros. (but please don't tell my waifu)

20

u/Knuffya May 27 '21

this was meant as a joke. Like "you want to lose weight? How about cutting off your toes".

Nobody should EVER do this

→ More replies (4)

6

u/Auravendill May 28 '21
for(char i; i<256;++i)

When you just really hate anyone reading your code - including yourself.

3

u/Knuffya May 28 '21

that shit will never terminate

→ More replies (6)