evaluation of 'or' statements in conditional

E. Joshua Rigler relgire at gmail.com
Tue May 20 10:14:01 CDT 2008


On Mon, May 19, 2008 at 2:57 PM, John W. Eaton <jwe at bevo.che.wisc.edu> 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.

-EJR


More information about the Help-octave mailing list