Is while (true) with break bad programming practice?

There is a discrepancy between the two examples. The first will execute the “do something” at least once every time even if the statement is never true. The second will only “do something” when the statement evaluates to true.

I think what you are looking for is a do-while loop. I 100% agree that while (true) is not a good idea because it makes it hard to maintain this code and the way you are escaping the loop is very goto esque which is considered bad practice.

Try:

do {
  //do something
} while (!something);

Check your individual language documentation for the exact syntax. But look at this code, it basically does what is in the do, then checks the while portion to see if it should do it again.

Leave a Comment

tech