While(1) or for(;;) for infinite loops?

S

scattered

eventually though you become "the other".

Believe me, some of your old code will eventually look very odd to
you!

Oh, I believe you. I've already had cases where I ended up rewriting
from scratch a program that I had intended to merely modify bacause it
seemed easier than trying to figure out what the earlier program was
actually doing. So I do use things like comments and descriptive
variable names much more than I strictly need to. I no longer use my
EVER macro (mostly because of laziness rather than concern for long-
term readability - it's more typing) but thought it still somewhat
amusing.

In one of my more demented moments I wrote a program in Visual Basic
that used a boolean variable FatLadySings in the construct Do ... Loop
Until FatLadySings. It was part of a simulation of the child's game
CandyLand, so I was in a frivolous mood.
 
A

Antti-Juhani Kaijanaho

Oh goodie! Just what we need, double semicolon!

do S ; /* semicolon required to denote infinite looping over S */

Mm. I said "after the block", not "after the statement". But good point, I
forgot C allows a single statement in that position.
 
N

Nick Keighley

In one of my more demented moments I wrote a program in Visual Basic
that used a boolean variable FatLadySings in the construct Do ... Loop
Until FatLadySings. It was part of a simulation of the child's game
CandyLand, so I was in a frivolous mood.

#ifdef HELL_FROZEN
... some ifdef'd out code ...
#endif
 
S

Stefan Ram

Keith Thompson said:
("while(1)" might be clearer than "for(;;)" to someone who doesn't
know C very well, but I wouldn't worry about that.)

This would be a reason in favor of »for(;;)« (I do not want
someone who doesn't know C well to review my code, so I hope
that it would keep such reviewers out or at least make life
harder for them.)
 
S

Stefan Ram

Kaz Kylheku said:
do S ; /* semicolon required to denote infinite looping over S */
Now let S be the statement ``do { } while(condition); '' and we get:

The semicolon is required only in your phantasy.

ISO/IEC 9899:1999 (E) reads:

do statement while ( expression ) ;

There is no »;« after »statement«.
 
N

Not Really Me

Charlie said:
Actually, there is one technical reason that has not been mentioned
yet... Funny it took pete's blunt answer to make me remember it:

for(;;) is a blunt, obvious looping contruct.

while(1) on the other hand can be confused with while(l)

this is imho a good reason to avoid both while(1) and local variables
named l

Putting on my Dilbert Dinosaur costume... Back in the early 80's I was a
one person consulting business. I finally hired a "receptionist/assitant".
She was a returning to work mother with grown kids. The first task I gave
her was to type an old basic program into the computer. You quessed? She
used lower case 'l' instead of 1 throughout the program. I was using
CBasic, so the code was entered in an editor, not a live basic interpreter.
Aargh!

--
Scott
Validated Software
Lafayette, CO



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4673 (20091209) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
K

Keith Thompson

Richard Heathfield said:
Try telling your compiler that.

There is no ';' after 'statement'. There may or may not be a ';' as
the last token of the statement.
 
C

caglar

I feel more comfortable with:
#define TRUE 1
do{
//...
}while(TRUE);
and I think that this is semantically more sound than the for(;;) for
me. Because when i see this snippet, in the code
the compiler in my head automatically translates it to: "do the stuffs
in the loop while all conditions are satisfied".
Also some weird staff(not sure if this is previously mentioned before)
like the following is possible which i'd recommend you to avoid from:

#include <stdio.h>
#include <stdlib.h>

int main(){
int test_var = 0;
loop:
test_var++;
if( test_var == 100 ) {
goto break_loop;
}else {
printf( "I'm inside the loop: %d", test_var );
}
goto loop;

break_loop:
printf("I'm out of loop\n");
return EXIT_SUCCESS;
}
 
K

Keith Thompson

caglar said:
I feel more comfortable with:
#define TRUE 1
do{
//...
}while(TRUE);
and I think that this is semantically more sound than the for(;;) for
me. Because when i see this snippet, in the code
the compiler in my head automatically translates it to: "do the stuffs
in the loop while all conditions are satisfied".

As I'm sure you know, it's semantically identical.

Also, putting the condition at the bottom makes it less obvious
that it's an infinite loop. And both "while (1)" and "for (;;)"
have the considerable advantage of being common C idioms.

I rarely see or use do-while loops in C. (I think the last time I
used one was in my IOCCC entry; I formatted the code so the "while"
looked, at first glance, like it was the top of a while loop rather
than the bottom of a do-while loop.)
Also some weird staff(not sure if this is previously mentioned before)
like the following is possible which i'd recommend you to avoid from:

#include <stdio.h>
#include <stdlib.h>

int main(){
int test_var = 0;
loop:
test_var++;
if( test_var == 100 ) {
goto break_loop;
}else {
printf( "I'm inside the loop: %d", test_var );
}
goto loop;

break_loop:
printf("I'm out of loop\n");
return EXIT_SUCCESS;
}

Yeah, and you can also use a global array as a call stack, so you
can put all your code in main() and use gotos instead of function
calls and returns. (Each return would have to be a switch statement
so it jumps to the correct label, though you can use a simple goto
if the "function" is "called" from only one place.) And you can
put a label on each statement:

L10: printf("This is a loop\n");
L20: goto L10;

And if that's not painful enough, you can glue small sharp pins to the
keys on your keyboard and soak your feet in a bucket of ice water.
 
K

Kaz Kylheku

Mm. I said "after the block", not "after the statement". But good point, I
forgot C allows a single statement in that position.

I think that I always add braces in a do/while, even if there is just a single
expression. It's easy to see why you might forget you can have a single
statement there.

But since we are toying with proposed syntaxes, you are right in
that is no need for that to be the case. This infinite looping construct based
on do could require a compount statement.

There are precedents for this in C itself and in related languages.

In C, the body of a function must be, syntactically, a compound statement.
In principle, it could just have been any statement, allowing things like

void foo(int x)
switch(x)
{
case 42:
break;
}

which is actually neat. :)

In C++, a try block requires a compound statement:

try-block ::= try compound-statement handler-seq

(but there is a function-try-block phrase which allows a try-block to be the
body of a function; which shows that someone had the mental flexibility to
uproot the idea that a function body must be a compound statement).
 
B

Ben Pfaff

Keith Thompson said:
I rarely see or use do-while loops in C. (I think the last time I
used one was in my IOCCC entry; I formatted the code so the "while"
looked, at first glance, like it was the top of a while loop rather
than the bottom of a do-while loop.)

do-while loops are certainly less common than while loops in most
C code, but I think it's absurd to imply that they're only useful
for obfuscation.

Here's one reasonable example from the source tree I'm currently
working on:

do {
nbytes = recvmsg(sock->fd, &msg, (wait ? 0 : MSG_DONTWAIT) | MSG_PEEK);
} while (nbytes < 0 && errno == EINTR);

Here's another:

/* Tries to complete the connection on 'stream', which must be an active
* stream. If 'stream''s connection is complete, returns 0 if the connection
* was successful or a positive errno value if it failed. If the
* connection is still in progress, returns EAGAIN. */
int
stream_connect(struct stream *stream)
{
enum stream_state last_state;

do {
last_state = stream->state;
switch (stream->state) {
case SCS_CONNECTING:
scs_connecting(stream);
break;

case SCS_CONNECTED:
return 0;

case SCS_DISCONNECTED:
return stream->error;

default:
NOT_REACHED();
}
} while (stream->state != last_state);

return EAGAIN;
}
 
K

Keith Thompson

Ben Pfaff said:
do-while loops are certainly less common than while loops in most
C code, but I think it's absurd to imply that they're only useful
for obfuscation.

I certainly didn't mean to imply that. It just happens to be the last
time I used it.

[reasonable examples snipped]

I realize that, back in the Old Days when I wrote Pascal, I used
repeat-until loops more often than while loops. It probably has
something to do with the differing ways Pascal and C do input.
 
P

Peter Nilsson

Ben Pfaff said:
do-while loops are certainly less common than while loops in most
C code, but I think it's absurd to imply that they're only useful
for obfuscation.

The syntax may be rare, but the construct is far from uncommon.

I've mentioned before that I have a header that supplies...

#define until(expr) while (!(expr))

I find it's usage, rare though it may be, exremely clear. Not
obfuscatory at all. The irony is that it's probably only die
hard C programmers who find it confusing!
 
P

Peter Nilsson

Joe Wright said:
But you see, it's not C.

Macros are not C?
Seeing 'until(expr)' in a C program forces you to
stop and look it up.

In a dictionary perhaps, but if you find do-until confusing
you should probably pack in programming and have a go at
ballet dancing. said:
It's abuse of the preprocessor in my view.

NULL is an abuse of the preprocessor in mine.
 
B

bartc

But you see, it's not C. Seeing 'until(expr)' in a C program forces you to
stop and look it up. It's abuse of the preprocessor in my view.

This what I sometimes find annoying:

If a compiler already has all the gubbins needed to implement do...while,
then it is a matter of minutes to also allow do...until. The only cost is
one new keyword.

Clearly some people find it useful.

(Instead the trend is to remove useful bits of syntax! Many languages don't
have goto; Python doesn't have switch (and c.l.python has questions about
how to get around this every few days); Go doesn't have While/Do-while; and
so on. Syntax (for control structures) has virtually zero cost, other than
the reserved words, so I don't know what all these designers are playing
at.)
 
