I've seen some code that looks like this:
if (a) return false;
if (b) return false;
if (c) return false;
return true;
Is there any difference in performance between the above and
if (a || b || c) return false;
else return true;
In general, what would be the preferred case to handle this? Maybe without the else in my second example?
EDIT: It seems a lot of people were mislead by the fact that I return true or false and suggest returning !(a || b || c). This wasn't what I wanted to ask. Imagine if instead of returning true or false I want to return "Yes" or "No", or 23423 or 3.
It is possible to nest multiple IF functions within one Excel formula. You can nest up to 7 IF functions to create a complex IF THEN ELSE statement. TIP: If you have Excel 2016, try the new IFS function instead of nesting multiple IF functions.
To use multiple IF functions where we can add multiple logical tests, after the first logical condition and TRUE value, again insert another IF Function followed by the different logical values to be compared with the TRUE value result.
Remarks. While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.
The IFS function checks whether one or more conditions are met, and returns a value that corresponds to the first TRUE condition. IFS can take the place of multiple nested IF statements, and is much easier to read with multiple conditions.
The only advantage to either one would be readability, it would be reasonable for an optimizing compiler to generate nearly identical code for the two cases.
If the 3 tests are short then return !(a||b||c);
is perfectly reasonable
If however they are long function calls then your first example would be easier to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With