[Gambas-user] How to realize AND, OR etc.
Doriano Blengino
doriano.blengino at ...1909...
Thu Sep 2 11:36:24 CEST 2010
Rolf-Werner Eilert ha scritto:
> This is just a general question about programming, it doesn't refer to
> Gambas specifically, but I would implement the results in Gambas.
>
> For some of my bigger projects I have had to implement IF and ELSE and
> similar functions. I tried to manage AND, OR etc. too, but I failed.
> Somehow I didn't find a proper way of implementing this logic. So up to
> now I have filled this gap by simply putting several IF ELSE IF ELSE and
> so on within each other. Of course this is somewhat tricky sometimes :-)
>
> Could someone here explain me how an AND and an OR can be implemented?
> Are there general rules? I do not need the parser, it is there. I just
> ask about the logic behind AND and OR for instance.
>
> Here are my thoughts so far:
>
> - Parser puts IF-clauses into array and logic (AND/OR) in between or
> into separate list
>
> - IF-clauses are solved and results are stored e. g. in a boolean array
>
> - The ANDs and ORs from the list are processed with the boolean array
> into a final result
>
> This does not, however, include looking for bracketed clauses... Oh my
> goodness :-)
>
Generally speaking, compilers (or parsers) don't use arrays to store
intermediate results. ANDs and ORs are like any other arithmetic
operation - they are evaluated left to right, giving precedence when
due. But! Especially when speaking about boolean expressions, you can't
assume a specific order of evaluation. Some compiler can even arrange
code in such a manner that some computation is be omitted, when at a
certain point of the expression the result is already known. This is
called short-circuit, and Gambas does *not* use it. To clarify, if I write:
IF A > 3 AND B+5 > 2 THEN...
some runtime could evaluate "A > 3" and, if it founds it false,
completely avoid to compute "b+5" and "B+5 > 2". Gambas does never do
so. It evaluates "A > 3", obtaining a true/false value. Then it computes
"B+5", compares with 2, and obtains another true/false value. Then it
computes the AND between the two boolean values, and so it can decide to
execute the THEN part or not. Again, you should not rely on a specific
order of execution; the same as with any other arithmetic calculus: you
can only tell that "B+5" is evaluated before "B+5>3", because the
greater-than has a lower precedence. But then, you don't know if the
first comparison is computed before or after the second.
All this is important if, when evaluating expressions, some collateral
result is expected. If, instead evaluating simple variables, you use
properties or function calls and, if these properties or function calls
do something behind the scenes (some collateral effect), then the order
of evaluation can be important, but you should not rely on it. The
solution in this case is to use temporary boolean variables to force the
evaluation of some piece of code, and have a predictable evaluation
order. The line of code above could be written like:
btemp1 = A > 3
btemp2 = B+5 > 2
IF btemp1 and btemp2 THEN...
This version of code assures that A and B are always evaluated, and in
that given order.
That said, when there is more than one possibility, it's a question of
style. I personally don't like long sequences of ANDs and ORs, but
sometimes they are expressive:
IF age<18 or sex=female or state=married or religion<>catholic THEN
you_are_not_a_catholic_priest()
To make the long line above a little shorter, one could use multi-line
IFs, or one could deploy temporary variables, but in this case the line:
btemp1 = age<18 or religion<>catholic
what does mean? Do we have a name for persons which are "minors or not
catholic"? This code is not self-explanatory, and so it is ugly.
Hope this helps a little,
Regards
Doriano
More information about the User
mailing list