C
Christopher Benson-Manica
I have a situation something like this:
int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) {
if( !baz(foo) ) break;
/* do stuff with foo */
}
The idea is that the loop happens at most twice - once for foo=bar(), and once
for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't
think this is the "elegant" way to get this behavior - there is a break (which
might be bad style, for some), and bar() may be called three times (which
doesn't seem optimal). I thought of
int foo=bar();
do {
if( !baz(foo) ) break;
/* doo stuff with foo */
} while( foo++ == bar() );
, but it's no better (and is somewhat more obfuscated, to boot). Any
thoughts?
int foo;
for( foo=bar() ; foo <= bar()+1 ; foo++ ) {
if( !baz(foo) ) break;
/* do stuff with foo */
}
The idea is that the loop happens at most twice - once for foo=bar(), and once
for foo=bar()+1. If baz(foo) fails, I know I can stop. However, I don't
think this is the "elegant" way to get this behavior - there is a break (which
might be bad style, for some), and bar() may be called three times (which
doesn't seem optimal). I thought of
int foo=bar();
do {
if( !baz(foo) ) break;
/* doo stuff with foo */
} while( foo++ == bar() );
, but it's no better (and is somewhat more obfuscated, to boot). Any
thoughts?