“Missing return statement” within if / for / while

If you put a return statement in the ifwhile or for statement then it may or may not return a value. If it will not go inside these statements 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 ifwhile or for.

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

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

Leave a Comment