Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it common practice to "abuse" loops as goto [closed]

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);
like image 769
Valentin H Avatar asked Sep 19 '16 11:09

Valentin H


People also ask

Why goto is not recommended to use in programs?

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.

Why is goto statement harmful?

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.

Should you ever use goto in C?

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.”

When should goto be used?

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.


1 Answers

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.

like image 133
Bathsheba Avatar answered Oct 17 '22 02:10

Bathsheba