1

Possible Duplicate:
Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?

Why use the ugly for(;;) syntax instead of the slightly better looking while(true) loop?

Community
  • 1
  • 1
Matt Williamson
  • 39,165
  • 10
  • 64
  • 72

2 Answers2

4

There is no advantage to for(;;) over while(1). Use while(1), because it's easier to comprehend (imho). I've never seen a for(;;) used before, and it may be confusing to others who view your code, and may wonder the same thing you just asked.

EDIT: Here's a link: while (1) Vs. for (;;) Is there a speed difference?

It basically says that they both generate the same code. In assembly, it's jmp ....

Community
  • 1
  • 1
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
2

Less typing? It's shorter. And on a QWERTY keyboard, typing for alternates between the left and right hand, and while has three consecutive letters that are typed with the right hand, making typing it potentially even slower.

Ori Pessach
  • 6,777
  • 6
  • 36
  • 51
  • 3
    What is it about C programmers and idiot levels of micro-optimizations? If you're focusing on the speed of your typing, you're doing programming wrong. – JUST MY correct OPINION Sep 05 '10 at 03:34
  • 1
    @JUST MY correct OPINION: Whoosh... – Ori Pessach Sep 05 '10 at 03:41
  • 1
    While you may be (and probably are) joking, I have heard this kind of foolishness before. Vi/m advocates use this kind of reasoning for evangelizing Vi/m, for example, and I have heard from more than a few C programmers that they can't stand the Wirthian family of languages (Pascal, the Modulas, the Oberons, Ada etc.) because it takes too much effort to type `BEGIN ... END` over `{ ... }`. Can you forgive me for confusing your joke with these very tragic realities? ;) – JUST MY correct OPINION Sep 05 '10 at 08:53