+ 25

{Tips for Java} Using return with if statement.

Whenever you have a return statement inside an if statement, you do not need to provide an else clause. If an if statement condition is false, the code execution continues after the block. If the statement is true, the method execution will finish with the return statement. if (condition) { // code == true return valueIfTrue; } // code == false return valueIfFalse;

1st Mar 2017, 2:51 PM
Ram chandra Giri
Ram chandra Giri - avatar
3 Answers
+ 15
@Hitesh to reduce indentation caused by else use return that was what I meant to say.
2nd Mar 2017, 1:27 PM
Ram chandra Giri
Ram chandra Giri - avatar
+ 2
thanks mate, this one really helped me out
1st Mar 2017, 4:55 PM
Tengku Izdihar
Tengku Izdihar - avatar
+ 1
you can use break statement then code execution is stop in if block. try it.
2nd Mar 2017, 9:43 AM
Hitesh Morasiya
Hitesh Morasiya - avatar