compound if alternatives

Robert Brenstein rjb at rz.uni-potsdam.de
Sun Jan 25 18:39:00 EST 2004


>Recently, "Robert Brenstein"  wrote:
>
>>  I need to repeatedly check within which of predefined time periods
>>  the current moment is. Since this affects overall performance (but is
>>  necessary), I am trying to see whether there is an alternative to a
>>  compound if to speed this up.
>>
>>  t1, t2, t3, ... , t7 -- time interval boundaries in seconds
>>  c - current time in seconds
>>
>>  if c < t1 then
>>   do1
>>  else if c >= t1 and c < t2 then
>>   do2
>>  else if c >= t2 and c < t3 then
>>   do3
>>  ...
>>  else if c >= t7 then
>>   do8
>>  end if
>>
>>  Anyone any ideas? I seem to have a "writer's block" :(
>
>You could try switch:
>
>  switch
>   case c < t1
>     do1
>   case c < t2
>     do2
>   case c < t3
>     do3
>   ...
>   case c < t8
>     do8
>  end switch
>
>Note there are no breaks between case statements so the script should run
>through all options until it finds a valid equation.
>
>Regards,
>
>Scott Rossi
>Creative Director
>Tactile Media, Multimedia & Design


Scott, but your switch statement is not equivalent to the compound if 
above. For example, if c < t1, the if's will execute do1 whereas your 
switch will do do1, do2, ... do8. That's wrong.

I tested timing of switch with double conditions versus compound if 
(in other words, just replacing if's with case's) and they clock to 
be the same on my G3/400.

Robert


More information about the metacard mailing list