Basically, because there is absolutely no good
reason to use 1 instead of True (well, it saves some negligible amount of typing, I suppose). The only reason it is done that way in C is because C does not have a boolean type. And even in C, it is considered bad practice to say "while (1)" and instead people "#define TRUE 1" (and most OS will already have that #define'd somewhere, actually). In Symbian, there even was '#define FOREVER for(;;)' so you could say
- Code: Select all
// do stuff;
It's a small readability improvement, but it costs nothing, and every little helps. But I digress.
Reasons for not using "while 1:"
- It's arbitrary. Why "while 1:" and not "while 2:" or "while 'true'"?
- There is something to be said for consistency. As a community, Python programmers decided (or it may have been a decree for Guido, I'm not sure) that "while True" is how we're gonna do infinite loops. Most developers are used to seeing it and when they read code and see "while True", their mind immediately chunks it into a single "forever" token. Because "while 1" is less usual, it draws more attention to itself than it deserves and breaks the flow somewhat.
- It's logically incoherent. Logically, "while 1" makes no sense (you have to draw the arbitrary* parallel that 1 is equivalent to truth for it to make sense).
- You actually incur a conversion cost in the code as int object 1 needs to be converted to a boolean object. Yes, it's negligible (probably to the point of not being worth mentioning), but still.
While none of the above reasons may seem terribly compelling, you have to keep in mind that there is nothing
on the other side of the scales. Since "while 1:" is breaking away from the norm, the question that should be asked is not "why not do it?", but "why do it?"; and there is no good answer to that.
It's a small thing, to be sure, and using "while 1:" is not going to get you mauled by raptors
. My comment you've quoted was nested in a series of others addressing the style of the code being discussed. I would not have mentioned it on its own.
* And it is
arbitrary. For example, in bash 0 is true (because zero return code indicates successful termination).