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.
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)>>
Any help for this neophyte is much appreciated, thank you!
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:
- the current value of $name is compared to “Bob”
- the current value of $intelligence is compared to 5
- the true / false outcomes of 1. & 2. are joined together using the and operator
- the current value of $name is compared to “Phil”
- the true / false outcomes of 3. & 4. are joined together using the or operator
- the true / false outcome of 5. is compared to true
Thank you for taking the time to explain! 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?
<<if (($name is "Bob" and $intelligence gte 5) and ($lastname is "Beemis" and $charisma gte 10)) or ($name is "Bill")>><</if>>
- $name is Bob and $intelligence is greater than or equal to 5
- $lastname is Beemis and $charisma is greater than or equal to 5
- $name is Bill
–the sequence would execute if:
- the contents of 1 and 2 were true
- the contents of 3 alone were true?
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.
_charismatic_Beemis or something.
Thank you so much, that’s a great idea and wonderful to know!