[Gambas-user] IF and AND - what is more efficient?

Benoit Minisini gambas at ...1...
Thu Jun 7 11:22:40 CEST 2007


On jeudi 07 juin 2007, Rolf-Werner Eilert wrote:
> Good morning everyone,
>
> as I don't know how the Gambas interpreter handles AND clauses in IF
> conditions, this is where I just was contemplating about:
>
> Let's assume I've got three different strings to compare which are in a
> table-like structure. Only when all the three strings are right, the app
> is to do something with it. The priorities are that there are a lot of
> entries with string1, fewer ones with string2 and the least with
> string3, so it's advisable to sort out string1 first, then string2 and
> string3 as the last one. The table is rather long, and I want to save
> time and processor load :-)
>
> Sure it would be ok to write
>
> IF string1 = "xyz" AND string2 = "abc" AND string3 = "www" THEN
>
> but would the interpreter try to check all the three before deciding to
> skip the block? Might it be more advisable to check it like
>
> IF string1 = "xyz" THEN
> 	IF string2 = "abc" THEN
> 		IF string3 = "www" THEN
>
> so it wouldn't jump to the next condition at all? Or is THIS the less
> efficient version because of something I don't know?
>
> Regards
>
> Rolf
>

AND always evaluates all its arguments, so if you do

IF test1 AND test2 AND test3 THEN

...the three test expressions will be evaluated.

If you want test2 to be evaluated only if test1 is TRUE, and so on, you must 
use the following syntax:

IF test1 AND IF test2 AND IF test3 THEN

Note that this syntax is only available in gambas 1.9.x.

Regards,

-- 
Benoit Minisini




More information about the User mailing list