ternary-operator

Understanding nested PHP ternary operator

孤街浪徒 提交于 2019-11-26 20:43:52
I dont understand how that output (" four ") comes? $a = 2; echo $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 5 ? 'four' : 'other' ; // prints 'four' I don't understand why " four " gets printed. You need to bracket the ternary conditionals: <?php for ($a=0; $a < 7; $a++) { echo ( $a == 1 ? 'one' : ($a == 2 ? 'two' : ($a == 3 ? 'three' : ($a == 5 ? 'four' : 'other')))); echo "\n"; // prints 'four' } exit; ?> returns: other one two three other four other as you'd expect. See the note at the bottom of "Ternary operators" at PHP Ternary operator help . The expressions are being

Why is it not possible to overload the ternary operator?

匆匆过客 提交于 2019-11-26 20:24:11
问题 Why is it not possible to overload the ternary operator ' ?: '? I use the ternary operator often to consolidate if statements, and am curious why the language designers chose to forbid this operator from being overloaded. I looked for an explanation as to why in C++ Operator Overloading but did not find one describing why this isn't possible. The only information the footnote provides is that it cannot be overloaded. My initial guess is that overloading the operator will almost always violate

Ternary operator left associativity

拥有回忆 提交于 2019-11-26 20:20:42
In the PHP manual, I find the following 'user contributed note' under "Operators". Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity. You cannot write code like this (as you may have accustomed to in C/C++): <?php $a = 2; echo ( $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 4 ? 'four' : 'other'); echo "\n"; // prints 'four' I actually try it and it really prints four . However I could not understand the reason behind it and still feel it should print two or other . Can someone please explain what is happening

Is this ternary conditional ?: correct (Objective) C syntax?

浪尽此生 提交于 2019-11-26 20:19:58
问题 I didn't think this was possible but apparently in Objective C it is allowed: int a = b ?: c; So you see what they're doing here, they're leaving out the second part of the ternary expression, such that if b is nonzero, b is used as the second part. It's clever but as far as I know this is against K&R C, and probably ANSI C. If not, I've been missing out of a terribly clever syntax trick for years...alas! Update: It is gcc. 回答1: From http://en.wikipedia.org/wiki/%3F%3A A GNU extension to C

Ternary ? operator vs the conventional If-else operator in c# [duplicate]

本小妞迷上赌 提交于 2019-11-26 19:49:34
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is the conditional operator slow? I'm a massive user of the ? operator in C#. However my project manager frequently warns me that using ? operator might cost some performance compared to the If-Else statements in a large scale application. So I'm told to avoid using it. However, I love using it because it is concise and sort of keeps the code clean. Is there such performance overhead when using ? operator? 回答1:

Kotlin Ternary Conditional Operator

我是研究僧i 提交于 2019-11-26 19:31:30
What is the equivalent of this expression in Kotlin? a ? b : c This is not valid code in Kotlin. In Kotlin, if statements are expressions. So the following code is equivalent: if (a) b else c The distinction between expression and statement is important here. In Java/C#/JavaScript, if forms a statement, meaning that it does not resolve to a value. More concretely, you can't assign it to a variable. // Valid Kotlin, but invalid Java/C#/JavaScript var v = if (a) b else c If you're coming from a language where if is a statement, this might seem unnatural but that feeling should soon subside.

Ternary operation in CoffeeScript

杀马特。学长 韩版系。学妹 提交于 2019-11-26 18:48:13
问题 I need to set value to a that depends on a condition. What is the shortest way to do this with CoffeeScript? E.g. this is how I'd do it in JavaScript: a = true ? 5 : 10 # => a = 5 a = false ? 5 : 10 # => a = 10 回答1: Since everything is an expression, and thus results in a value, you can just use if/else . a = if true then 5 else 10 a = if false then 5 else 10 You can see more about expression examples here. 回答2: a = if true then 5 else 10 a = if false then 5 else 10 See documentation. 回答3: In

Java ternary operator (?:) doesn't work; second or third operand return boolean

我是研究僧i 提交于 2019-11-26 18:36:41
问题 Can someone tell me why this use of the ternary operator is incorrect? Operands 2 and 3 return a boolean. public class Something { ... private static final double REFERENCE_FRAME_MID_X = 0; private static final double REFERENCE_FRAME_MID_Y = 0; private boolean findInsideOrOutsideGeneralEllipse(Point2D destCirclePos) { List<Boolean> returnValue = new ArrayList<>(); Point2D referenceFrameCenter = new Point2D.Double(REFERENCE_FRAME_MID_X, REFERENCE_FRAME_MID_Y); Ellipse2D insideAreaEllipse2D =

How to write an inline IF statement in JavaScript?

谁说我不能喝 提交于 2019-11-26 18:16:55
How can I use an inline if statement in JavaScript? Is there an inline else statement too? Something like this: var a = 2; var b = 3; if(a < b) { // do something } MattW You don't necessarily need jQuery. JavaScript alone will do this. var a = 2; var b = 3; var c = ((a < b) ? 'minor' : 'major'); The c variable will be minor if the value is true , and major if the value is false . This is known as a Conditional (ternary) Operator. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator There is a ternary operator, like this: var c = (a < b) ? "a is less than

Is there a Matlab conditional IF operator that can be placed INLINE like VBA's IIF

筅森魡賤 提交于 2019-11-26 17:43:45
问题 In VBA I can do the following: A = B + IIF(C>0, C, 0) so that if C>0 I get A=B+C and C<=0 I get A=B Is there an operator or function that will let me do these conditionals inline in MATLAB code? 回答1: There is no ternary operator in Matlab. You can, of course, write a function that would do it. For example, the following function works as iif with n-d input for the condition, and with numbers and cells for the outcomes a and b : function out = iif(cond,a,b) %IIF implements a ternary operator %