Page 1 of 1

Ternary conditinals v1.5

Posted: 31 Oct 2008 00:57
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...";

Re: Ternary conditinals v1.5

Posted: 04 Dec 2008 14:36
by admin
Nice! (Just to take this out of the "unanswered posts"... :wink: )

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