News for hackers. Stuff that matters.
Re:The Beauty of Bresenham's Algorithm by barbs on Tuesday August 07, 2012 @09:04PM (#4353311)
Why do people write non-conditional loops as "for(;;)" instead of "while(1)"?
Re:The Beauty of Bresenham's Algorithm by biot on Tuesday August 07, 2012 @10:10PM (#4353502)
According to the C99 standard [0], an omitted optional expression-2 in a for loop is replaced by a non-zero constant. Thus, for(;;) is equivalent to for(;1;) which is effectively equivalent to while(1) as there are no other declarations or expressions. And, for those who are counting, it also saves a character.

[0] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf (See 6.8.5.3)

Re:The Beauty of Bresenham's Algorithm by ThePherocity on Wednesday August 08, 2012 @12:30AM (#4353909)
Why would anyone wreck legibility to save a character?
Re:The Beauty of Bresenham's Algorithm by dbaupp on Wednesday August 08, 2012 @01:55AM (#4354162)
I don't think it wrecks legibility, since it's a C idiom. People reasonably experienced in C will read "for (;;)" as an infinite loop just as well as "while(1)". That said, it might be a little confusing for beginners (or others who don't use C very regularly).
Re:The Beauty of Bresenham's Algorithm by Jimmie on Wednesday August 08, 2012 @01:06AM (#4354020)
Ahhh, the ole' semicolon debate.
Re:The Beauty of Bresenham's Algorithm by sclangdon on Wednesday August 08, 2012 @03:05AM (#4354336)
Because while(1) evaluates to a constant, which can sometimes be a bug and thus, some compilers emit a warning.

http://msdn.microsoft.com/en-us/library/6t66728h%28v=vs.80%2...

http://stackoverflow.com/questions/3490823/why-msvc-generate...

Re:The Beauty of Bresenham's Algorithm by ephemient on Tuesday August 07, 2012 @09:10PM (#4353329)
So that you can do tricks like this:

    #define ever (;;)
    for ever {...}
;-)
Re:The Beauty of Bresenham's Algorithm by georgemcbay on Tuesday August 07, 2012 @09:15PM (#4353345)
Not applicable to the original article, but the reason I do it is because Go has no while loop. It doesn't require all those extra semis, though:

  for { 
    ...
  }
Re:The Beauty of Bresenham's Algorithm by nitrogen on Tuesday August 07, 2012 @09:11PM (#4353333)
Because while(1) still has a condition that is checked on every loop iteration when optimizations are disabled. for(;;) can be read as "forever".
Re:The Beauty of Bresenham's Algorithm by morsch on Tuesday August 07, 2012 @09:18PM (#4353356)
FWIW, GCC and Perl generate the same code for both.

http://stackoverflow.com/questions/885908/while-1-vs-for-is-...

Re:The Beauty of Bresenham's Algorithm by barbs on Tuesday August 07, 2012 @09:25PM (#4353380)
Interesting. Looks like they'd be the same performance wise, since

for (;;);

is the same as

{ while (true) { ; ; } }

I originally asked this question cos it's used this way in first C program in the link. Bit of a tangent though :P

Re:The Beauty of Bresenham's Algorithm by WalterBright on Wednesday August 08, 2012 @12:47AM (#4353964)
That issue disappeared before the 1980's. Check your compiler.
Re:The Beauty of Bresenham's Algorithm by jejones3141 on Wednesday August 08, 2012 @12:11AM (#4353854)
Any compiler/interpreter worth its salt will notice when an expression being tested in a conditional is constant and generate code that doesn't actually test it at run time. for (;;) is arguably more "idiomatic" C, though.
Re:The Beauty of Bresenham's Algorithm by Someone on Wednesday August 08, 2012 @06:45AM (#4354874)
for(;;), being equivalent to for(;1;), also has that condition that has to be checked. So, it all boils down to how compilers treat the two.

