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 {
//..code
if (error1) break;
//..code
if (errorN) break;
//do finally something when no errors before
} while (false);
NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.
goto isn't considered harmful because of its positive effects but because of its negative consequences and these have been eliminated by structured programming. Similarly, saying “GOTO is a tool, and as all tools, it can be used and abused” is completely off the mark.
According to the authors of the C programming language, “Formally, [the goto keyword is] never necessary” as it is “almost always easy to write code without it”. They go on to recommend that goto “be used rarely, if at all.”
The goto statement is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With