Do Google Sheets logical operators short circuit? - if-statement

Basically the title. Making a big Analytics based spreadsheet that tests for a lot of date conditions. I would like to know if the AND operator supports short-circuit evaluation.

"short-circuit evaluation" is basically nesting of IF functions:
=IF(A1=1, TRUE,
IF(A1=2, TRUE))
for multi-criteria it would be:
=IF(AND(A1=1, B1=1), TRUE,
IF(AND(A1=2, B1=2), TRUE))
when it comes to arrays/ranges it will be like this:
=ARRAYFORMULA(IF((A1:A=1) * (B1:B=1), TRUE,
IF((A1:A=2) * (B1:B=2), TRUE)))

Related

Using ARRAYFORMULA with SUMIF for multiple conditions combined with a wildcard to select all values for a given condition

I am using ARRAYFORMULA with multiple conditions using SUMIF concatenating the conditions using & and it works. Now I would like to use similarly this idea for a special condition indicating to consider all values using a wildcard ("ALL") for a given column, but it doesn't work.
Here is my example:
On I2if have the following formula:
=ARRAYFORMULA(if(not(ISBLANK(H2:H)),sumif(B2:B & C2:C & D2:D & year(E2:E),
if($G$2="ALL",B2:B,$G$2) & if($G$4="ALL",C2:C,$G$4) & if($G$6="ALL",D2:D,$G$6) &
H2:H,A2:A),))
and it works, when I enter specific values, but when I use my wildcard: ALL indicating that for a given column/criteria all values should be taken into consideration, it doesn't work as expected. The scenario should consider that all criteria can be labeled as ALLin such case it will provide the sum of NUM per year.
Here is my testing sample in Google Spreadsheet:
https://docs.google.com/spreadsheets/d/1c28KRQWgPCEdzVvwvXFOQ3Y13MBDjpEgKdfoLipFAOk/edit?usp=sharing
Notes:
I was able to get a solution for that using SUMPRODUCT but this function doesn't get expanded with ARRAYFORMULA
In my real example I have more conditions, so I am looking for a solution that escalates having more conditions
use:
=QUERY(QUERY(FILTER(A2:E,
IF(G2="All", B2:B<>"×", B2:B=G2),
IF(G4="All", C2:C<>"×", C2:C=G4),
IF(G6="All", D2:D<>"×", D2:D=G6)),
"select year(Col5),sum(Col1)
where Col1 is not null
group by year(Col5)"),
"offset 1", 0)

Two conditions in Google Sheets Function

I have a question on using two functions with an if statement in Google Sheets as one complete function. Both variables have to be true, otherwise it returns false. I need one function to check the date 20 months back from today. If said cell is less than today's date 20 months back it's true, naturally. However, for the complete function to return true it also searches for another text value in another cell and has to be an exact match. Both conditions have to be true (the date and the exact match) for the function to be true. So if the date in the cell is less than today's date 20 months back and the text value in the other cell is an exact match, function is true.
Problem is that it seems like the date function does not seem to apply.
=IF(D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())),AND(REGEXMATCH(M2,"text")),TRUE,FALSE)
You current formula is not set up correctly (nor logically). Given only what you've shown here, this should work:
=IF(AND( D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text") ),TRUE,FALSE)
Notice that the AND( ) contains both conditions here, whereas your original formula had it only around the second condition.
However, a shorter version of this would be as follows:
=AND( D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text") )
... since the result of a properly functioning AND( ) is always TRUE or FALSE anyway.
It looks like you're supplying 4 arguments to the IF statement:
=IF(DATECHECK,AND(TEXTCHECK),TRUE,FALSE)
The IF statement expects 3 arguments instead. 1) the condition, 2) the value if true, and 3) the value if false. You can combine your two conditions using an AND statement like this:
AND(DATECHECK,TEXTCHECK)
The final formula would then be:
=IF(AND(D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text")),TRUE,FALSE)

If function based on query function results

Can you wrap a query function with an if function?
My query can produce 3 results, yes yes, yes no, no no. These results occur in two different cells. I want the if function to then say - yes yes - true and either of the other results false. When doing it simply it just gives an error.
Wrapping of formulas
The IF formula has the syntax IF(logical_expression, value_if_true, value_if_false)
Thereby, the logical expression can be a comparison of values, e.g IF(value="Yes", TRUE, FALSE)
value can be in this case either
a fixed value, e.g. a string
a cell reference, e.g. IF(J2="Yes", TRUE, FALSE)
a nested formula, which can a query
In your case, you are interested in the latter, so:
=IF(query(Sheet2!A1:J7,"select G where A = '"&A2&"' and '"&E2&"' = B",0)="Yes", TRUE, FALSE)
I was able to get an answer with using the IFS function =ifs(query(Sheet2!A3:J9,"select G where A = '"&A4&"' and '"&E4&"' = B",0)="yes","TRUE",query(Sheet2!A3:J9,"select G where A = '"&A4&"' and '"&E4&"' = B",0)="no","FALSE")
That was able to solve the issue that the simple if function could not solve

List of mathematical operations in Prolog

I've created a list of a mathematic expression such as [1,+,2,-,3] and I'm trying to get this evaluated as 1+2-3 i.e 0. However, I'm having trouble implementing this.
Here is the code I've tried:
test(List, Atom) :-
atomic_list_concat(List, '', Val),
Atom is Val.
Any help would be much appreciated.
I think you should implement a parser which converts given list into syntax tree.
you should convert
[1,+,2,-,3]
into
-(+(1,2),3)
?- A is -(+(1,2),3).
A = 0.
Parser is often developed by using DCG.
You may consider about operators priority(* is stronger than +) and which direction the tree extends
I.E. you should decide
[a,+,b,+,c]
is converted into
+(a,+(b,c))
or
+(+(a,b),c)
I'm sorry for my bad English!
The problem with using atomic_list_concat is that the result is a single atom, not a term. So Prolog doesn't know how to "execute" it using is/2.
You can convert the atom to a term using term_to_atom/2:
eval_exp(List, Result) :-
atomic_list_concat(List, '', Atom), % This relates a list to an atom
term_to_atom(Term, Atom), % This relates an atom to term
Result is Term. % This evaluates a term

XSL - Does evaluating conditional expressions "shortcut"?

Given an XSL 'If' statement:
<xsl:if test="a = 'some value' and b = 'another value'">
If a does not equal 'some value', is the value of b still checked? (As if the first test is false, the only outcome of the and is false.) This is what languages like C# do - I was wondering if the same goes in XSL. Does it depend on the engine/parser?
Yes, it is called lazy-evaluation or short-circuiting, and xsl supports it. See: http://www.w3.org/TR/xpath#booleans
An and expression is evaluated by
evaluating each operand and converting
its value to a boolean as if by a call
to the boolean function. The result is
true if both values are true and false
otherwise. The right operand is not
evaluated if the left operand
evaluates to false.
Yes, this is depending on the implementation. But since XSLT is side-effect-free (as opposed to C# and other languages, where a function call chainging some state or even an assignment can be in the expression), this does not matter.