conditional-operator

Assignment inside Perl ternary conditional operator problems

走远了吗. 提交于 2019-12-04 08:47:51
问题 This snippet of Perl code in my program is giving the wrong result. $condition ? $a = 2 : $a = 3 ; print $a; No matter what the value of $condition is, the output is always 3, how come? 回答1: This is explained in the Perl documentation. Because of Perl operator precedence the statement is being parsed as ($condition ? $a= 2 : $a ) = 3 ; Because the ?: operator produces an assignable result, 3 is assigned to the result of the condition. When $condition is true this means ($a=2)=3 giving $a=3

JSX doesn't evaluate integer in expression as boolean

依然范特西╮ 提交于 2019-12-04 04:13:43
问题 I'm used to write render optional Components like this: var Foo = React.createClass({ render: function() { var length = 0; return <div>Foo {length && <Bar />}</div>; } }); This shorthand if is mentioned in the if/else in JSX guide as immediately-invoked function expression. However, since my latest update of React, it started to render 0 instead of null . Here is a jsfiddle Why is that happening? 回答1: The && operator evaluates the left-hand expression first, and if the left-hand expression

PHP nested conditional operator bug?

♀尐吖头ヾ 提交于 2019-12-04 04:04:29
问题 return true ? 'a' : false ? 'b' : 'c'; This should return 'a', but it doesn't. It returns 'b' instead. Is there a bug in PHP's order of handling the different parts of the conditional operators? I got the idea from Are multiple conditional operators in this situation a good idea? where it does seem to work correctly. (the true and false are for the purpose of the example, of course. in the real code they are statements that evaluate to true and false respectively. yes, i know that for sure)

if(condition, then, else) in Oracle

╄→гoц情女王★ 提交于 2019-12-04 00:58:12
MySQL/MSSQL has a neat little inline if function you can use within queries to detect null values, as shown below. SELECT ... foo.a_field AS "a_field", SELECT if(foo.bar is null, 0, foo.bar) AS "bar", foo.a_field AS "a_field", ... The problem I'm running into now is that this code is not safe to run on an Oracle database, as it seems not to support this inline if syntax. Is there an equivalent in Oracle? Use the standard COALESCE function: SELECT COALESCE(foo.bar, 0) as "bar", ... Or use Oracle's own NVL function that does the same. To supplement the rest of the answers here, which deal

Does Python have the Elvis operator?

浪尽此生 提交于 2019-12-03 17:49:55
问题 The ternary operator in many languages works like so: x = f() ? f() : g() Where if f() is truthy then x is assigned the value of f() , otherwise it is assigned the value of g() . However, some languages have a more succinct elvis operator that is functionally equivalent: x = f() ?: g() In python, the ternary operator is expressed like so: x = f() if f() else g() But does python have the more succinct elvis operator? Maybe something like: x = f() else g() # Not actually valid python 回答1: Yes

Conditional Operators in Javascript

[亡魂溺海] 提交于 2019-12-03 16:34:41
问题 Is it ok to use conditional operators like a statement like so? (x == y) ? alert("yo!") : alert("meh!"); Or is it more correct to use it to assign a value like so? z = (x == y) ? "yo!" : "meh!"; If it's not incorrect to use it like a statement, then is it possible to add more than one line of code for execution like so? Is it more correct to use ifthen and switch statements for multiple lines of code? (x == y) ? (alert("yo!"), document.write("woot!")) : (alert("meh!"), document.write("blah!")

Convert ternary conditional operators into if statements?

橙三吉。 提交于 2019-12-03 13:55:47
问题 With minified code that looks like this, f&&!f.error?k.button.b==k.button.c.G?k.button.Q(b,e,f,c,d):k.button.b==k.button.c.o&&k.button.P(b,e,f,c,d):(console.error(f),f=f.error.message||chrome.i18n.getMessage("error_tooltip"),k.button.v(b.id,f),d({action:"error"})) Is there an automated tool that can transform that one line of conditional operators into a series of if statements? Example 1: From (i < 0 ? function1() : function2()) to if (i < 0) { function1(); } else { function2(); } Example 2:

Compiler error for conditional operator “?:” when used with typecasting operator

风格不统一 提交于 2019-12-03 13:41:53
Following code is in simplest form: struct X { operator char () const { return 'a'; } }; int main () { X obj, *p = &obj; char a = *p; // ok char c = (true)? *p : 'z'; } This code gives compiler error as, error: operands to ?: have different types ‘X’ and ‘char’ Why *p is not resolved to char when there is no ambiguity in class X for typecasting operator ? Is such spurious error message correct or it's a g++ bug ? [ Update Note: Interestingly this scenario doesn't generate such error ] It seems to be a compiler-bug. I checked it out in the spec, the Standard clearly says (§5.16/3 - C++03),

Using conditional operator in lambda expression in ForEach() on a generic List?

偶尔善良 提交于 2019-12-03 12:59:57
Is it not allowed to have a conditional operator in a lambda expression in ForEach? List<string> items = new List<string>{"Item 1", "Item 2", "Item I Care About"}; string whatICareAbout = ""; // doesn't compile :( items.ForEach(item => item.Contains("I Care About") ? whatICareAbout += item + "," : whatICareAbout += ""); Compilation error -> "Only assignment, call, increment, decrement, and new object expressions can be used as a statement" Trying to use a normal if doesn't work either: // :( items.ForEach(item => if (item.Contains("I Care About")) {whatICareAbout += item + ", ";} Just not

Ternary operator in C vs C++ [duplicate]

与世无争的帅哥 提交于 2019-12-03 12:38:44
This question already has answers here : Errors using ternary operator in c (5 answers) There are a lot of differences between C and C++ and came to stuck on one of them The same code gives an error in C while just executes fine in C++ Please explain the reason int main(void) { int a=10,b; a>=5?b=100:b=200; } The above code gives an error in C stating lvalue required while the same code compiles fine in C++ Have a look at the operator precedence. Without an explicit () your code behaves like ( a >= 5 ? b = 100 : b ) = 200; The result of a ?: expression is not a modifiable lvalue [#] and hence