4

I've two pieces of code:

A do while loop:

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

} while (0);

A goto label:

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

xy:

Which one is better? Please give me the details why? or is there any better way to do this? We are optimizing the code. We are most looking into these kind of big loops. Is assembly level, there is not that much scope for optimisation. Please provide your inputs.

I dont like to use else-if since, it is again a overhead of checking one more condition. So directly exit when there is an issue.

I feel after this edit my question makes sense

Thanks in advance

Rajeev
  • 1,196
  • 3
  • 14
  • 21
  • 2
    What's wrong with a simple `else if`? How many conditions are there? And what language is this actually? – Dirk Vollmar Dec 13 '10 at 10:29
  • 4
    This isn't a very good question. It's like "which is better: Having my brains scooped out with a blunt spoon or having my testicles crushed with a nut cracker". Neither is any good at all, avoid them both! Also, `do...while` is usually (but not always) a sign that you have written the code wrong. Avoid it unless it really really really makes sense. I know lots of people will disagree with that statement, but I've always found that `while...do` constructs are much easier to read and usually just...better. – AlastairG Dec 13 '10 at 11:37
  • 1
    @AlastairG: Having your testicles crushed is survivable. It's clearly the better option. Having said that, I think I'll just not abuse C control structures and avoid either punishment. I agree with you about `do ... while()` I just don't use it. – JeremyP Dec 13 '10 at 12:01
  • @AlastairG: About my question. I have a block. In which there is a series of exit conditions which depends on the processing it does, just above the condtion. So it is useless to use while....do, since it checks the condition first so gives the overhead. In our case it is frequently used function. Thats why we have used do...while. it makes sense in our case – Rajeev Dec 14 '10 at 16:50
  • A similar question with answers: http://stackoverflow.com/questions/1412081/are-do-while-false-loops-common – anatolyg Dec 14 '10 at 17:04

5 Answers5

13

Option 3:

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

  ...
}

frobnicate(the_arguments)

Pick a meaningful name, and keep it short.

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
  • 2
    Virtual -1 for not answering the question. Your `...` should be between the two ifs. – JeremyP Dec 13 '10 at 10:58
  • 1
    This does not work in many cases (for example when you need to do cleanup before returning) – PoVa May 23 '18 at 13:22
5

They generate the same code (assuming the compiler's worth consideration) so the difference is one of which is most easy to understand and whether the do/while interferes with other loop constructs that are about. If there is such interference, use gotos. Otherwise don't; they're less clear (usually).

And look carefully to see if your function is over-complex and should be refactored into multiple functions with a clearer purpose and simpler control flow.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
3

Seriously? Have you never heard of else?

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

else
{
    // default
}

Edit

I misread the question. I'm going to leave the old answer here though because the comments don't make sense otherwirse.

The proper way to code exactly as per what is in the question is:

if (!cond1)
{
    .
    .
    .
    .
}

In both cases in the question, the if (cond2) part has no effect (assuming cond2 has no side effects) because they both cause a jump to the statement that would be executed next anyway. That's why I left it out.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Why the downvote? Depending on the programming language being used and the number of conditions this code is straight-forward and pretty easy to read. Of course long and complex nested if's are always a good canditate for a refactoring but in this case we know too little to assume anything like that. – Dirk Vollmar Dec 13 '10 at 10:38
  • @0xA3: It's not a solution to the question, that's why. – Donal Fellows Dec 13 '10 at 10:49
  • Juding from the initial code, the if-based solution would be `if (!cond1) { ... if(!cond2) { ... } }` (no else required). – Victor Nicollet Dec 13 '10 at 10:49
  • @Victor Nicollet: I see what you mean, I misread the original. I'm editing my answer. – JeremyP Dec 13 '10 at 10:52
  • @Donald Fellows, @Victor Nicollet, @JeremyP: The problem here is that the question only shows a code snippet which is pretty useless. Given that the conditions themselves don't have side-effects you could simply as well remove all the code. But the OP has left the scene so we are not going to know what the code actually is supposed to do. – Dirk Vollmar Dec 13 '10 at 11:02
  • @0xA3: If cond1 evaluates as true, you don't execute anything, but if it evaluates false, you go through the dotted bit. – JeremyP Dec 13 '10 at 11:05
-4

goto is evil. You should only use gotos when there is absolutely no other way to do this. gotos can lead to unpredictable spaghetti code (hard to read, to understand, to debug, very prone to mistakes on maintenance phases).

I believe that an infinite while loop with conditional break statements is still better, although it's more readable when the conditions are expressed in the while statement.

Alex
  • 14,338
  • 5
  • 41
  • 59
  • 1
    The loop here is not infinite. Seems a simple `else if` would do... (this also shows how easily this code can be mis-read, as already mentioned in @Victor Nicollet's comment) – Dirk Vollmar Dec 13 '10 at 10:31
  • @0xA3: the voice of sanity... – JeremyP Dec 13 '10 at 10:34
  • 3
    using goto in this case is much clearer than the abused do/break/while construction. – salva Dec 13 '10 at 11:30
  • 2
    @salva: Using an if statement would be better. – JeremyP Dec 13 '10 at 14:11
  • "`goto` is evil" is a dogma put into the world by enthusiasts. Frowning upon something only because of its name is bad. Other control statements can lea to unreadable code as well. Just because `goto` can be used in a bad manner, it is not completely bad. – glglgl Nov 10 '11 at 15:19
  • No, mainly because the code literally becomes unreadable. One good placed goto leads to another, and after a while you will be cursed by each developer who looks into your code. Code can be made bad enough by junior developers (and even senior) by using `while` and `for`. Once they get a green light for `goto` statements - you're doomed. There is absolutely no reason to use `goto`, if you're using languages like C. And as long as you can't prove that I'm wrong, the fact that you downvoted 2 answers just makes you a lonely angry old-school developer in my eyes, and in no case a good developer. – Alex Nov 11 '11 at 12:58
  • And yes, the statement isn't by itself bad. But only because it is usually used in a bad manner, it is considered bad. It's life. Deal with it. – Alex Nov 11 '11 at 12:59
-9

The choice here, base on your question, is do-while and goto.

I recommend you choose do-while.

I discourage you to use goto. That will lead you to spaghetti-code later on.

You can have alternatives like if and if-else to come up with the same result.

Say no to goto

Neilvert Noval
  • 1,655
  • 2
  • 15
  • 21
  • 6
    This is a nonsense-statement. You can write spaghetti code perfectly without `goto`, and you can write clean code containing `goto` e.g. for error handling. – glglgl Nov 10 '11 at 15:16
  • @glglgl Languages adapted to error handling as well. For example, the use of structured exception handling and `finally` clauses. If it's a good enough problem, languages adapt to handle it better (as long as it doesn't break the language too much). Using `goto` still means you need to remeber to jump to this particular label when handling errors - using `finally`/`catch` lets you just use `return` as anywhere else. This is especially important when copying code - it's easy to forget to change that one `return` to a `goto releaseStuff;`. – Luaan Jul 28 '15 at 07:50
  • @Luaan If you have a language with proper exception handling, there is indeed no reason to use goto. But in C, it's often the simplest thing. – glglgl Jul 28 '15 at 07:58