0

What are some acceptable use cases for using while true. I’ve heard that it’s not good practice to use while true and break. For simple use cases like let’s say a rps game, would it be worth using. If not, I’m considering to use boiler plate code.

Dirie
  • 21
  • 2
  • "acceptable use cases" is a highly subjective topic. The best way I've seen it described is this: If you have a stopping criteria in mind, then put it in the loop definition/criteria. If the code is meant to run forever without stopping, then use `while True` as it's optimized for truly infinite loops (See [this question](https://stackoverflow.com/questions/71689682/python-i-need-a-continuous-loop-that-will-run-forever-i-know-its-bad-practice) for some efficiency comparisons) – G. Anderson Jul 20 '22 at 18:12
  • You've heard that it is not good pratice, I have heard and seen otherwise. Check "[Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response)" for example. The accepted answer (at the monment about 1000 upvotes) relies on `while True:`. – Matthias Jul 20 '22 at 18:41

1 Answers1

1

You can use while(True) in such conditions when you need to make the code running until interrupted by the person running the code.

A use case can be fee submitting system. The program needs to be running for the entire day. There are a lot of other use cases too.

imxitiz
  • 3,920
  • 3
  • 9
  • 33