Valentin Heinitz Valentin Heinitz - 1 month ago 4x
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);


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.