Tuesday, 8 January 2013

Conditional operator ( ? )

The conditional operator evaluates an expression returning a value if that expression is true and a different one if
the expression is evaluated as false. Its format is: 

condition ? result1 : result2

If condition is true the expression will return result1, if it is not it will return result2.

 
In this example a was 2 and b was 7, so the expression being evaluated (a>b) was not true, thus the first value
specified after the question mark was discarded in favor of the second value (the one after the colon)which was b,
with a value of 7.


Comma operator ( , )

The comma operator (,) is used to separate two or more expressions that are included where only one expression
is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is
considered.
For example, the following code: 


Would first assign the value 3to b, and then assign b+2to variable a. So, at the end, variable a would contain the
value 5while variable b would contain value 3.

No comments:

Post a Comment