I have a question regarding return statements used within if()
while()
or for()
statements.
As you can see in the following method, it
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;
}