ternary-operator

unusual ternary operation

ぃ、小莉子 提交于 2019-11-28 02:43:42
问题 I was asked to perform this operation of ternary operator use: $test='one'; echo $test == 'one' ? 'one' : $test == 'two' ? 'two' : 'three'; Which prints two (checked using php). I am still not sure about the logic for this. Please, can anybody tell me the logic for this. 回答1: Well, the ? and : have equal precedence, so PHP will parse left to right evaluating each bit in turn: echo ($test == 'one' ? 'one' : $test == 'two') ? 'two' : 'three'; First $test == 'one' returns true, so the first

Ternary operator: bad or good practice? [duplicate]

无人久伴 提交于 2019-11-28 02:27:34
问题 This question already has answers here : Closed 10 years ago . I'm looking for reasons to use/not to use it and for original ideas (in their use and to replace them). Duplicate: To Ternary Or Not To Ternary Related (but does not address the question being asked): Which coding style you use for ternary operator? 回答1: For the sake of readability, I only use a ternary if it fits into one 80-char line. 回答2: Good for short tags in templating languages like PHP, e.g: <form> <input type='radio' name

if/else vs ternary operator

白昼怎懂夜的黑 提交于 2019-11-28 01:59:58
Considering the evaluation time, are following two equivalent? if(condition1) { //code1 } else { //code2 } condition1 ? code1 : code2 Or they are just syntactically different? The difference is that the latter station can be used to return a value based on a condition. For example, if you have a following statement: if (SomeCondition()) { text = "Yes"; } else { text = "No"; } Using a ternary operator, you will write: text = SomeCondition() ? "Yes" : "No"; Note how the first example executes a statement based on a condition, while the second one returns a value based on a condition. Well ... In

ternary operator not working

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 01:34:41
Netbeans is saying that my ternary operator isn't a statement. How come? int direction; direction = (Math.random() < 0.5) ? 0 : 1; // direction is either L or R (0 or 1) direction == 0 ? System.out.print('L') : System.out.print('R'); I tried it's if/then/else counterpart and it works fine: int direction; direction = (Math.random() < 0.5) ? 0 : 1; // direction is either L or R (0 or 1) if(direction == 0){ System.out.print('L'); } else { System.out.print('R'); } The statements in the ternary operator need to be non-void. They need to return something. System.out.println(direction == 0 ? 'L' : 'R

why I can set primitive types to null in ternary operations

做~自己de王妃 提交于 2019-11-27 23:42:23
问题 I always thought that primitive types in Java cannot be null , as it is a compile time error if i attempt to do something like this: int test = null; However in a ternary operation, it seems to be allowed: int test = something != 0 ? 5 : null; Isn't a ternary operation just short for (in this case): int test; if (something != 0){ test = 5; } else { test = null } which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException due to autoboxing.

Ternary operators in C#

走远了吗. 提交于 2019-11-27 22:18:06
问题 With the ternary operator, it is possible to do something like the following (assuming Func1() and Func2() return an int: int x = (x == y) ? Func1() : Func2(); However, is there any way to do the same thing, without returning a value? For example, something like (assuming Func1() and Func2() return void): (x == y) ? Func1() : Func2(); I realise this could be accomplished using an if statement, I just wondered if there was a way to do it like this. 回答1: Weird, but you could do class Program {

Why is it not possible to overload the ternary operator?

痴心易碎 提交于 2019-11-27 20:37:45
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 number one or two of the principles given in the link above. The meaning of the overload will rarely

C conditional operator ('?') with empty second parameter [duplicate]

北战南征 提交于 2019-11-27 19:59:45
This question already has an answer here: ?: ternary conditional operator behaviour when leaving one expression empty 2 answers Typically the '?' operator is used in the following form: A ? B : C However in cases where B = A I have seen the following abbreviation A ? : C This surprisingly works. Is it better to leave the second parameter in (style wise), or is their a chance certain compilers won't be able to handle this? Juri Robl It is not permitted by the language C (as far as I know), but compilers such as gcc have the shortcut a?:c as an extension . a?:c means the same as a?a:c . Its a

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

旧城冷巷雨未停 提交于 2019-11-27 19:18:40
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? I ran 100 million Ternary Operators and 100 million If-Else statements and recorded the performance of each. Here is the code: Stopwatch s = new Stopwatch(); // System

Angularjs if-then-else construction in expression

大憨熊 提交于 2019-11-27 17:33:36
Can I somehow use if-then-else construction (ternary-operator) in angularjs expression, for example I have function $scope.isExists(item) that has to return bool value. I want something like this, <div ng-repeater="item in items"> <div>{{item.description}}</div> <div>{{isExists(item) ? 'available' : 'oh no, you don't have it'}}</div> </div> I know that I can use function that returns string, I'm interesting in possibility of using if-then-else construction into expression. Thanks. Andre Goncalves Angular expressions do not support the ternary operator before 1.1.5, but it can be emulated like