viewpoint-particle

Author Topic: Using And / Or  (Read 3023 times)

atlanta1213

  • Newbie
  • *
  • Posts: 12
Using And / Or
« on: January 30, 2014, 08:26:36 am »
Cannot for the life of me find anything about using AND / OR in an IF THEN statement when you want to conditions to be evaluated at the same time. I did find an example in help where this symbol was used || but still not clear how that gets evaluated.

Thanks in advance,
Paul

JTaylor

  • Pundit
  • *****
  • Posts: 1363
    • Data & Stuff Inc.
Re: Using And / Or
« Reply #1 on: January 30, 2014, 08:38:55 am »
Look in the Help file under "Operators".

|| - Logical Or
&& - Logical And

Jim

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Using And / Or
« Reply #2 on: January 30, 2014, 09:12:06 am »
The logical And & OR operators are documented under the topic "Operators" in the Windows Interface Language  help file.  See the "Conditional Statements" topic for some examples of conditional code. Also all of the conditional statements have there own topic in the Windows Interface Language help file.

Here is a code sample to help get you started.
Code: Winbatch
;Check if a number is between 1 and 10
x = 5
If x>0 && x<=10
  Pause( 'Result', x : ' is between 1 and 10' )
Else
  Pause( 'Result', x : ' is NOT between 1 and 10' )
EndIf
 

Deana F.
Technical Support
Wilson WindowWare Inc.

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 317
Re: Using And / Or
« Reply #3 on: January 30, 2014, 09:25:59 am »
Do be aware, though, that although they look the same, the && and || operators in WinBatch do not behave like their C/C++ (and most/all other "C-like" languages) counterparts.

That is, they do not implement the "short circuit evaluation", which is central to how these operators work in those other languages.  That is, if you write:

IF someFunction(...) && someOtherFunction(...) THEN ...

both function calls (someFunction() and someOtherFunction()) will be evaluated, regardless of whether or not someFunction() returns true or false.

In most/all other C-like languages, if someFunction() returns false, someOtherFunction() will not be evaluated.

Now, many people will argue that this doesn't matter and that this whole post is a meaningless nitpick, but the fact is that this "short circuit" method is central to how these operators work and is widely used in both C-like and LISP-derived languages.  Once you get used to using it (say, in LISP), you will not see it as meaningless.

To clarify this last point further, LISP has functions AND(...) and OR(...) which mean, literally, evaluate expressions until one returns NIL (false), and evaluate expressions until one returns non-NIL (true), respectively.  Very powerful in the context of LISP.

I have implemented a UDS that does short-circuit evaluation in WinBatch.  I use it frequently when I need this behavior.

atlanta1213

  • Newbie
  • *
  • Posts: 12
Re: Using And / Or
« Reply #4 on: January 30, 2014, 09:31:04 am »
Thank you