Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!mcvax!unido!pbinfo!corona!pgcomp From: pgcomp@pbinfo.UUCP (Projektgr. Uebersetzerbau) Newsgroups: comp.lang.pascal Subject: Re: C Ternary Conditional Expression? Summary: RE: C Ternary Conditional Expression Message-ID: <534@corona.pb> Date: 26 May 89 09:41:15 GMT References: <950025@hpclcdb.HP.COM> Organization: Uni-GH Paderborn, Germany Lines: 39 In article <950025@hpclcdb.HP.COM> cdb@hpclcdb (Carl Burch) writes: >Can anyone think of a way to express in Pascal a form equivalent to the >ternary conditional expression in C without using a separate statement? >I need to speed up an expression that usually matches a cached value, but >can't figure out how to move the code out of the called function >( represented by f(indx) below) to save the procedure call overhead. > >For instance, how could one translate : > > a[ (indx == last_indx) ? last_val : f(indx) ] = a [ y ]; > >without using an IF statement? Preferably without using a temporary variable. > >The only Pascal equivalent I've generated is : > > IF (indx = last_indx) > THEN temp := last_val > ELSE temp := f(indx); > a[temp] := a[y]; > >Please omit all flames - I just happen to know how to do this in C and nobody >I've asked has been able to design a Pascal equivalent. > Since BOOLEAN Values should be implemented as TYPE BOOLEAN = (FALSE,TRUE); simply try a[ord(indx=last_indx)*last_val+ord(indx<>last_indx)*f(indx)] := a[y]; or a[ord(indx=last_indx)*last_val+(1-ord(indx=last_indx))*f(indx)] := a[y]; whichever is faster. thomas roemke