Ternary operation

From Wikipedia, the free encyclopedia

(Redirected from Ternary operator)
Jump to: navigation, search

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 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.

Programming languages in the Visual Basic family instead use a library function, IIf, with syntax iif(condition, op1, op2). This has significant disadvantages, because the subexpressions must all be evaluated, according to Visual Basic's evaluation strategy for function calls.

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


Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.