And to add oil to the fire, here are some even 'better' ways to do this:

  do {
    ...
  } while(1);
At least that fits the common macro pattern "do{...}while(0)" (http://stackoverflow.com/questions/257418/do-while-0-what-is...).

Of course, all of these are inferior to:

  considered:
    ...
  goto considered;
Re:The Beauty of Bresenham's Algorithm by FredBrach on Tuesday August 07, 2012 @09:21PM (#4353366)
I do doubt that even for a for(;;), a jmp (x86) is use instead of a conditional jump like jz.
Re:The Beauty of Bresenham's Algorithm by dbaupp on Wednesday August 08, 2012 @01:57AM (#4354165)
Compilers are smart! For common things like this, they will easily optimise away any conditional jumps.
Re:The Beauty of Bresenham's Algorithm by tsahyt on Wednesday August 08, 2012 @09:27AM (#4355377)
It is, you can easily check that yourself. for(;;) compiles to exactly one jmp in GCC (tested with gcc 4.7.0)
Re:The Beauty of Bresenham's Algorithm by TwoBit on Wednesday August 08, 2012 @12:40AM (#4353945)
VC++ generates a warning with while(1) but not with for(;;). I just can't remember what warning level you need in order to see that distinction.
Re:The Beauty of Bresenham's Algorithm by pslam on Tuesday August 07, 2012 @09:27PM (#4353383)
Why do people write while(1) instead of for(;;)? Are you saying that "while(1)" is superior in some way? Why?

I prefer "for(;;)" because it is explicitly defined as an infinite loop, but "while(1)" needs an implicit cast of "1" to "true", and even then there's an implicit test for "true".

Re:The Beauty of Bresenham's Algorithm by wollw on Tuesday August 07, 2012 @10:12PM (#4353507)
True and false are actually defined as 1 and 0 respectively. I don't have a copy of the standard but this specifies true and false as macros.

http://pubs.opengroup.org/onlinepubs/007904975/basedefs/stdb...

Edit: Of course I'm talking about C here. Also, I prefer for(;;) because it's an easy to recognize idiom that means "infinite loop."

Re:The Beauty of Bresenham's Algorithm by tsahyt on Wednesday August 08, 2012 @03:41AM (#4354399)
There's no casting involved in while(1). 1 is "true" in C, in fact, any integer != 0 is "true".

I just did a quick check and gcc -S produces the same ASM output for both of them, which doesn't include any comparisons whatsoever, just a jmp. So both of them are really compiled to an actual infinite loop.

Re:The Beauty of Bresenham's Algorithm by barbs on Tuesday August 07, 2012 @09:43PM (#4353434)
I guess it's just personal preference. "for(;;)" looks really messy to me, and doesn't seem obvious that it signifies an infinite loop. I think that even the standard format for for loops (for (initialisation; condition; thing done at end of loop)) is quite unintuitive, and I have to think a bit to remember what each part does if one of them is omitted. It's also the only place I can think of where semicolons aren't expected at the end of a line (conventionally). I don't find a problem implicitly casting "1" to "true", I sort of do this automatically after programming in C for a while. Obviously if I were using java or something, I'd use "while(true)". Basically, I don't have to think much to know that it's an infinite loop :)
Re:The Beauty of Bresenham's Algorithm by sp332 on Tuesday August 07, 2012 @09:34PM (#4353405)
You could just say while(true). And I'm not sure that the test for true is implicit, since that is what while() explicitly does.
Re:The Beauty of Bresenham's Algorithm by zanny on Wednesday August 08, 2012 @02:37AM (#4354271)
C doesn't have true / false as language constants, only as override-able macros.

The good thing is modern compilers can evaluate a loop and if the break condition is constant it can just do an infinite loop without checking.

Re:The Beauty of Bresenham's Algorithm by ajays on Wednesday August 08, 2012 @12:23AM (#4353891)
It's 1 character shorter.... ;)