What is the difference between following two if blocks ?<br /> <br /> if(x < 10) <br /> do this;<br /> <br /> else if(x > 10)<br /> do that;<br /> <br /> else<br /> do this and that;<br /> <br /> <br /> <br /> <br /> if(x < 10)<br /> do this;<br /> <br /> if(x > 10)<br /> do that;<br /> <br /> if(x == 10)<br /> do this and that;

Search Interview Questions

 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text


Interview Questions and Answers

 Q1. What is the difference between following two if blocks ?

if(x < 10)
do this;

else if(x > 10)
do that;

do this and that;

if(x < 10)
do this;

if(x > 10)
do that;

if(x == 10)
do this and that;
Ans. Though Logically both code segments are same, but first one is more efficient as the 3 checks are mutually exclusive. In second code segment, all conditions will be evaluated no matter what and will never result in short circuit. In first segment , max 2 conditions will be checked if x >= 10.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     if else

Related Questions

  Difference between == and .equals() ?
  Why is String immutable in Java ?
  Explain the scenerios to choose between String , StringBuilder and StringBuffer ?


What is the difference between String , StringBuilder and StringBuffer ?
  What are the difference between composition and inheritance in Java?
  Does garbage collection guarantee that a program will not run out of memory?
  Why Char array is preferred over String for storing password?
  What are different ways to create String Object? Explain.
  What is a Lambda Expression ? What's its use ?
  Why do we need Inner classes ? Cant we just work with outer classes wherever we implement Inner classes ?
 What is the difference between declaration, instantiation and initialization ?

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: