Ternary conditional operator: Difference between revisions

Content deleted Content added
SmackBot (talk | contribs)
m Date/fix the maintenance tags or gen fixes using AWB
Jesin (talk | contribs)
I think this deserves some mention. It could probably be said better, but it's better than saying nothing.
Line 37:
 
====C++====
In [[C++]] there are conditional assignment situations where use of the ''if-else'' statement is not possible, since this language explicitly distinguishes between [[initialization]] and [[assignment]]. In such case it is always possible to use an inline function call, but this is cumbersome and inelegant. Use ofocanf the ?: operator is much better whenever possible. 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 we can use a conditional assignment expression, or a function call. 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:
<source lang="c">
#include <iostream>
Line 58:
 
In this case there's no possibility to replace the use of ?: operator with '''if-else''' statement. (Although we can replace the use of ?: with a call to a custom inline function, inside of which can be an ''if-else'' statement.)
 
====[[Python (programming langage)|Python]]====
The Python programming language uses a different syntax for this operator:
<source lang="python">value_when_true if condition else value_when_false</source>
 
===Result type===