Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple IFs with the same return

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.

like image 704
user818794 Avatar asked Jun 28 '11 08:06

user818794


People also ask

Can you put 2 IFS in Excel?

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.

How do you do IFS with multiples?

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.

How many nested ifs can you have?

Remarks. While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.

Can the IFS function return multiple true conditions?

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.


1 Answers

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.

like image 139
tobyodavies Avatar answered Oct 27 '22 02:10

tobyodavies