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

Re: maxlist in a for loop



PureBytes Links

Trading Reference Links

Romi:

>For something like the following:
>
>for value1=0 to 10 begin    {FYI, value1 is not related to bar numbers}
>value2=value0*value1;
>end;
>
>What would be the most efficient (or shortest) code to compute the max (or
>highest) value of value2 in the above for loop

For EXACTLY what you wrote above, with value0 a constant, the
maximum of value2 is proportional to the loop counter value1.  That
is, the maximum of valuew is its last value in the loop if value0 is
positive, or its first value in the loop if value0 is negative.

More generically (if your calculation of value2 is unpredictable in each
iteration) you would put a test within the loop itself:

maxvalue2 = -999999;
for k = 0 to 10 begin
   value2 = {some calculation};
   if value2 > maxvalue2 then maxvalue2 = value2;
end;

--
  ,|___    Alex Matulich -- alex@xxxxxxxxxxxxxx
 // +__>   Director of Research and Development
 //  \
 // __)    Unicorn Research Corporation -- http://unicorn.us.com