# Ternary operation

﻿
Ternary operation

In mathematics, a ternary operation is an n-ary operation with n = 3. A ternary operation on a set A takes any given three elements of A and combines them to form a single element of A. An example of a ternary operation is the product in a heap.

In computer science a ternary operator (sometimes called a "tertiary operator") [ [http://www.economicexpert.com/a/Ternary:operation.html Ternary operation] "economicexpert.com"] is an operator that takes three arguments. The arguments and result can be of different types.

Many programming languages that use C-like syntax feature a ternary operator, `?:`, which defines a conditional expression. Since this operator is often the only existing ternary operator in the language, it is sometimes simply referred to as "the" ternary operator".

The ternary conditional operator was anticipated by ALGOL, which allowed if then else to be used in expressions, for example:

"a" := if "x" > 0 then "x" else -"x".

Languages that emphasize functional programming, such as Scheme, may define the plain if-then-else construction in terms of this ternary operator.For example, the Scheme expression `(if (> a b) a b)` is equivalent in semantics to the C expression `(a > b) ? a : b`.

Though it had been delayed for several years by disagreements over syntax, a ternary operator for Python was approved as Python Enhancement Proposal 308 and was added to the 2.5 release in September 2006. Python's ternary operator differs from the common `?:` operator in the order of its operands; the general form is `op1 if condition else op2`. This form invites considering `op1` as the normal value and `op2` as an exceptional case.

Version 9 of Visual Basic has added a ternary operator, `If()`, in addition to the existing `IIf()` function that had existed previously. As a true operator, it does not have the side effects and potential inefficiencies of the `IIf()` function.

The syntaxes of the tokens are similar - `If(condition [, op1] , op2)` vs `IIf(condition, op1, op2)`. As mentioned above, the function call has significant disadvantages, because the subexpressions must all be evaluated, according to Visual Basic's evaluation strategy for function calls and the result will always be of type variant (VB) or object (VB.NET). The `If()`operator however does not suffer from these problems as it supports conditional evaluation and determines the type of the expression based on the types of its operands.

* ?:, the ternary conditional expression
* IIf, inline if function

* [http://msdn2.microsoft.com/en-us/library/ty67wk28(VS.80).aspx Description of Ternary Operator in C#]
* [http://msdn.microsoft.com/en-us/library/bb513985.aspx Description of If operator in Visual Basic]
* [http://www.python.org/peps/pep-0308.html Description of Ternary Conditional Expression in Python (PEP 308)]
* [http://www.diveintopython.org/power_of_introspection/and_or.html Alternative workaround for ternary conditional expression]
* [http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25 Description in the Java Language Specification]
* [http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary Description in the PHP Language Documentation]

Wikimedia Foundation. 2010.

### Look at other dictionaries:

• ternary operation — an operation in a mathematical system by which three elements are combined to yield a single result. Cf. binary operation, unary operation. * * * …   Universalium

• ternary operation — an operation in a mathematical system by which three elements are combined to yield a single result. Cf. binary operation, unary operation …   Useful english dictionary

• Ternary — (from Latin ternarius ) can mean:* Ternary complex, a complex formed by the interaction of three molecules * Ternary compound, a type of chemical compound * Ternary computer, a computer using a ternary numeral system * Ternary form, a form used… …   Wikipedia

• Ternary logic — A ternary, three valued or trivalent logic (sometimes abbreviated 3VL) is a term to describe any of several multi valued logic systems in which there are three truth values indicating true , false and some third value. This is contrasted with the …   Wikipedia

• Binary operation — Not to be confused with Bitwise operation. In mathematics, a binary operation is a calculation involving two operands, in other words, an operation whose arity is two. Examples include the familiar arithmetic operations of addition, subtraction,… …   Wikipedia

• Unary operation — In mathematics, a unary operation is an operation with only one operand, i.e. a single input. Specifically, it is a function where A is a set. In this case f is called a unary operation on A. Common notations are prefix notation (e.g. +, −, not) …   Wikipedia

• unary operation — noun an operation with exactly one operand • Syn: ↑monadic operation • Hypernyms: ↑operation * * * an operation in a mathematical system by which one element is used to yield a single result, as squaring or taking the square root. Cf. binary… …   Useful english dictionary

• binary operation — a mathematical operation in which two elements are combined to yield a single result: Addition and multiplication are binary operations on the set of real numbers. Cf. ternary operation, unary operation. [1930 35] * * * …   Universalium

• unary operation — an operation in a mathematical system by which one element is used to yield a single result, as squaring or taking the square root. Cf. binary operation, ternary operation. [1930 35] * * * …   Universalium

• Boolean algebras canonically defined — Boolean algebras have been formally defined variously as a kind of lattice and as a kind of ring. This article presents them more neutrally but equally formally as simply the models of the equational theory of two values, and observes the… …   Wikipedia