“Missing return statement” within if / for / while

后端 未结 7 1067
粉色の甜心
粉色の甜心 2020-11-22 02:02

I have a question regarding return statements used within if() while() or for() statements. As you can see in the following method, it

7条回答
  •  孤独总比滥情好
    2020-11-22 02:07

    If you put return statement in if, while or for statement then it may or may not return value. If it will not go inside these statement then also that method should return some value ( that could be null). To ensure that, compiler will force you to write this return statement which is after if, while or for.

    But if you write if / else block and each one of them is having return in it then compiler knows that either if or else will get execute and method will return a value. So this time compiler will not force you.

    if(condition)
    {
     return;
    }
    else
    {
     return;
    }
    

提交回复
热议问题