ternary-operator

Use of brackets in expression that include ternary operator [duplicate]

江枫思渺然 提交于 2019-12-24 07:06:03
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Error: lvalue required in this simple C code? (Ternary with assignment?) In the following piece of code I got an error like " lvalue required as left operand of assignment ". I am unable to understand why such an error is being reported. But when I am using parenthesis in the expression like (i>j)?(k=i):(k=j) it is not reporting an error. please explain. int main() { int i = 2; int j = 9; int k; (i>j) ? k=i : k

Ternary operator evaluation order

ぃ、小莉子 提交于 2019-12-24 05:15:06
问题 class Foo { public: explicit Foo(double item) : x(item) {} operator double() {return x*2.0;} private: double x; } double TernaryTest(Foo& item) { return some_condition ? item : 0; } Foo abc(3.05); double test = TernaryTest(abc); In the above example, why is test equal to 6 (instead of 6.1) if some_condition is true? Changing the code like below returns value of 6.1 double TernaryTest(Foo& item) { return some_condition ? item : 0.0; // note the change from 0 to 0.0 } It seems that (in the

Java 8 ifPresent vs ternary operator

对着背影说爱祢 提交于 2019-12-24 03:36:07
问题 What do you think is better (with arguments, of course): Optional.ofNullable( userName ) .ifPresent( nonNullUserName -> header.setUser( createUser( nonNullUserName ) ) ); or header.setUser( userName == null ? createUser( userName ) : null ); The method createUser creates xml element and the intent of the whole peace of code is to set it in a SOAP request depending on presence of userName . The benefits of the first approach I see is the absence of useless operations, the code does one thing

Proper use of a comma in javascript ternary operator

给你一囗甜甜゛ 提交于 2019-12-24 01:44:08
问题 Rather than use an if else statement, I'm trying to use the ternary operator but have a syntax error somewhere in my statement. Can someone tell me where I am going wrong? Statement is: my_alert(status ? ('Accepted', 'alert-success') : ('Declined', 'alert-info')) my_alert is a function which has 2 parameters. Status just evaluates to true or false. When I pass more than 1 parameter into the above expression, it doesn't like the use of the comma. In chrome and firefox when the function runs it

Ternary operator not working with reference variables in PHP [closed]

吃可爱长大的小学妹 提交于 2019-12-23 18:17:03
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . Why isn't this working? $a = 'FOO'; $foo = $a ? &$a : 'whatever'; // <- error here echo $foo; I get a parse error :| 回答1: If you want to assign a

Ternary operator and increment operator

余生长醉 提交于 2019-12-23 17:12:01
问题 Is this a valid / sound way of resetting a counter if a condition is not met? It is the most compact way I could think of. int counter = 0; int a,b; // Do .. and assign a and b counter = ((a<b) ? counter++ : 0); 回答1: You are already assigning to counter , so don't use ++ as well. counter = condition ? (counter + 1) : 0; 回答2: The behaviour of counter = (condition ? counter++ : 0); is undefined as there's no sequencing point. (The ternary is not sequenced, and neither is assignment). It's

Does JScript Provide a Ternary Operator?

孤街醉人 提交于 2019-12-23 16:08:00
问题 Do we have a ternary operator in Jscript (as opposed to JavaScript)? If so, what is the syntax? 回答1: It's expression ? expression : expression just like C. It's a little looser, actually, because JavaScript is not strongly-typed. Thus the two possible "forks" of the operator can result in different types of values. Thus: alert(document.all ? "Hello from IE!" : "Hello from a non-IE browser!"); Most of the time, the differences between Microsoft's ECMAScript and those found in other browsers

Can't use break in the false part of the ternary operator in Ruby

不羁岁月 提交于 2019-12-23 07:10:12
问题 Consider the following code just as an example : This one works i = 0 flag = false while i < 10 flag = true if flag i+=1 else break end end But when turn If part into ternary operator like this i = 0 flag = false while i < 10 flag = true if flag ? i+=1 : break end I get this errors: ternary.rb:5: void value expression ternary.rb:6: syntax error, unexpected end-of-input, expecting keyword_end I know this code lacks of logic, but the current example is the best what I came up with to show you

Boolean multiplication in c++?

让人想犯罪 __ 提交于 2019-12-23 07:00:39
问题 Consider the following: inline unsigned int f1(const unsigned int i, const bool b) {return b ? i : 0;} inline unsigned int f2(const unsigned int i, const bool b) {return b*i;} The syntax of f2 is more compact, but do the standard guarantees that f1 and f2 are strictly equivalent ? Furthermore, if I want the compiler to optimize this expression if b and i are known at compile-time, which version should I prefer ? 回答1: Well, yes, both are equivalent. bool is an integral type and true is

'exit' is not a keyword in Python, but no error occurs while using it

ε祈祈猫儿з 提交于 2019-12-22 08:55:17
问题 I learn that exit is not a keyword in Python by, import keyword print('exit' in keyword.kwlist) # Output: False But there is no reminder of NameError: name 'exit' is not defined while using it. The output of the following snippet code makes me confused. Can anyone help me out? for i in range(5): print(i) cur=i if i<2 else exit print(cur) # Output 0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit I am unable to get related info about exit from Python documentations, except for exit([code=None]