Question about combining multiple "and" "or" conditional statements in Sugarcube

Hi everyone, I tried looking through the official syntax guidelines for this, but I think they only address regular conditional statements or those using “and” only. When checking to see if multiple conditions are met, how would one deal with something like this?

<<if ($name is "Bob" and $intelligence gte 5) or ($name is "Phil")>>
You're a cool dude.
<</if>>

Are the parentheses acceptable here? Will the statement be read correctly–IE only fire if the first two statements combined are true OR if the third is true? And what about for even more complex combinations, such as here?

<<if ($name is "Bob" and $intelligence gte 5) or ($name is "Phil and $charisma gte 10)>>
Etc...
<</if>>

Any help for this neophyte is much appreciated, thank you! :slight_smile:

There is a specific order that the operators contained within a conditional expression will be evaluated in.

You can use Parenthesis (brackets) to change/control the order of evaluation, which you have done in both of your examples.

So in your first example you have used parenthesis to control the order so that:

  1. the current value of $name is compared to “Bob”
  2. the current value of $intelligence is compared to 5
  3. the true / false outcomes of 1. & 2. are joined together using the and operator
  4. the current value of $name is compared to “Phil”
  5. the true / false outcomes of 3. & 4. are joined together using the or operator
  6. the true / false outcome of 5. is compared to true
4 Likes

Thank you for taking the time to explain! :slight_smile: The operator precedence page that you linked will take some time for me to wrap my head around, but it’s good to know that parentheses/brackets can be used determine the order of evaluation! Am I correct in concluding that multiple “nesting” parentheses would be acceptable when needed, then?

Wherein:

<<if (($name is "Bob" and $intelligence gte 5) and ($lastname is "Beemis" and $charisma gte 10)) or ($name is "Bill")>><</if>>
  1. $name is Bob and $intelligence is greater than or equal to 5
  2. $lastname is Beemis and $charisma is greater than or equal to 5
  3. $name is Bill

–the sequence would execute if:

  1. the contents of 1 and 2 were true
    OR
  2. the contents of 3 alone were true?
1 Like

Yup! Nest them as deeply as you want. Although in practice, if you’re nesting them deeply it can get hard to read and you might want to assign some of the inner parts to a temporary variable, e.g. _smart_Bob or _charismatic_Beemis or something.

2 Likes

Thank you so much, that’s a great idea and wonderful to know! :slight_smile: