In a simple if-else case (where the denominator of a fraction will need to be checked for a value of zero for each entry), does a correct arrangement of the statements even exist?
For example:
if (!(denominator == 0))
{
quotient = numerator / denominator;
System.out.println(numerator + " / " + denominator + " = " + quotient);
}
else
{
System.out.println("Division by zero is not allowed.");
}
or
if (denominator == 0)
{
System.out.println("Division by zero is not allowed.");
}
else
{
quotient = numerator / denominator;
System.out.println(numerator + " / " + denominator + " = " + quotient);
}
I know this is a trivial example, but I am curious if there is any benefit to giving consideration to the ordering of the if-else statements (in a case such as above). For less trivial testing of input/values, I can understand the consensus answer put forth here: Best Practice on IF/ELSE Statement Order.
Apologies in advance if this is a bad question (or one with what should have an obvious answer). My intent is to increase the general efficiency of my coding. Although, I doubt that there is any significant difference between the two examples that I have given. My newbie analysis indicates that the denominator will be tested for zero either way therefore negating any benefit of testing for the "normal case". Please correct/enlighten me if I am wrong.
TIA