[Gambas-user] How to realize AND, OR etc.

Rolf-Werner Eilert eilert-sprachen at ...221...
Thu Sep 2 12:40:38 CEST 2010


Am 02.09.2010 11:36, schrieb Doriano Blengino:
> 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 :-)
>>

Thanks for your comprehensive answer! Here are my thoughts:

> 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.

Ok... But what about brackets? When the user of my program wants to make 
sure the expressions ARE evaluated in a special order? How does this fit 
in here?

> 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.

And I would be glad if I managed my own program to be half as clever as 
Gambas is :-)


> 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.

Yep - and how does it organize this? Doesn't it have to look into the 
whole line prior to seeing that there is a "> 2" following in order to 
decide to compute "B+5" first? And doesn't it have to evaluate "A > 3" 
and "B + 5 > 2" prior to computing the AND? So everything has to be put 
apart into certain pieces, then evaluated, computed and so on to get a 
final result.

If there's no array way of doing it - how is it done then?

If I go letter-by-letter through the formula text the user of my program 
is giving me, I might at every moment stumble over new surprises :-)

> 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.

As far as I can see, I wouldn't risk collateral results in my programs.

> 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 would make up for a nice array, wouldn't it? :-)

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

Ok. Let's use my printing forms as an example. This is where my need for 
this kind of logic is most urgent.

If I want to have a certificate printed for the number of language 
courses a student is taking, I have to decide how to use the free space 
for the marks. So if it's only 2 languages, there is more space and I 
can keep the places for the marks more apart than if it's 4 languages.

So the line of code in the printing form could read somewhat like (I 
write pseudo code, that's easier here)

IF Instr "courses" = "Spanish" AND Instr "courses" = "Russian" THEN
  print special 4 languages version
ELSE IF Instr "courses" = "Spanish" OR Instr "courses" = "Russian" THEN
  print 3 languages version
ELSE
  print 2 languages version

Of course it is possible to do this without any ANDs and ORs, but it 
would make things easier to read. It is really complicated to read now.

Another case which came about yesterday and reminded me to this whole thing:

For example, if a student has booked a certain main course which is more 
expensive than the ordinary one but then books in for another special 
language course, there will be a discount on the main course.

If I have to indicate on a sheet how much the students pay, I have to 
decide:

IF expensive main course THEN
   IF plus special course THEN
     x Euros for main course
     z Euros for special course
   ELSE
     y Euros for main course only
   END IF
ELSE
   x Euros for main course
   IF special course booked THEN
     z Euros for special course
   END IF
END IF

If there were ANDs and ORs it would make life somewhat easier ;-)

Furthermore, there are other places in the program where a logical 
sequence with ANDs and ORs could help, and if I managed to program the 
whole thing in a way to be able to use it everywhere, this would open 
more ways of organisation.

There is a filter function for instance to build lists of students in 
different classes and courses. It is very primitive now, it can only 
look for Instr in a single field, then put the name into the list or 
not. If I could use genuine logic, many things would be easier.

Hope I could make this clearer. Your answer already helped, but if using 
arrays is unusual, which way is better then?

Regards

Rolf





More information about the User mailing list