Unsure about the combination of operators "&&" and "||"

Moin. Good day to y’all.

I have this conditional:

<<if $Var1 == true && $Var2 == true || $Var3 == true && $Var4 == true>>

How is it operated?

<<if $Var1 == true && ($Var2 == true || $Var3 == true) && $Var4 == true>>

Does it mean the conditions are met if all is fulfilled and if Var2 OR $Var3 is true?

Or is it like

<<if ($Var1 == true && $Var2 == true) || ($Var3 == true && $Var4 == true)>>

conditions are met if Var1 with Var2 are true OR Var3 with Var4 are true?
In other words: Var1 and Var2 are grouped as are Var3 with Var4?

I had checked before, but became unsure again.

If operators apply like Javascript, it’ll be the latter, as ‘and’ has higher precedence.

Also, a tip to improve readability, the comparison to true is implicit, so you can write the expressions like << if $var1 && $var2 >>

Hmm, question 1: Does Twine2 use the conditionals as JavaScript?

question 2:
if the AND operator has a higher precedence, wouldn’t it be like that:
$Var1 && Var4 && ($Var2 || $Var3)?
Var1 is grouped with Var4 and either Var2 or Var3.

And: thanks for the fast response.

I cannot validate question1 now (typing on phonr) but I think Sugarcube is likely to work as JS in this respect.

As for question2, order is always significant. Operator precedence has the effect of implied parentheses but it won’t move your operands. I auggest using parentheses in complex expressions, even if they are not required.

Ok … gotta check whether parentheses in conditionals work.

Thank you much.

SugarCube tries to act as a thin abstract layer over standard JavaScript, so in the case of Operator Precedence it follows the standard JavaScript rules.

note: SugarCube includes a number of keyword based equivalents (to, and, or, not, etc…) for some of the standard JavaScript operators (===, &&, ||, !, etc…). These keywords are automatically converted into their JavaScript equivalents before the expression gets evaluated. So the precedence of those keyword based operators is the same as the JavaScript ones they represent.

So if the conditional expression being evaluated was…

<<if $Var1 && $Var2 || $Var3 && $Var4>>

…the logical AND && has a ranking of 4 in the table linked to earlier, and the logical OR || has a ranking of 3, where the higher the ranking the earlier the operator is applied. This means that the && join will be evaluated before the || join is.
eg. the evaluation order would be…

$Var1 && $Var2      => Result1
$Var3 && $Var4      => Result2
Result1 || Result2  => Final Result

…or the equivalent of…

<<if ($Var1 && $Var2) || ($Var3 && $Var4)>>
2 Likes

Wow! Fabulous.

Thanks you both, and Greyelf for the elaboration/clarification. I wish you both a nice weekend.