Re: DIFF formula in weather.c how it work?

From: Edward Almasy (almasy@axis.com)
Date: 02/08/96


Brian Jones writes:
>   int diff, change; 
>   if ((time_info.month >= 9) && (time_info.month <= 16))
>     diff = (weather_info.pressure > 985 ? -2 : 2); 
>   else
>     diff = (weather_info.pressure > 1015 ? -2 : 2); 
> 
>         My C still sucks :) I normaly was a pascal person.. but what does 
> this do?   the (weather_info.pressure > 985    i think may return a 0 if 
> the statement is true and a nonzero if its false.. but then what does the 
> ? -2 : 2)   do? This is the first time I've ever seen something like 
> that... some sort of math operator or comparison or something? can 
> someone please explain what that statement does?

The "?:" operator is called a trigraph, and it's just shorthand for
an IF/THEN/ELSE construct, so:

    diff = (weather_info.pressure > 1015 ? -2 : 2);

translates to:

    if (weather_info.pressure > 1015)
        diff = -2;
    else
        diff = 2;

Some development teams outlaw trigraphs because they can obscure the
meaning behind a bit of code, but I think they come in very handy in
some situations (like the above).


 \_\_\_     _/ \_\_\_  axis data: specializing in online system setup & design
 \_  \_     _/ \_  \_    Edward Almasy                       almasy@axis.com  
 \_\_\_     _/ \_\_\_    President: Axis Data       Proprietor: NineJackNine
     \_ _/  _/     \_    608-256-5732 (voice)            608-256-5697 (data)
 \_\_\_ _/_/_/ \_\_\_  9jack9: on the bleeding edges of culture and technology



This archive was generated by hypermail 2b30 : 12/07/00 PST