[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: evaluation of 'or' statements in conditional
From: |
Dushan Mitrovich |
Subject: |
Re: evaluation of 'or' statements in conditional |
Date: |
Wed, 21 May 2008 11:10:51 -0700 |
User-agent: |
VSoup v1.2.9.48Beta [OS/2] |
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