Using And / Or

Started by atlanta1213, January 30, 2014, 08:26:36 AM

Previous topic - Next topic

atlanta1213

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

Look in the Help file under "Operators".

|| - Logical Or
&& - Logical And

Jim

Deana

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) Select

;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

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