ternary-operator

Using ternary operator to initialize a reference variable?

自古美人都是妖i 提交于 2019-12-05 14:35:21
问题 Putting all the maintainability and reading issues aside, can these lines of code generate undefined behavior? float a = 0, b = 0; float& x = some_condition()? a : b; x = 5; cout << a << ", " << b; 回答1: No, it's just fine. It would not create undefined behavior in this code. You will just change value of a or b to 5, according to condition. 回答2: This is absolutely fine, as long as both sides of the conditional are expressions that can be used to initialize a reference (e.g. variables, pointer

Weird behaviour when using Java ternary operator

梦想与她 提交于 2019-12-05 14:08:52
问题 When I write my java code like this: Map<String, Long> map = new HashMap<>() Long number =null; if(map == null) number = (long) 0; else number = map.get("non-existent key"); the app runs as expected but when I do this: Map<String, Long> map = new HashMap<>(); Long number= (map == null) ? (long)0 : map.get("non-existent key"); I get a NullPointerException on the second line. The debug pointer jumps from the second line to this method in the java.lang.Thread class: /** * Dispatch an uncaught

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

时光怂恿深爱的人放手 提交于 2019-12-05 13:58:41
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]) . Keywords are part of the python syntax. They usually have special meaning in statements (e.g. for ,

How to Implement the Ternary Operator in the DLR

淺唱寂寞╮ 提交于 2019-12-05 11:44:44
I am implementing a language interpreter in C# using the DLR, and I'm having some troubles with the ternary operator. At this point, I have basic function declarations/calls implemented, like so: F := (x) -> x + 1 F(1) # returns 2 I've not had a problem with a function body being a sequence of expressions -- the value of the last expression is always returned, and I've made sure all cases in the interpreter return at least something as a side effect. I'm now trying to implement the ternary operator (? :). The Expression tree I'm rendering looks like this: work = Expression.IfThenElse(

Is There '?' Control Flow in Python? [duplicate]

烂漫一生 提交于 2019-12-05 10:15:35
This question already has answers here : Closed 7 years ago . Possible Duplicate: Python Ternary Operator Is there control flow operator similar to '?' of C/C++ in python? If there is a chunk of code similar to this: return n <= 1 ? n : fibo(n-1) + fibo(n-2) Will got an error like this: File "fibonacci.py", line 2 return n <= 1 ? n : fibo(n-1) + fibo(n-2) ^ SyntaxError: invalid syntax Yes, the conditional expression is available in Python 2.5+ : return n if n <= 1 else fibo(n-1) + fibo(n-2) You can try this short circuit expression return n > 1 and fibo(n-1) + fibo(n-2) or n . While this is

More concise ternary expression?

白昼怎懂夜的黑 提交于 2019-12-05 07:46:39
I often find myself needing to write code with the following logical pattern: $foo = isset($bar) ? $bar : $baz; I know about the ?: syntax: $foo = $bar ?: $baz; ...which, on the surface, appears to be what I'm looking for; however, it throws an undefined notice index when $bar is not set. It also uses the same logic as empty() , meaning that "empty" values like FALSE , 0 , "0" , etc. don't pass. Hence, it's not really equivalent. Is there a shorter way of writing that code without throwing a notice when $bar is not set? Edit: To make it a bit more clear why I'm looking for a shortcut syntax,

Kotlin equivalent of ternary operator [duplicate]

余生颓废 提交于 2019-12-05 07:21:35
This question already has an answer here: Kotlin Ternary Conditional Operator 28 answers So in java we have the ternary operator (?), which sometimes is useful to easy some value computed by a if-else inlines. For example: myAdapter.setAdapterItems( textToSearch.length == 0 ? noteList : noteList.sublist(0, length-5) ) I know the equivalent in kotlin would be: myAdapter.setAdapterItems( if(textToSearch.length == 0) noteList else noteList.sublist(0, length-5) ) But i just used to love the ternary operator in Java, for short expression conditions, and when passing values to a method. Is there any

Initializing reference variables with the conditional operator

感情迁移 提交于 2019-12-05 06:32:14
The following C++ is invalid because reference variables require initializers: int& a; // illegal if (isfive) { a = 5; } else { a = 4; } However, MSVC seems to think this is okay: int& a = isfive ? 5 : 4; This implies to me that MSVC is actually treating the conditional operator like a single expression and not expanding it into an if-else statement. Is it always valid C++ to initialize a reference using the conditional operator? MSVC has a non-standard "extension". What it means is that it allows broken code. There's a good reason this is prohibited. Note also that int& a = 5; is not legal in

Are ternary statements faster than if/then/else statements in javascript?

大憨熊 提交于 2019-12-05 06:29:20
I see a lot of: var something = (is_something_true()) ? 3 : 4; in javascript. Is this faster than var something; if (is_something_true()) { something = 3; } else { something = 4; } Or is it written concisely for convenience? Please enjoy this -- if difference is statistically valid then the result (true or false) also matters -- clearly this is just other stuff on the machine having an impact on browser performance: Here is the link There is a fundamental difference between the two, the ternary statements are expressions and not flow of control. If there is a case where someone writes it as a

question mark and colon - if else in ruby

点点圈 提交于 2019-12-05 04:52:50
Hi I have a question about ruby on rails Apparently I have a statement like this: def sort_column Product.column_names.include?(params[:sort]) ? params[:sort] : "name" end From what I read, it's said that this method sort the column based on params[:sort] and if there no params the products will be sorted by "name". However, I don't understand the way this statement is written, especially the second "?". Can someone explain it to me ? This is your code, rearranged for easier understanding. def sort_column cond = Product.column_names.include?(params[:sort]) cond ? params[:sort] : "name" # it's