473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

do/while false

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
11 4228
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
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
"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
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
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
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
> ...
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
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
> > 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert Stearns | last post by:
While attempting to alleviate a persistent but not consistent problem with my database operation (DB2) I coded the do...while loop around my odbc_connect. When $dbConn is false the first time, the...
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
0
by: Yelena Kaplun | last post by:
Hi, I'm trying to customize print settings while streaming HTML content into Excel. I'm using ASP.NET 1.1 and Excel 2003. While some printer settings like Margins are working correctly, I cannot...
75
by: Greg McIntyre | last post by:
I have a Python snippet: f = open("blah.txt", "r") while True: c = f.read(1) if c == '': break # EOF # ... work on c Is some way to make this code more compact and simple? It's a bit...
52
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
1
by: Tmillwee | last post by:
I have the following while statement in my code While dataReader.read( ‘Do Stuff†End Whil The dataReader contains 19 rows of data, but the while loop continues and fails on a 20th loop. I...
3
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
5
by: maestro | last post by:
why does this work? "while p" = "while p != 0" ? 1 is True and 0 is false in python but other numbers have no boolean value so why doesnt it abort. 16 print p p -= 1
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.