Valentin Heinitz Valentin Heinitz - 1 year ago 72
C Question

Is it common practice to "abuse" loops as goto

If in code there are many if-clauses and execution of code is not sensible on a previous error, is it OK to use a one-cycles loop for this? Simply for being able to exit the block with a break?
Like this:

do {
if (error1) break;
if (errorN) break;
//do finally something when no errors before
} while (false);

Answer Source

Yes, this is idiomatic, even if, perhaps, it was not the intended use for a do while loop. The source code for the linux kernel exploits this.

There's nothing unclear about it: while(false) does exactly what is says on the tin.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download