Share on Facebook Share on Twitter Email
Answers.com

Ternary operation

 
Wikipedia: 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 incorrectly called a tertiary operator)[1] 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".

Most of the languages emphasizing functional programming don't need such an operator as their regular conditional expression(s) is an expression in the first place e.g. the Scheme expression (if (> a b) a b) is equivalent in semantics to the C expression (a > b) ? a : b.

This is also the case in many imperative languages, starting with ALGOL where it would be possible to write a := if x > 0 then x else -x, but also Smalltalk (a := (x > 0) ifTrue: [ x ] ifFalse: [ -x ]) or Ruby (a = if true: x else -x end)

Contents

Examples

PHP

A traditional if-else construct would be written like so:

if ($cake == "fresh") 
{
    print "Yum yum! This cake is tasty.";
} else 
{
    print "Yuck! This cake tastes awful!";
}

This can be rewritten as the following ternary statement:

  print ($cake == "fresh") ? "Yum yum! This cake is tasty." : "Yuck! This cake tastes awful!";

The statement takes the form of:

 $variable = condition ? if true : if false

As of version 5.3, PHP supports a ternary shortcut syntax. In this form, the variable is unchanged unless the condition evaluates to false:

 $variable = condition ?: if false

Python

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. Before 2.5, one could use the syntax

(lambda x:op2, lambda x:op1)[condition]()

as a workaround which also takes care that only expressions are evaluated which are actually needed in order to prevent side effects.

A less reliable but simpler to read alternative was to abuse the and and or operator and write

condition and op1 or op2

but this code would break if op1 could be a "falsy" value (None, False, 0, an empty sequence or collection, …) as the expression would return op2 (whether it was truthy or falsy) instead of the (falsy) op1

Perl

A traditional if-else construct in Perl would be written like so:

if (1 == 1) {
    $i = "True";
} else {
    $i = "False";
}
print $i;

Rewritten to the ternary operator, it would look like so:

$i = (1 == 1) ? "True":"False";
print $i;

Java

An if-else construct would be written like so:

boolean variable = true;
if(variable)
    System.out.println("Yes");
else
    System.out.println("No");

This can be rewritten as a ternary statement

boolean variable = true;
System.out.println(variable ? "Yes" : "No");

C#

An if-else construct would be written as follows:

Boolean variable = true;
if(variable)
{
  Console.WriteLine("Yes");
}
else
{
  Console.WriteLine("No");
}

It can be rewritten as a ternary statement:

Boolean variable = true;
Console.WriteLine(variable ? "Yes":"No"):

JavaScript

An if-else construct would be written like so:

var num = 5;
 
if(num == 5)
{
 document.write("Number was 5");
} else 
{
 document.write("Number was " + num);
}

This can be rewritten as a ternary statement:

var num = 5;
document.write(num == 5 ? "Number was 5" : "Number was " + num);

Visual Basic

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.

See also

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

References

  1. ^ Ternary operation economicexpert.com

External links


Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Ternary operation" Read more