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

后端 未结 11 1576
栀梦
栀梦 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 21:43

    First of all, it's not a bad style to use break, if you use it judiciously.
    Although I understand why your professor insists on coding without break for now (because so many people, especially beginners, tend to 'overuse' it), there's no reason to ban it completely.

    Is it bad style to use return as a way to break the loop?
    Personally, I think you'll be fine: code will get only simpler. But obviously your professor is the higher authority here. The whole question is very subjective, thus I can't tell for him.

    But again, regardless of what your professor sais, using return in a loop is not a bad style.

提交回复
热议问题