Is it bad style to use 'return' to break a for loop in Java?

后端 未结 11 1608
栀梦
栀梦 2020-12-18 21:01

I\'m doing a project with basic Java for a CS class. The project has a for loop nested inside a while loop.

I am not allowed to use break as a wa

11条回答
  •  我在风中等你
    2020-12-18 22:00

    This question seems to enter the territory of a very old still unresolved argument, the "Single vs. Multiple method exit points".

    So if using return in a loop is worrying you (or your teacher), you (or he) could have a look here.

    To answer your question. In my (and not only my) opinion using a return in a loop is definitely OK.

    Do note however that overusing it might decrease code readability (see linked answer for details/examples).

提交回复
热议问题