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

tricky do-while

P: n/a
Hi All,

I saw in some code

do{//for error jump
/*Some code here*/
}while(0);

Can anyone plese tell me what is the purpose of this code? There was a
comment saying that //for error jump. What does it mean? There is no
error related code inside the do-while block. I guess the comment
specifies the reason for that block.

Regards,
Mohan.
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Mohanasundaram" <mo************@msn.com> wrote in message
news:9b**************************@posting.google.c om...
Hi All,

I saw in some code

do{//for error jump
/*Some code here*/
}while(0);

Can anyone plese tell me what is the purpose of this code? There was a
comment saying that //for error jump. What does it mean? There is no
error related code inside the do-while block. I guess the comment
specifies the reason for that block.

Regards,
Mohan.

Hello,

This is used to break out of a code in case of errors...consider this:
do
{
/* ....... */
if (!someFunction())
break;
/* .... */
if (!someChecks())
break;
/* ......*/
} while (0);
/* put here some de-initialization code */

Follow this thread for more info:
http://groups.google.com/groups?hl=e...fe=off&frame=r
ight&th=b90082f332d8b543

--
Elias
Nov 14 '05 #2

P: n/a
mo************@msn.com (Mohanasundaram) wrote:
I saw in some code

do{//for error jump
/*Some code here*/
}while(0);

Can anyone plese tell me what is the purpose of this code?


The purpose of this code is to write spaghetti code, yet at the same
avoiding the use of goto to satisfy the anal-retentive reviewers.

IOW, it's an illegible botch. If you need a goto, damn well use one.

Richard
Nov 14 '05 #3

P: n/a
Mohanasundaram wrote:
Hi All,

I saw in some code

do{//for error jump
/*Some code here*/
}while(0);

Can anyone plese tell me what is the purpose of this code? There was a
comment saying that //for error jump. What does it mean? There is no
error related code inside the do-while block. I guess the comment
specifies the reason for that block.


Richard Bos' comments notwithstanding, this idiom is more commonly seen in debug
macros to avoid dangling else clauses, and to introduce a new scope.

/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
Nov 14 '05 #4

P: n/a
David Rubin <no****@nowhere.net> wrote:
Mohanasundaram wrote:
do{//for error jump
/*Some code here*/
}while(0);

Can anyone plese tell me what is the purpose of this code? There was a
comment saying that //for error jump. What does it mean? There is no
error related code inside the do-while block. I guess the comment
specifies the reason for that block.


Richard Bos' comments notwithstanding, this idiom is more commonly seen in debug
macros to avoid dangling else clauses, and to introduce a new scope.


It is rarely seen with that comment in macros (where, you are correct,
it _is_ useful), and to introduce a new scope a simple pair of braces
suffices.

Richard
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.