Page 1 of 1 1
Topic Options
#81460 - 2002-12-30 06:12 PM Evaluation of conditional expressions
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
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.
_________________________
Eric

Top
#81461 - 2002-12-30 07:21 PM Re: Evaluation of conditional expressions
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#81462 - 2002-12-30 09:31 PM Re: Evaluation of conditional expressions
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
I second this,

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

Top
#81463 - 2003-01-01 12:26 AM Re: Evaluation of conditional expressions
Will Hetrick Offline
Hey THIS is FUN

Registered: 2001-10-02
Posts: 320
Loc: Harrisburg, PA USA
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 ]
_________________________
You have at least 2 choices. Each choice changes your destiny. Choose wisely!

Top
#81464 - 2002-12-31 01:25 PM Re: Evaluation of conditional expressions
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
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...
_________________________
!

download KiXnet

Top
#81465 - 2003-01-02 05:30 PM Re: Evaluation of conditional expressions
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
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

Top
Page 1 of 1 1


Moderator:  Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 289 anonymous users online.
Newest Members
Kelly001, MichaelK72, Janneman, standardbruker, maikoenig2001
17773 Registered Users

Generated in 0.031 seconds in which 0.011 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org