Ascalonian Ascalonian - 6 months ago 11
Java Question

What is the advantage of a do-while(false)?

When looking through some code that was handled by another employee, I see a lot of code written in:

do{
...
}while(false);


What advantage (if any) does this provide?

Here is more of a skeleton that is happening in the code:

try{
do{
// Set some variables

for(...) {
if(...) break;
// Do some more stuff
if(...) break;
// Do some more stuff
}
}while(false);
}catch(Exception e) {
// Exception handling
}


Update:


C++ Version:

Are do-while-false loops common?

M4N M4N
Answer

Maybe it was done to be able to jump out of the "loop" at any time, e.g:

do
{
    ...
    if (somethingIsWrong) break;
    //more code
    ...
}
while(false);

But as others have said, I wouldn't do it like this.

Comments