Ternary conditinals v1.5

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Ternary conditinals v1.5

Post by jacky »

Just a little thing I thought I'd mention here, even though I'm sure many of you obviously already know this, and others don't care because you're not (that) into scripting, but maybe it could be useful to some :

You know how we can already use ternary to do basic IF conditions, e.g.

Code: Select all

msg (confirm("Are you sure?")) ? "You are!" : "You're not...";
But what if you wanted to test two expressions, to test if expr1 is true AND/OR expr2 is true ? Well, while such operators aren't (yet) supported, it's obviously easy to do anyways since XY returns 0 for false, and 1 for true.

To test if both conditions are true (AND), simply check the sum is 2, and to check at least one is (OR), that it is at least 1

Code: Select all

  // AND:
  msg (confirm("Are you sure?") + confirm("Are you really sure?") == 2) ? "So you are sure" : "You need to be 100% sure";
  // OR:
  msg (confirm("Are you sure?") + confirm("Are you really sure?") >= 1) ? "Good enough" : "Well then...";
Proud XYplorer Fanatic

admin
Site Admin
Posts: 66075
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Ternary conditinals v1.5

Post by admin »

Nice! (Just to take this out of the "unanswered posts"... :wink: )

PS: Of course, Boolean stuff will come to scripting later...

Post Reply