[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multi conditional for loops



K Mankoff wrote:
> 
> Hi *,
>    I am interested in coding with a c/c++ style multi-conditional for
> loop.
> 
> c++ ex:
> for ( int i = 0; ( ( i < 10 ) && ( j <= 4 ) ); i++ )
> {
>     /* do something */
> }
> 
> Here is sample IDL code. It runs but produces very interesting
> results. (this is IDL5.3 on Linux RH6.2)
> 
> PRO test
> j = 0
> FOR i = 0, ((j LE 4) AND 10) DO BEGIN
>     j = j + 2
>     PRINT, i, j
> ENDFOR
> END
> 
> thanks for any info,
>    ken.

The value of a conditional is either 0 or 1. So, the loop will execute
either 1 or 2 times depending on the value of the conditional. 
(J < 4) => 1, 1 AND 10 => 0, so the terminating expression is 0 and
the loop should execute once (FOR i=0, 0 DO...). The first and second 
expressions are only evaluated before the loop begins, not during its 
execution, so even if the condition changes the value used as the limit 
will not.

What you need to use is a while loop.

-- 
-----------------------------------------------------------
Nigel Wade, System Administrator, Space Plasma Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK 
E-mail :    nmw@ion.le.ac.uk 
Phone :     +44 (0)116 2523568, Fax : +44 (0)116 2523555