HOME

TheInfoList



OR:

In certain
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
languages, the Elvis operator, often written ?:, is a
binary operator In mathematics, a binary operation or dyadic operation is a rule for combining two elements (called operands) to produce another element. More formally, a binary operation is an operation of arity two. More specifically, an internal binary ope ...
that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand. This is identical to a short-circuit ''or'' with "last value" semantics. The notation of the Elvis operator was inspired by the ternary
conditional operator The conditional operator is supported in many programming languages. This term usually refers to ?: as in C, C++, C#, and JavaScript. However, in Java, this term can also refer to && and , , . && and , , In some programming languages, e.g. Jav ...
, ? : since the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional A ? A : B. The name "Elvis operator" refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an
emoticon An emoticon (, , rarely , ), short for "emotion icon", also known simply as an emote, is a pictorial representation of a facial expression using characters—usually punctuation marks, numbers, and letters—to express a person's feelings, ...
of
Elvis Presley Elvis Aaron Presley (January 8, 1935 – August 16, 1977), or simply Elvis, was an American singer and actor. Dubbed the "Honorific nicknames in popular music, King of Rock and Roll", he is regarded as Cultural impact of Elvis Presley, one ...
with his signature hairstyle. A similar operator is the
null coalescing operator The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#, PowerShell as of version 7.0.0, Perl a ...
, where the boolean truth check is replaced with a check for non-
null Null may refer to: Science, technology, and mathematics Computing * Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value * Null character, the zero-valued ASCII character, also designated by , often use ...
instead. This is usually written ??, and can be seen in languages like C#.


Alternative syntaxes

In several languages, such as
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
,
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
,
Lua Lua or LUA may refer to: Science and technology * Lua (programming language) * Latvia University of Agriculture * Last universal ancestor, in evolution Ethnicity and language * Lua people, of Laos * Lawa people, of Thailand sometimes referred t ...
,
Object Pascal Object Pascal is an extension to the programming language Pascal (programming language), Pascal that provides object-oriented programming (OOP) features such as Class (computer programming), classes and Method (computer programming), methods. ...
,
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
,
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, and
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, the OR operator (typically , , or or) has the same behavior as the above: returning its first operand if it would evaluate to true in a boolean environment, and otherwise evaluating and returning its second operand. When the left hand side is true, the right hand side is not even evaluated; it is " short-circuited." This is different than the behavior in other languages such as C/C++, where the result of , , will always be a boolean.


Example


Boolean variant

In a language that supports the Elvis operator, something like this: : x = f() ?: g() will set x equal to the result of f() if that result is a true value, and to the result of g() otherwise. It is equivalent to this example, using the conditional ternary operator: : x = f() ? f() : g() except that it does not evaluate the f() twice if it is true.


Object reference variant

This code will result in a reference to an object that is guaranteed to not be null. Function f() returns an object reference instead of a boolean, and may return null: : x = f() ?: "default value"


Languages supporting the Elvis operator

* In GNU C and
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
(that is: in C and C++ with GCC extensions), the second operand of the ternary operator is optional. This has been the case since at least GCC 2.95.3 (March 2001), and seems to be ''the'' original elvis operator. * In
Apache Groovy Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming lan ...
, the "Elvis operator" ?: is documented as a distinct operator; this feature was added in Groovy 1.5 (December 2007). Groovy, unlike GNU C and PHP, does ''not'' simply allow the second operand of ternary ?: to be omitted; rather, binary ?: must be written as a single operator, with no whitespace in between. * In
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
, it is possible to leave out the middle part of the ternary operator since PHP 5.3. (June 2009). * The
Fantom FANTOM (Functional Annotation of the Mouse/Mammalian Genome) is an international research consortium first established in 2000 as part of the RIKEN research institute in Japan. The original meeting gathered international scientists from diverse bac ...
programming language has the ?: binary operator that compares its first operand with null. * In Kotlin, the Elvis operator returns its left-hand side if it is not null, and its right-hand side otherwise. A common pattern is to use it with return, like this: val foo = bar() ?: return * In
Gosu Gosu (고수) is a Korean term used to refer to a highly skilled person. In computer gaming the term is usually used to refer to a person who dominated games like '' StarCraft'', ''Counter-Strike'', Tekken, ''Warcraft III'', ''Diablo II'', ''Do ...
, the ?: operator returns the right operand if the left is null as well. * In C#, the null-conditional operator, ?. is referred to as the "Elvis operator", but it does not perform the same function. Instead, the
null-coalescing operator The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic Conditional (programming), conditional expression in several programming languages, including C Sharp (program ...
?? does. * In ColdFusion and
CFML ColdFusion Markup Language, more commonly known as CFML, is a scripting language for web development that runs on the JVM, the .NET framework, and Google App Engine. Multiple commercial and open source implementations of CFML engines are availab ...
, the Elvis operator was introduced using the ?: syntax. * The
Xtend Xtend is a general-purpose high-level programming language for the Java Virtual Machine. Syntactically and semantically Xtend has its roots in the Java programming language but focuses on a more concise syntax and some additional functionality ...
programming language has an Elvis operator. * In Google's Closure Templates, the Elvis operator is a
null coalescing operator The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#, PowerShell as of version 7.0.0, Perl a ...
, equivalent to isNonnull($a) ? $a : $b. * In
Ballerina A ballet dancer ( it, ballerina fem.; ''ballerino'' masc.) is a person who practices the art of classical ballet. Both females and males can practice ballet; however, dancers have a strict hierarchy and strict gender roles. They rely on ye ...
, the Elvis operator L ?: R returns the value of L if it's not nil. Otherwise, return the value of R.


See also

* ?: or
conditional operator The conditional operator is supported in many programming languages. This term usually refers to ?: as in C, C++, C#, and JavaScript. However, in Java, this term can also refer to && and , , . && and , , In some programming languages, e.g. Jav ...
, when used as a
ternary operator 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''. In computer science, a ternary operator i ...
*
Safe navigation operator In object-oriented programming, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional operator, null-propagation operator) is a binary operator that returns null if its first argument is null; ...
, often ?. *
Spaceship operator In computer science, a three-way comparison takes two values A and B belonging to a type with a total order and determines whether A < B, A = B, or A > B in a single operation, in accordance with the mathematical law of trichotomy. Machine ...
<=> *
Option type In programming languages (especially functional programming languages) and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional value; e.g., it is used as the return type of functions whic ...


References

{{reflist Articles with example code Binary operations Conditional constructs Operators (programming)