N

Nick

Keith Thompson said:
Ben Pfaff said:
do-while loops are certainly less common than while loops in most
C code, but I think it's absurd to imply that they're only useful
for obfuscation.

I certainly didn't mean to imply that. It just happens to be the last
time I used it.

[reasonable examples snipped]

I realize that, back in the Old Days when I wrote Pascal, I used
repeat-until loops more often than while loops. It probably has
something to do with the differing ways Pascal and C do input.

I've just had a look at my code base. In 32k lines of source (excluding
header files) there are 165 hits on "while .* {" and 63 on "do {". So
roughly a quarter of the loops are do...while - which is slightly higher
than I expected.

There are 35 "for(;;)" in there as well, often bearing comments saying
"will always exit by a return" (and one bearing the comment "barbaric
flow control!"). That also surprises me.
 
B

Ben Pfaff

Kaz Kylheku said:
I think that I always add braces in a do/while, even if there
is just a single expression. It's easy to see why you might
forget you can have a single statement there.

Having a closing brace on the same line as the "while" in a
do/while also avoids a potential visual ambiguity: if the
do/while's "while" is at the top of the screen in the editor
window, and there is no closing brace on that line, then it can
look like an empty "while" loop. Scrolling up immediately makes
the situation clear, but I dislike surprises of this sort.

Coding styles that put braces and keywords on different lines
make this worse.
 
B

Ben Pfaff

Kenneth Brody said:
But, as I said, I maintain code that has been written by
multiple coders, edited on multiple platforms with multiple
editors, and with varied tab-stop settings;

All within a single project or source tree? I suggest that the
problem here is really that there needs to be a single
well-defined coding style for a given project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,287
Latest member
Helenfem

Latest Threads

Top