maciep
(Korg Regular)
2002-12-30 06:12 PM
Evaluation of conditional expressions

I came across this during the golf tournament. It would be nice if a statement like this

code:
     if $a and $b
.
.
endif

would result in false if $a is false, without evaluating $b. Because false AND anything is false.

For example, the following code errors out with "array reference out of bounds!" Because on the last iteration $c = -1

code:
$a = 1,2,3
$c=2
while $c >= 0 and $a[$c] <5
$c=$c-1
loop

I don't know if this possible or if anyone else would like to see it. But I thought I'd throw it out there.


Howard Bullock
(KiX Supporter)
2002-12-30 07:21 PM
Re: Evaluation of conditional expressions

I agree short circuiting conditionals would be beneficial. Once the condition is determined to be either TRUE or FALSE then further processing of the line is not needed.

kholm
(Korg Regular)
2002-12-30 09:31 PM
Re: Evaluation of conditional expressions

I second this,

It might improve the speed of KiX even more, and secondly make the valid code smaller.


Will Hetrick
(Hey THIS is FUN)
2003-01-01 12:26 AM
Re: Evaluation of conditional expressions

Hey guys,
for some reason my brain is saying that would not be a good idea. It seems like you are assuming that because there is an and in the statement that we should automatically break there and stop.

What about this
While ($C<>5 and $D <> 7) or ($A <>3 and $b <>4)
...
loop

By you definition if $c <> 5 then the script would stop.

I can't see this as a good thing. There is always an exception to the error.

I say let it check the whole expression.

[ 31. December 2002, 12:27: Message edited by: Will Hetrick ]


LonkeroAdministrator
(KiX Master Guru)
2002-12-31 01:25 PM
Re: Evaluation of conditional expressions

will, I think the suggestion would change it to:
While ($C<>5 and $D <> 7) or ($A <>3 and $b <>4)

if $C<>5 is true check $D<>7 for true, else:
While $A <>3 and $b <>4

if $A<>3 is true check $b<>4 for true, else exit loop.

so, having the brackets actually makes it whole lot of different.
and if the first and (inside the first brackets) returns true, don't check the last part after the or as it's already sure what will happen.

anyway, this is nice thought but I'm not sure it's worth it.
shouldn't be too easy to code...


BrianTX
(Korg Regular)
2003-01-02 05:30 PM
Re: Evaluation of conditional expressions

I'll add my support to this idea! It would simplify some coding.

In an "AND" operation, if the first condition is false, there is no need to evaluate the second condition. The result will always be false.

In an "OR" operation, if the first condition is true, there is no need to evaluate the second condition. The result will always be true. Evaluating only the first condition unless the second is necessary would definitely speed up sorting algorithms, AND make the coding simpler.

Brian