Thursday, February 24, 2011

Queen Size Bed In Cargo Van?

The ternary operator

In computing a ternary operator (sometimes incorrectly called tertiary operator) is an operator that takes three arguments. The arguments and the result can be of different types. The syntax would be.

result = condition? valor_si_cierto: value_if_false

This operator is present in many modern programming languages \u200b\u200band many other far less modern. The C language, and almost all languages \u200b\u200bhave derived from it has been inherited (C + +, C #, Java, PHP ...) While many other languages \u200b\u200bdo not have it, I know ... For example, all derivatives of Pascal (Delphi, Modula, Oberon ...). Not far from essential construction, but often come in handy.

An example classics by, the larger of two numbers, using code PHP:

With the binary operator> (major-minor) would be:
if ($ a> $ b)
$ max = $ a;

else $ max = $ b;
echo $ max; / / show the maximum

With the ternary operator:
$ max = ($ a> $ b)? $ A: $ b;
echo $ max;

or even:
echo ($ a> $ b)? $ A: $ b;

0 comments:

Post a Comment