Does this condition:
if (x > 1)
//some logic
else if (y >47)
//some logic
else
//default logic
Does this always translate to :
if (x >1)
{some logic}
else{
if (y >47)
{some logic}
else
{default logic}
}
Or are there exceptions? If no exceptions, then is there a best practice of how to write this?
These forms are logically equivalent, yes. I think the best practise is to write the code so it is as easy to understand as possible.
Consider:
The truth table is then
Which is the same as for
*NB: Most languages will short-circuit these cases and not evaluate B at all, but some do. This doesn’t affect the logic though.