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

后端 未结 11 1582
栀梦
栀梦 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:40

    Also does it matter if 'return' doesn't return anything?

    That depends on your method's return type. if the return type is void, a return clause must be empty:

    return;
    

    otherwise, a return clause must return the required type, e.g.

    return true; // legal for boolean return type
    return null; // legal for all object types
    return "Hello"; // legal if return type is String
    

提交回复
热议问题