Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I just compiled your code on a VAX (VMS) both with and without optimization enabled. In each case, both routines generated identical code.

To me, the ternary operator is most useful when used in a function call argument list or other complex expression where one element needs to change based on some condition. These become much easier and concise to express with a ternary operator than with two separate statements within an if/else construct.

Contrived example:

printf("File name: %s\n", (ptr == NULL) ? "<unspecified>" : ptr);


char *s; if (ptr == NULL) s = "<unspecified>"; else s = ptr; printf("File name: %s\n", s);

What it boils down to is that the ternary operator presents an expression and can be used anywhere an expression can be used. if/else is a (compound) statement and is much more restrictive about where it can be used.

# trinary movl %eax, -28(%ebp) # to_return # ifelse movl $1, -28(%ebp) # to_return
The trinary function is handling the variable between registers, whereas ifelse is using a memory location. This is another boost to the trinary operator.

Doesn't the $1 notation mean that the value 1 is encoded in the instruction itself (immediate value)? You seem to imply that it is referencing a variable somewhere else in memory.

90% of every Perl application is already written.

In reply to Re: Trinary Operator Semantics by pfaut
in thread Trinary Operator Semantics by hardburn

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (5)
    As of 2021-01-24 13:04 GMT
    Find Nodes?
      Voting Booth?