0

I was attending a session on Java and the speaker said that it is a bad practice to use break statement in a while loop. He said that it can be used, but it should not be used from a "good coding practice" point of view. He could not satisfactorily explain why is it so.

However, he also mentioned that this restriction is not valid in case of a for loop. Can anyone shed some light on this ?

Edit: I am not talking about while(true). In general for any while(condition).

Pranit Bankar
  • 453
  • 1
  • 7
  • 21
  • I don't think there is any problem with that. We use break statement when we don't want to loop any more. – cody123 Oct 04 '16 at 05:28
  • Possible duplicate of [Is while (true) with break bad programming practice?](http://stackoverflow.com/questions/390481/is-while-true-with-break-bad-programming-practice) – Frederic Klein Oct 04 '16 at 05:29
  • 4
    This is just academic BS. `break` inside `while` is used all the time out here in the real world. There's a long-standing tendency in academia to favour setting or clearing booleans over the `break` statement, often requiring introduction of extra variables for the purpose. Ignore it. – user207421 Oct 04 '16 at 05:32
  • I think that's it's OK to break or continue a loop if you only have one break or continue, if you need to have more, then something is fishy. But that's just my opinion –  Oct 04 '16 at 05:38
  • @F.Klein: I am not talking about `while(true)`. In general for any `while(condition)`. – Pranit Bankar Oct 04 '16 at 05:42
  • Single entry - single exit. But I fail to see how this principle does *not* apply to for-loops... – mtj Oct 04 '16 at 05:48

2 Answers2

1

Many consider break and continue like a goto or instruction since they interrupt the flow of execution and jump to a different point. A good practice would instead be to have a single entry and exit point for each block of execution.

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
1

IMO, using break in a while loop isn't that bad. But I can guess why the speaker said that.

So if I have a while loop like this:

while (condition1) {
    if (condition2) {
        break;
    }
    // do other stuff
}

You can definitely make the code shorter by doing this:

while(condition1 && !condition2) {
    // do other stuff
}

The speaker might think that breaks in a while loop are not neccessary because you can just write all the conditions where you are supposed to write them i.e. in the parantheses.

However, this is not the case. If I have this while loop:

while (condition1) {
    // do other stuff
    if (condition2) {
        break;
    }
}

How do you change that so it doesn't use break?

while (condition1) {
    // do other stuff
    if (condition2) {
        condition1 = false;
    }
}

That doesn't seem really nice to me.

Let's put this into a piece of kind of meaningful code. Which do you think is more readable:

while(noWallInFront) {
    walk();
    if (sawFriend) {
        sayHi();
        break;
    }
}

or

while(noWallInFront) {
    walk();
    if (sawFriend) {
        sayHi();
        noWallInFront = false;
    }
}

Obviously the first one, right? The second one sounds like you're treating your firend like a wall.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    Disclaimer: Opinion-based, etc. -- Nevertheless, I think if you resort to breaks, you probably have made a bad loop definition in the first place. Your example basically tells the program to walk up to a wall. THEN you define an EXCEPTION to that rule. Why? Simply define a rule that can be followed instead. E.g. while(walkingIsAGoodThing) { if(sawFriend) { stopToTalk(); walkingIsAGoodThing = false } if(wallInFront) { walkingIsAGoodThing = false } } One control flow, no exceptions, no suprises. – mtj Oct 04 '16 at 08:59