[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: evaluation of 'or' statements in conditional
From: |
E. Joshua Rigler |
Subject: |
Re: evaluation of 'or' statements in conditional |
Date: |
Wed, 21 May 2008 12:20:25 -0600 |
On Wed, May 21, 2008 at 12:10 PM, Dushan Mitrovich <address@hidden> wrote:
> On 21 May 2008, "E. Joshua Rigler" <address@hidden> wrote:
>>On Mon, May 19, 2008 at 2:57 PM, John W. Eaton <address@hidden> wrote:
>>> On 19-May-2008, E. Joshua Rigler wrote:
>>>
>>> | I have a non-functional 'do until' loop that, when simplified, looks like
>>> this:
>>> |
>>> | do
>>> | do_stuff();
>>> | i = increment_array_index(i);
>>> | until i==0 || i==(max_i+1) || array(i) == some_value
>>> |
>>> | The idea is to check if the bounds of the array have been exceeded,
>>> | then check to see if the array element meets a pre-defined condition
>>> | that breaks the loop. I guess I expected the condition to return true
>>> | as soon as the first item returned true, but it looks like Octave
>>> | actually evaluates every single expression, which results in an
>>> | attempt to address the array with an out of bounds index.
>>> |
>>> | My question is, is there a preferred way to do this that does not
>>> | involve placing a conditional break inside the loop when the array
>>> | bounds are exceeded?
>>>
>>> The || and && should short circuit. If not, then it is a bug, but I'm
>>> unable to reproduce this problem with the example I made up:
>>>
>>> x = [1,2,3,4,5]; i = 2; do i, --i; until (i == 0 || x(i) == 10);
>>
>> Not surprisingly, your example worked for me too. After looking more
>> closely at my actual code, I realized that my index "i" should have
>> been checked using less-than or greater-than operators because my
>> algorithm made it possible (even likely) that my index would start out
>> out-of-bounds. Stupid mistake on my part; sorry for the distraction.
>
> I noticed one difference between Rigler's and Eaton's 'until' lines. The
> condition list is enclosed in parentheses in the latter, but not in the
> former. Does this make any difference?
>
> - Dushan
To the best of my knowledge, no, since jwe encloses the entire
expression in parenthesis (but I did check, just to be certain). As I
noted previously, Octave works as expected with regard to short
circuiting || and && operators. This was definitely a case of
operator (me) error.
-EJR