|
?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages including C, Objective-C, C++, C#, D, Java, JavaScript, Linoleum, Perl, PHP, Tcl, and Ruby. In mathematics, a ternary operation is any operation of arity three, that is, that takes three arguments. ...
A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ...
C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...
Objective-C, often referred to as ObjC or more seldomly as Objective C or Obj-C, is an object oriented programming language implemented as an extension to C. It is used primarily on Mac OS X and GNUstep, two environments based on the OpenStep standard, and is the primary language...
C++ (pronounced see plus plus, IPA: ) is a general-purpose, high-level programming language with low-level facilities. ...
Also, due to technical limitations, C# redirects here. ...
D is an object-oriented, imperative system programming language designed by Walter Bright of Digital Mars as a re-engineering of C/C++. He has done this by re-designing many C++ features, and borrowing ideas from other programming languages. ...
Java is an object-oriented programming language developed by Sun Microsystems in the early 1990s. ...
JavaScript is the name of Netscape Communications Corporations and now the Mozilla Foundations implementation of the ECMAScript standard, a scripting language based on the concept of prototype-based programming. ...
A diagram is needed here to show a scaled-down version of the L.In. ...
Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...
PHP (PHP: Hypertext Preprocessor) is a reflective programming language originally designed for producing dynamic Web pages. ...
Tcl (originally from Tool Command Language, but nonetheless conventionally rendered as Tcl rather than TCL; and pronounced like tickle) is a scripting language created by John Ousterhout. ...
Ruby is a reflective, object-oriented programming language. ...
Conditional assignment
?: is most frequently used in conditional assignment statements. When used this way, it is used in an expression as follows: Expression may refer to: (in the vernacular) the act or particular way of expressing something (including an emotion through a facial expression or configuration) (in mathematics) a mathematical expression (in computing) a programming language expression (in computing) a vector graphics software Microsoft Expression (in genetics) the effect produced by a...
condition ? value if true : value if false The condition is evaluated true or false as a Boolean expression. On the basis of the evaluation of the Boolean condition, the entire expression returns value if true if condition is true, but value if false otherwise. This is similar to the way conditional expressions (if-then-else constructs) work in functional programming languages. A Boolean expression is an expression that results in a Boolean value, that is, TRUE or FALSE. For example, the value for 5 > 3 is TRUE, the value for An apple is not a fruit is FALSE. Boolean expressions are used also in document retrieval. ...
Functional programming is a programming paradigm that conceives computation as the evaluation of mathematical functions and avoids state and mutable data. ...
This ternary operator's most common usage is to minimize the amount of code used for a simple conditional assignment statement. (In this case, the expression is converted into a statement by appending a semicolon ; to the expression.) For example, if we wish to implement some C code to change a shop's opening hours to 12 o'clock in weekends, and 9 o'clock on weekdays, we may use int opening_time = (day == WEEKEND) ? 12 : 9; instead of the more verbose int opening_time; if (day == WEEKEND) opening_time = 12; else opening_time = 9; The two forms are equivalent. Note that neither value if true nor value if false expressions can be omitted from the ternary operator without an error report upon parsing. In C++ using of this operator may be even required, since this language explicitly distinguishes between initialization and assignment. For example, if you want to pass conditionally different values as an argument for a constructor of a field or a base class, it is not possible to use plain if-else statement; in this case only the conditional assignment expression can be used. Mind also that some types allow initialization, but do not allow assignment, or even the assignment operator does totally different things than the constructor. The latter is true for reference types, for example: C++ (pronounced see plus plus, IPA: ) is a general-purpose, high-level programming language with low-level facilities. ...
In computing, booting is a bootstrapping process that starts operating systems when the user turns on a computer system. ...
In computer programming, an assignment is the defining of a variable. ...
#include <iostream> #include <fstream> using namespace std; // int main( int argc, char** argv ) { const char* name = 0; ofstream fout; if ( argc > 1 ) { name = argv[1]; fout.open( name, ios::out | ios::app ); } // ostream& sout = name ? fout : cout; } In this case there's no possibility to replace the use of ?: operator with if-else statement. Some corporate programming guidelines list the use of the ternary operator as bad practice because it can harm readability and long-term maintainability. Also, an if statement can be debugged more easily than its ternary counterpart. However, ternary operators are widely used and can be useful in certain circumstances to avoid the use of an if statement, either because the extra verbiage would be too lengthy or because the syntactic context does not permit a statement. For example: #define MAX(a,b) (((a)>(b))? (a): (b)) or The C preprocessor (cpp) is the preprocessor for the C programming language. ...
for (i = 0; i < MAX_PATTERNS; i++) c_patterns[i].ShowWindow(m_data.fOn[i] ? SW_SHOW : SW_HIDE); (The latter example uses the Microsoft Foundation Classes Framework for Win32.) Microsoft Foundation Classes, or MFC, is a Microsoft library that wraps portions of the Windows API in C++ classes, forming an application framework. ...
Windows API is a set of APIs, (application programming interfaces) available in the Microsoft Windows operating systems. ...
Conditional jumps This ternary operator can also be used (though less commonly) in conditional jump statements, as in Linoleum: A branch is a point in the instruction stream of a computer program where the address of the next instruction is not the next sequential storage location. ...
A diagram is needed here to show a scaled-down version of the L.In. ...
? logical comparison -> code label; Although the usage and format are different, it's still a ternary operator (used as a conditional expression) because the three arguments come from the code label and from the two arguments in the logical comparison. (The -> operator, which signifies an unconditional jump, takes the place of the : operator.) A branch is a point in the instruction stream of a computer program where the address of the next instruction is not the next sequential storage location. ...
|