JRS: In article <J7KOf.27331$***@newsfe2-win.ntli.net>, dated
Sun, 5 Mar 2006 23:04:09 remote, seen in news:comp.lang.pascal.delphi.mi
Post by DunnyPost by JamiePost by ScottDoes Delphi have an operator similar to C's "?: ternary" operator?
Example: i := (n > 0) ? sum div n : 0
if n > 0 then average := sum div n
else average := 0;
the shortest i can break it down to is this.
if Boolean(n) then i := sum div n else i := 0;
Ooh!
I wonder if Delphi is as flexible as Sinclair BASIC...?
i := Integer(Boolean(n)) * (sum div n);
That will do as it should, which is not what was actually wanted.
Boolean() is a typecast to a byte-sized value, but it does not change
the bits of the byte. Integer will then expand the byte to dword size.
The combination is essentially a 'mod 255' operation, or 'and $FF'. The
line will round sum down to be a multiple of n (I'm assuming everything
is positive and in-range for its storage size).
Such a ternary operator would be useful; it amounts to the conditional
arithmetic expression of Algol, though it should not be limited to
arithmetic.
But in Delphi style it should be written as
Answer := if condition then this else that ;
where both this and that are expressions assignable to answer.
I don't see why there should be any great difficulty in compiling it,
with 'if condition then this else that' usable wherever an expression is
allowed', those who don't like it don't have to use it.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines