Which one is better? do {} while (0); or goto xy;

I have two codes:

A while while:

do
{
    errorflag=0;
    ...
    if(cond1)
    {
        errorFlag=12;
        break;   // Error Conditions
    }
    .
    .            // Processing
    .
    if(cond2)  
    {
        errorflag=56;
        break;
    }
     .
     .

} while (0);

Goto attribute:

errorflag=0;
if(cond1)
{
     errorflag=12;
     goto xy;
 .
 .
 .
 .
if(Cond2)
{
     errorflag=56;
     goto xy;
}
.
.
.

xy:

Which one is better? Please give me information why? or is there a better way to do this? We are optimizing the code. We look most at such large loops. It is a build level; there are not many possibilities for optimization. Please enter your details.

I do not like to use else-if, since this is again the overhead of checking another condition. So go out immediately when there is a problem.

I feel that after this change my question makes sense

Thanks in advance

+3
source share
6 answers

, , do-while goto.

do-while.

goto. - .

, if if-else, .

goto

-8

3:

void frobnicate(arguments) 
{
  if (cond1) return;
  if (cond2) return; 

  ...
}

frobnicate(the_arguments)

.

+13

( , ), , do/while . , gotos. ; ().

, , .

+4

? else?

if (cond1)
{
    //stuff
}
else if (cond2)
{
    // more stuff
}
// etc

else
{
    // default
}

Edit

. , , .

, :

if (!cond1)
{
    .
    .
    .
    .
}

if (cond2) (, cond2 ), , . .

+3

goto- evil. You should use it only gotowhen there is absolutely no other way to do this. gotocan lead to unpredictable spaghetti code (difficult to read, understand, debug, very prone to errors at the service stages).

I believe that an infinite while loop with conditional statements is breakeven better, although it is more readable when conditions are expressed in an instruction while.

-3
source

Source: https://habr.com/ru/post/1779975/


All Articles