By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,145 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,145 IT Pros & Developers. It's quick & easy.

do/while false

P: n/a
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;
This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading. The same could be
achieved with a boolean flag.

e.g.,

bool bSuccess = CatchFish(pFish);
if (bSuccess) {
bSuccess = CleanFish(pFish);
}
etc.

Exceptions are not being used because we are assuming that the methods
can return false under non-exceptional circumstances.

Any comments on this sort of do/while (false) style?

Angus.

Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Angus Graham wrote:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;


What's wrong with if - else if?

if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}
delete pFish;
Jul 22 '05 #2

P: n/a
On Sat, 14 Feb 2004 12:46:06 -0500 in comp.lang.c++, Angus Graham
<pl****************@notme.com> was alleged to have written:
This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading. The same could be


Well, you could do it Perl style...

CatchFish(pFish)
&& CleanFish(pFish)
&& CookFish(pFish)
&& EatFish(pFish);

Jul 22 '05 #3

P: n/a
"Angus Graham" <pl****************@notme.com> wrote in message
news:F9*******************@news20.bellglobal.com.. .
This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading.


The advantage of a do loop is that it is guaranteed to execute at least
once; but to my mind they imply the possibility of executing more than once.

It reminds me of two other questionable but popular language structures: the
for(;;) loop and the if(true) statement

I avoid their use as well.
Jul 22 '05 #4

P: n/a
Aggro wrote:
Angus Graham wrote:
Hi:

I've recently come across a number of programmers who use the
following type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
Final if() is redundant.
while (false);
delete pFish;
What's wrong with if - else if?

if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}


Here too.
delete pFish;


Or if you want to avoid the empty if statements, at the price of some
nesting:

if( CatchFish(pFish) )
if( CleanFish(pFish) )
if( CookFish(pFish) )
EatFish();
delete pFish;

--
Unforgiven
"Most people make generalisations"
Freek de Jonge

Jul 22 '05 #5

P: n/a
Angus Graham, 2/14/2004 9:46 AM:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;

[...snip...]

I think some people use this sort of thing (perhaps not in as simple a
context as this) as a substitute for 'goto'.

denis
Jul 22 '05 #6

P: n/a
On Sat, 14 Feb 2004 12:46:06 -0500, Angus Graham <pl****************@notme.com> wrote:
I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;
Well, they don't know shit about programming. Should have used a
std::auto_ptr here. That code is not exception-safe.
This is effective but seems to me to be bad style.
Yep.
The do/while loop
isn't really a loop at all which is misleading. The same could be
achieved with a boolean flag.

e.g.,

bool bSuccess = CatchFish(pFish);
if (bSuccess) {
bSuccess = CleanFish(pFish);
}
Hungarian notation is [censored derogatory expression].

And succcess/failure flags are seldom useful for anything.

Use a simple if-else.
Any comments on this sort of do/while (false) style?


The do-while style is useful in macros, allowing a macro to be used
as a statement in all cases where C/C++ allows a statement.

Jul 22 '05 #7

P: n/a
> ...
while (false);
delete pFish;
Well, they don't know s*!t about programming. Should have used a


Why use foul language here, and then censor yourself when
referring to Hungarian notation?
... Hungarian notation is [censored derogatory expression].

And succcess/failure flags are seldom useful for anything.
Very useful in making sense of a more complicated if then else where
it is harder to ensure a single "else" part.
Use a simple if-else.
Agreed, this would make most sense.
Any comments on this sort of do/while (false) style?


I think it was the mid 1960s when goto-less programs were championed, this
seems to be a throwback to then :-)
The do-while style is useful in macros, allowing a macro to be used
as a statement in all cases where C/C++ allows a statement.


Agreed - an excellent use of "do ... while (0);" (assuming you have
suitable
justification for using macros in the 1st place :-)

JK
Jul 22 '05 #8

P: n/a
On Mon, 16 Feb 2004 16:34:20 -0000, "John Kewley" <j.******@dl.ac.uk> wrote:
...
>while (false);
>delete pFish;


Well, they don't know s*!t about programming. Should have used a


Why use foul language here, and then censor yourself when
referring to Hungarian notation?


I thought what I was about to write about Hungarian notation would
qualify as foul language... ;-)

Jul 22 '05 #9

P: n/a
> > I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;


What's wrong with if - else if?

if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}
delete pFish;


This form doesn't let you insert extra statements without obfuscation;
eg. if you wanted to perform some series of non-checked actions
in between the cleaning and the cooking

To the OP: using try...catch is a bit less verbose,
especially if you want to know what the failure reason was at
the end of the code.
Jul 22 '05 #10

P: n/a
Angus Graham wrote:

Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;

This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading. The same could be
achieved with a boolean flag.


Could be a placeholder for LATER development when that
chunk would need to be a real loop. When they get to
that point, they can replace the "false" with whatever
condition is applicable. This gives them a quick one-
time run through the code without having to drastically
change the syntax to add looping later.

Mike
Jul 22 '05 #11

P: n/a
Angus Graham wrote:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;
This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading.


This technique is typically used in definitions of debugging macros to
introduce a scope for local variables. For example (admittedly more of a
C example than a C++ example):

#define LOOP(f,x,a,b) do{ int i, j; \
for(i=0; i<a; ++i){ \
for(j=0; j<b; ++j){ \
printf("debug: " f, i, j, x[i][j]); \
} \
} \
}while(0)

which can be used as

int x[10][20] = {...};
DBG("x[%d][%d]=%d", x, nelem(x), nelem(x[0]));

I agree that I would not recommend its use as you have presented it.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.