This article is rated C-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||||||||||||||||||
|
I just have to get this off my chest: the MAX(a,b) code here,
#define MAX(a,b) (((a)>(b))? (a): (b))
causes three evaluations instead of two, so it is inefficient and should not be used. Melchoir 10:19, 25 February 2006 (UTC)
More importantly it shouldn't be used because it may produce unexpected results, e.g. int z = MAX(++x, ++y); —Preceding unsigned comment added by 27 super goats (talk • contribs) 14:56, 9 May 2008 (UTC)
What is the given argc argv example program good for? I don't understand what the
ostream& sout = name ? fout : cout;
statement does. --Abdull 06:15, 28 March 2007 (UTC)
name
to see if it is a null pointer. If it is, then it sets sout
to a reference to cout
(standard output), otherwise, it sets sout
to a reference to the output stream fout
. --Btx40 (talk) 19:53, 18 April 2008 (UTC)I don't know how to do so, but this page should be flagged as being a very biased viewpoint. The text runs counter to my experience of 20+ years as a professional software programmer. The only people who prefer the ternary conditional operator to the more readable if-then-else format (when both are allowed by the language syntax) are those people who have never had to maintain other people's code bases for any significant period of time. This opinion is found in industry standard texts, such as "Code Complete" by Steve McConnell. I would love to see a valid citation supporting the viewpoint presented here, i.e. that the ternary operator is NOT considered to be antiquated, harder to debug, and harder to maintain.
— Preceding unsigned comment added by 209.242.154.132 (talk • contribs) 16:01, 18 September 2007
I think this would be an acceptable C# equivalent of the C++ example
using System.IO;
static void Main(string[] args)
{
string name;
StreamWriter stw;
if (args.Length >= 1)
{
name = args[0];
stw = new StreamWriter(name, true);
}
StreamWriter output = (name.IsNullOrEmpty() ? System.Console.Out: stw );
}
--Btx40 (talk) 20:10, 17 April 2008 (UTC)
argc > 1
, argv[1]
will always yield a pointer, and the output stream would be the ofstream instance fout
even for an empty string argv[1]
. With the C# example, the output stream would be the console output in such a case. Had the C++ ternary been written as "ostream& sout = (argc > 1) ? fout : cout;
", which more closely and correctly models the possibly intended behaviour of "try to open and use a file if an argument is supplied", then a precise translation would be more likely. The C# example might then set and test a boolean flag such as "filenameWasSupplied
" if an exact copy of the C++ behaviour were required. This said, testing argc instead of name would preclude the possibility for the filename to come from another source in some cases (e.g. an environment variable), and a test for "was a filename supplied" is quite reasonable and more extensible.System.ArgumentException
, ending the program immediately (since there is no handler) while the C++ example would use setstate to set fout
's fail bit.output
because the last line would not run. --Btx40 (talk) 18:07, 18 April 2008 (UTC)System.ArgumentException
from being thrown.using System.IO;
static void Main(string[] args)
{
string name;
StreamWriter stw;
if (args.Length >= 1 && !args[0].IsNullOrEmpty())
{
name = args[0];
stw = new StreamWriter(name, true);
}
StreamWriter output = (name.IsNullOrEmpty() ? System.Console.Out: stw );
}
variable = condition and value_if_true or value_if_false
is incorrect: (true ? false : true) results as false; (true and false or true) results as true. Jarod42 (talk) 13:32, 19 March 2012 (UTC)
I saw a trick a while back to do the ternary operator in python. It went something like this:
[false-condition, true-condition][condition]
Since booleans are 1 or 0 in python, the condition can be an index of a list, and the list can be the true/false conditions. Maybe this ought to be mentioned. --Belugaperson (Talk|Contribs) 00:30, 6 June 2008 (UTC)
# (false-condition, true-condition)[condition]
(3.1415, 42)[$true -eq $false]
3.1415
The page, no longer makes sense. It says, "in PHP uses the incorrect associativity when compared to other languages, and given a value of T for arg, the PHP equivalent of the above example " under PHP, even though the C example is below. Either the PHP text needs to be corrected, or the C needs to be moved up. Superm401 - Talk 19:08, 10 December 2008 (UTC)
Is this the same as in PHP : $int = ($ifstatement) ? $thenvalue : $elsevalue;
An example in PHP but not
I see several other ternary operators, such as ?:, have articles, so I'm wondering if there's a name for this one or a category it falls under:
A Chinese language corpus I use (corpus here, documentation here, but they will only display properly in IE) has several ternary operators for searching, they basically do the same thing as regex but through operators such as $, +, -, etc. For instance,
中$5国
means "all results where 中 comes before 国 and they are separated by 5 or fewer characters"—so 中, 5, and 国 are its arguments. Does anyone know if there's a name for ternary operators like this? rʨanaɢ talk/contribs 23:35, 24 September 2009 (UTC)
There's little information about the ternary in C. Although the header references to the C syntax, there's no information on wether it's C99 etc and there is no example showing C code. Would someone with proper knowledge fix this? Xertoz (talk) 14:48, 23 October 2009 (UTC)
The C, C#, Java, and Javascript examples are completely 100% identical to each other. The Perl and PHP are also the same functionally, but just add the sigils those languages require. These excessive examples add absolutely nothing to helping understand what a ternary operation is. One example in a C-like language would be fine. 128.205.70.161 (talk) 18:23, 18 February 2010 (UTC)
Azarien merged a couple of the examples yesterday. Xertoz (talk) 12:52, 21 February 2010 (UTC)
I understand the point but doesn't the use of only two things a and b fog the issue.
Shouldn't the distiction be something like X if Y else Z
or
if Y result = X else Z.
The use of two variables a and b is also equivalent to asking for the max(a,b) if a language supported it. That is not the spirit of the topic. The spirit is if a>b there is a value returned and if not a different value. In fact the comparison used (a>b) confuses the issue as well. It should be a simple if true/false not necessarily a greater than. It need only be a boolean. —Preceding unsigned comment added by 129.170.107.121 (talk) 21:19, 6 May 2010 (UTC)
Where one in many other languages would write something like a < b && b < c
, Python lets you write a < b < c
. As is explained in Python syntax and semantics#Comparison operators, the Python expression can't be considered as just a shorthand for the expanded version, so it is a ternary operator. Maybe we should add it as an example.
The article mentions 'Visual Basic', the link pointing to the 'Visual Basic.NET' article, and I'm not convinced that Visual Basic (_without_ .NET) has a higher version than 6, as of 2011. — Preceding unsigned comment added by 89.132.149.210 (talk) 17:38, 4 November 2011 (UTC)
Shouldn't be the page renamed “inline if operator” or so and list on it the various inline if's listed here. The “?:” page would redirect to the language C item of the list. I'm suggesting this because when I'm reading how to write inline if in python, it has nothing to do with the “?:” syntax. Furthermore, I believe functional programming languages should be mentioned here, because in at least every functional programming language I've ever met, the conditional operators are “inline”. For example in Haskell,
let x = if a == b then 17 else 84
is perfectly valid, even though it isn't written with an interrogation mark. Großinquisitor Zweihänder (talk) 17:10, 9 November 2012 (UTC)
I agree and this should be done. 50.250.238.149 (talk) 06:05, 14 February 2015 (UTC)
the example given for JS is currently:
var fooNotNull = (foo !== null) ? true : false;
var timeout = settings !== null ? settings.timeout : 1000;
Which is great if you already understand the ternary operator; you are able to figure out what the example is doing. If you don't it's cryptic. The example used on MSDN is much better and as follows:
var now = new Date();
var greeting = "Good" + ((now.getHours() > 17) ? " evening." : " day.");
Which it goes on to explain as:
var now = new Date();
var greeting = "Good";
if (now.getHours() > 17)
greeting += " evening.";
else
greeting += " day.";
Which is a million times better because even without knowing the ternary form you can see what is trying to be achieved. Obviously we can't lift MS's example but there must be many many more examples where the reader could understand the intention and therefore use it to understand the operator. such as
var hemisphere = latitude >= 0 ? "northern" : "southern";
week day / weekend. morning / afternoon. anything where the test is intuitively obvious so we can learn the form by reading it. It may help if all the examples were the same.Flagpolewiki (talk) 15:51, 27 September 2014 (UTC)
For a noobie this article is not readable as it is to complicated! Whoever happens to navigate here does not know what :? operator is. Also that person have no clue to lots of other things in programming. An expert SE however have no reason to be looking for this stuff anyway. So mind the noobies and dont worry about the pros. — Preceding unsigned comment added by AlphaOmega2211 (talk • contribs) 13:00, 25 December 2014 (UTC)
It now states that
a == x ? : y;
is equivalent to
a == x ? x : y;
I think this is incorrect (but I'm not an expert). It should say
a == x ? a==x : y;
The GCC manual says: x ? : y is equivalent to x ? x : y
The sample here confirms my point.
#include <stdio.h>
int main(){
double a=3.0;
double x=3.0;
// condition is true, wikipedia expression fails.
double p = a == x ? : y;
double q = a == x ? x : y;
double r = a == x ? x==a : y;
printf("%g %g %g\n",p,q,r);
// condition is false , here wikipedia is correct
a=3.0;
x=2.0;
p = a == x ? : y;
q = a == x ? x : y;
r= a == x ? a==x : y;
printf("%g %g %g\n",p,q,r);
}
Wikipedia is not Rosetta Code. I think a list of "these languages have it", "these languages don't need a dedicated syntax for it", "these sort of have it" and "these don't" with some code samples where necessary would be useful, but this article is just a pile of "add your favorite language" and little actual substance.
And also ?:#Programming_languages_without_the_conditional_operator mentions Rust and CoffeScript, both of which are in the #Usage section as well. Akeosnhaoe (talk) 06:14, 6 April 2021 (UTC)