Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

?: 53 C Variants A GNU extension to C allows the second operand to be omitted, and the first operand is implicitly used as the second as well: a = x ? : y; The expression is equivalent to a = x ? x : y; except that if x is an expression, it is evaluated only once. The difference is significant if evaluating the expression has side effects. C# (and Perl) provide similar functionality with their coalescing operator a= x ?? y; (Unlike the above usage of "x ?: y", ?? will only test if x is non-null, as opposed to non-false.) 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 a function call, but this can be cumbersome and inelegant. 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 a 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: #include #include #include using namespace std; int main(int argc, char *argv[]) { } string name; ofstream fout; if (argc > 1 && argv[1]) { } name = argv[1]; fout.open(name.c_str(), ios::out | ios::app); ostream &sout = name.empty() ? cout : fout; 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 function call, inside of which can be an if-else statement.)

?: 54 Furthermore, the ternary operator can yield an lvalue, i.e. a value to which another value can be assigned. Consider the following example: #include int main () { } int a=0, b=0; const bool cond = ...; (cond ? a : b) = 1; std::cout

?: 53<br />

C Variants<br />

A GNU extension to C allows the second operand to be omitted, and the first operand is implicitly used as the<br />

second as well:<br />

a = x ? : y;<br />

The expression is equivalent to<br />

a = x ? x : y;<br />

except that if x is an expression, it is evaluated only once. The difference is significant if evaluating the expression<br />

has side effects.<br />

C# (and Perl) provide similar functionality with their coalescing operator<br />

a= x ?? y;<br />

(Unlike the above usage <strong>of</strong> "x ?: y", ?? will only test if x is non-null, as opposed to non-false.)<br />

C++<br />

In C++ there are conditional assignment situations where use <strong>of</strong> the if-else statement is not possible, since this<br />

language explicitly distinguishes between initialization and assignment. In such case it is always possible to use a<br />

function call, but this can be cumbersome and inelegant. For example, if you want to pass conditionally different<br />

values as an argument for a constructor <strong>of</strong> a field or a base class, it is not possible to use a plain if-else statement; in<br />

this case we can use a conditional assignment expression, or a function call. Mind also that some types allow<br />

initialization, but do not allow assignment, or even the assignment operator does totally different things than the<br />

constructor. The latter is true for reference types, for example:<br />

#include <br />

#include <br />

#include <br />

using namespace std;<br />

int main(int argc, char *argv[])<br />

{<br />

}<br />

string name;<br />

<strong>of</strong>stream fout;<br />

if (argc > 1 && argv[1])<br />

{<br />

}<br />

name = argv[1];<br />

fout.open(name.c_str(), ios::out | ios::app);<br />

ostream &sout = name.empty() ? cout : fout;<br />

In this case there's no possibility to replace the use <strong>of</strong> ?: operator with if-else statement. (Although we can replace the<br />

use <strong>of</strong> ?: with a function call, inside <strong>of</strong> which can be an if-else statement.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!