473,969 Members | 5,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4285
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******** ***********@new s20.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2437
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 program never returns. This problem repeatable with or without the @ on the odbc_connect call. I would someone pointing out my stupidity. I did RTFM, including the examples, and was no further enlightened. Thanks in advance. function conn() {...
33
3912
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 - regardless of the loop beeing entered or not. So where is an actual use case for that feature? I imagined that the else-clause would only be executed if the loop body
0
2048
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 change the Page Orientation to Landscape: mso-page-orientation:landscape;. I tried to produce the Excel Page, save it as HTML, open in text editor and then copy and paste Excel generated settings. But it did not help. If anybody resolved this...
75
5428
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 spaghetti.
52
22030
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
1199
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 have attached a debugger and watched the value of dataReader.read go from True to False after the 19th loop. For some strange reason the loop continues after "While dataReader.read" becomes false Any fixes, tips, or suggestions are greatly...
3
2520
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. I have it set in a way whenever I click the PLAY button, the PLAY button is suppose to hide and the PAUSE button is suppose to appear. This does not happen until the complete while iteration has finished. My goal is to Play and Pause at any time I...
5
1546
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
3408
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. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it vary in color range. Now when i used that php script it generates image successfully but it pixel ate...
0
10344
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10064
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8449
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.