Disclaimer

The content of this blog is my personal opinion only. Although I am an employee - currently of Nvidia, in the past of other companies such as Iagination Technologies, MIPS, Intellectual Ventures, Intel, AMD, Motorola, and Gould - I reveal this only so that the reader may account for any possible bias I may have towards my employer's products. The statements I make here in no way represent my employer's position, nor am I authorized to speak on behalf of my employer. In fact, this posting may not even represent my personal opinion, since occasionally I play devil's advocate.

See http://docs.google.com/View?id=dcxddbtr_23cg5thdfj for photo credits.

Monday, June 18, 2012

Else: what's the difference?

What's the difference

between cascaded IFs

   if( cond1 ) {
      ...
   } elsif( cond2 ) {
      ...
   } else {
       ...
   }

and a nested IF in the ELSE part:


   if( cond1 ) {
   } else {
        if( cond2 ) {
            ...
        } else {
            ...
        }
   }


?

Not much - except for intent.  The latter calls out the ELSE part, the IF cond2 part, emphasizing its separateness. The latter pivots easily to a nested IF in the THEN part. The cascaded IF probably needs to be transformed to the nested IF on the WELSE part, and then pivoted.

Note that the cascaded IF is often used to express a concurrent IF:


   if    cond1 => ...
   ::    cond2 => ...
   else ...
   fi

where the difference is meaningful: in the concurrent IF all of the condityions, cond1 and cond2, are evaluated.  Indeed, some argue that there should be no ELSE clause for a concurrent IF.

--

I wonder if there could be some useful difference made between cascaded IF and nested IF in the ELSE part.


No comments: