What's the difference between nested if statements & else if statements?
logic - Nested if statements vs extra else if? - Stack Overflow
Nested if statements vs. and if statements
Nested IF statement in Excel
I would first start by simplifying the IF's because you don't need the AND functions (in your provided example).
If(h4<=1.24, "non",if(h4<=3.75,"left",if(h4<=6.24,"center",if(h4<=8.75,"right",if(h4>8.76,"non")))))This should give you the same results but a little shorter.
More on reddit.comVideos
While the other answers are absolutely right in that your primary focus should be be readability, I want to address another difference: execution performance.
In the first example, there are 2 conditions that need evaluated before the else branch can run. If as we scale the number of conditions baked into this else/if ladder, the amount of evaluation to get to the else branch grows linearly. Now, we aren't expecting to have ten thousand conditions or anything, but it is something to take note of nonetheless.
Now, in your second example, we check the common condition between the first two branches, and if that fails, we quick-fail to the else branch, with no extra tests. In the extreme case, this can somewhat resemble a binary search for the correct code block- branching left and right until it finds its match, as opposed to a linear scan that checks each in order one-by-one.
Now, does this mean you should use the latter? Not necessarily- readability is more important, and if you're writing in a compiled language, the compiler will likely optimize away all that away anyways. And even if you're in an interpreted language, the performance hit is probably going to be negligible compared to everything else anyways, unless this is the hot section of a hot loop.
However, if you are bothered by the "wastefulness" of the repetition in the first example, but would rather avoid huge amounts of nesting, often languages will provide an assignment expression syntax, giving you a 3rd option, where you compute the result once and store it to a variable inline, for reuse in subsequent code.
For example:
if (expensive_func1() > 0 && is_on)
{
// (1)
}
else if (expensive_func1() > 0 && expensive_func2() > 0)
{
// (2)
}
else
{
// (3)
}
Becomes:
if ((is_alive = expensive_func1() > 0) && is_on)
{
// (1)
}
else if (is_alive && expensive_func2() > 0)
{
// (2)
}
else
{
// (3)
}
This saves us from recomputing the common sub expressions between our conditionals, in languages were we can't rely on a compiler to do that for us. Sure, we could just assign these to variables explicitly before the if statements, but then we bite the bullet of evaluating all shared expressions, rather than lazily evaluating them as needed (imagine we compute expensive_func2 > 0 for reuse in a 3rd if/else, only to find out we didn't need it, that we're taking the first branch).
I don't think there is any specific pros or cons to any of the approach. Its all depends upon how you want to design your code and what you think is more readable to anyone who is looking at your code for the first time.
As per me, the first approach looks better as its more readable and contains fewer lines of code.
I'm wondering if there is an efficiency difference between:
if a:
if b:
x + y
and
if a && if b:
x + y
I know that logic-wise, they are asking the same thing, but I'm wondering if the check stops in the second statement if condition a is not met or if both a and b are always checked regardless.
I'm working on a simple game that will run a check 60 times a second. While it shouldn't affect the performance of the simple game in any measurable way, I'm looking to write the code as efficiently as I can. To me, the second would be less confusing and one line shorter, so it would be my preferred method if it is just as efficient.
Also, I'm newly back to programming and this is my first time posting here, so I hope my post is clear and following guidelines. Thanks in advance for your help.