If vs If / Else

90
Views
2
Answers

What is the difference in

if (foo == true)  
 
return 1;
return 2;

and

if (foo == true)   
return 1;
else  
return 2;

and which one is better?

edit | flag
Gokul Lilley
Asked on: Jul 17, 2012 at 10:25PM

2 Answers

143
-2
143

if (foo == true)

return 1;

return 2;

EXPLANATION:

If the condition is true it will return both return 1 and return 2 values.

but in this condition,

if (foo == true)
return 1;
else

return 2;

In if else condition if the condition is true it will return the values of return 1 only.
or if the condition is false it will return the values of return 2 only.

edit | flag
vijay anand
Answered on: Aug 12, 2012 at 8:33PM
152
0
152

if (foo == true)

return 1;
else

return 2;

The above method is the method.
It will look for the condition and depending on whether foo is true or false it will return 1 or 2.

Whereas the below code
if (foo == true)
return 1;
return 2;
This will return back from return 1 statement itself

edit | flag
Ehtesham Siddiqui
Answered on: Sep 06, 2012 at 2:53PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags