conditional-operator

Ternary conditional and assignment operator precedence

蹲街弑〆低调 提交于 2019-11-27 13:05:20
I'm confused about direct assignment and ternary conditional operators precedence: #include<stdio.h> int main(void) { int j, k; j = k = 0; (1 ? j : k) = 1; // first printf("%d %d\n", j, k); j = k = 0; 1 ? j : k = 1; // second printf("%d %d\n", j, k); return 0; } I would expect the output to be: 1 0 1 0 But it happens to be: 1 0 0 0 Plus I get this warning: main.cpp:20: warning: statement has no effect which is about the line I commented as second. Since the direct assignment operator has less precedence than the ternary conditional operator, I was expecting lines commented as first and second

Conditional operator in Coffeescript

て烟熏妆下的殇ゞ 提交于 2019-11-27 12:58:12
I really like this: var value = maxValue > minValue ? minValue : maxValue; Is there something equally concise in Coffescript? value = if maxValue > minValue then minValue else maxValue There is a more concise option in both javascript and coffeescript :) value = Math.min(minValue, maxValue) As Răzvan Panda points out, my comment may actually one of the better answers: value = `maxValue > minValue ? minValue : maxValue` This is a case where it feels like CoffeeScript has competing philosophies: Be concise Don't be redundant Since all operations return a result, the if/then/else way of doing

should std::common_type use std::decay?

萝らか妹 提交于 2019-11-27 08:54:05
Given types A,B , I am concerned with the exact definition of std::common_type<A,B> , disregarding the variadic case std::common_type<A...> for arbitrary types A... . So let using T = decltype(true ? std::declval<A>() : std::declval<B>()); using C = std::common_type<A,B>; Now, according to a number of sources, I have found the following relations (skipping typename for brevity): cppreference.com : C::type = std::decay<T>::type cplusplus.com : C::type = T GCC 4.8.1 <type_traits> implementation: C::type = std::decay<T>::type if T is valid, otherwise C does not contain a ::type member ("SFINAE

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

孤街浪徒 提交于 2019-11-27 08:08:46
问题 So for binary operators on booleans, Java has & , | , ^ , && and || . Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For & , the result value is true if both operand values are true ; otherwise, the result is false . For | , the result value is false if both operand values are false ; otherwise, the result is true . For ^ , the result value is true if the operand values are

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

久未见 提交于 2019-11-27 08:01:13
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? 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 % pre-assign out out = repmat(b,size(cond)); out(cond) = a; For a more advanced solution, there's a way to

How can I port C++ code that uses the ternary operator to Rust?

ⅰ亾dé卋堺 提交于 2019-11-27 07:45:34
问题 How can I port this C++ code to Rust: auto sgnR = (R >= 0.) ? 1. : -1.; I have seen some examples with the match keyword, but I don't understand how it works. 回答1: Rust does not have the ternary operator because it's not needed. Everything evaluates to some value, and if / else statements are no exception: let r = 42.42; let sgn_r = if r >= 0. { 1. } else { -1. }; You'll note that I've also changed your variable names to be idiomatic Rust. Identifiers use snake_case . Do not be confused by

What is the difference between logical and conditional AND, OR in C#? [duplicate]

ぃ、小莉子 提交于 2019-11-27 06:52:38
Possible Duplicate: What is the diffference between the | and || or operators? Logical AND and OR: (x & y) (x | y) Conditional AND and OR: (x && y) (x || y) I've only known about conditional operands up to this point. I know what it does and how to apply it in if-statements. But what is the purpose of logical operands? I prefer to think of it as "bitwise vs. conditional" rather than "logical vs conditional" since the general concept of "logical" applies in both cases. x & y // bitwise AND, 0101 & 0011 = 0001 x | y // bitwise OR, 0101 | 0011 = 0111 x && y // true if both x and y are true x || y

C# if-null-then-null expression

淺唱寂寞╮ 提交于 2019-11-27 06:52:19
Just for curiosity/convenience: C# provides two cool conditional expression features I know of: string trimmed = (input == null) ? null : input.Trim(); and string trimmed = (input ?? "").Trim(); I miss another such expression for a situation I face very often: If the input reference is null, then the output should be null. Otherwise, the output should be the outcome of accessing a method or property of the input object. I have done exactly that in my first example, but (input == null) ? null : input.Trim() is quite verbose and unreadable. Is there another conditional expression for this case,

Coalesce operator and Conditional operator in VB.NET [duplicate]

纵然是瞬间 提交于 2019-11-27 06:37:43
问题 Possible Duplicate: Is there a conditional ternary operator in VB.NET? Hi guys, Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#? 回答1: I think you can get close with using an inline if statement: //C# int x = a ? b : c; 'VB.Net Dim x as Integer = If(a, b, c) 回答2: Sub Main() Dim x, z As Object Dim y As Nullable(Of Integer) z = "1243" Dim c As Object = Coalesce(x, y, z) End Sub Private Function Coalesce(ByVal ParamArray x As Object()) Return x.First

What is the idiomatic Go equivalent of C's ternary operator?

陌路散爱 提交于 2019-11-27 06:10:46
In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator : int index = val > 0 ? val : -val Go doesn't have the conditional operator. What is the most idiomatic way to implement the same piece of code as above ? I came to the following solution, but it seems quite verbose var index int if val > 0 { index = val } else { index = -val } Is there something better ? As pointed out (and hopefully unsurprisingly), using if+else is indeed the idiomatic way to do conditionals in Go. In addition to