conditional-operator

ternary operator in matlab

房东的猫 提交于 2019-11-27 01:51:13
问题 is there a way of typing for if like: var = (cond) ? true : false; or do we have to use this format? if (cond) true else false end 回答1: MatLab doesn't have a ternary operator, or any other syntactic sugar for one-line if-statements. But if your if-statement is really simple, you could just write it in one line anyway: if (cond); casetrue(); else; casefalse(); end It's not as simple as ternary operator, but still better than writing it in 5 lines of code. 回答2: If you only need true or false,

Why is the conditional operator right associative?

耗尽温柔 提交于 2019-11-27 01:07:24
I can understand why the assignment operator is right associative. It makes sense that when x = 4 + 3 is evaluated, that 4 and 3 are added before being assigned to x. I am unclear as to how ?: would benefit from being right associative. Does it only matter when two ?: s were used like this z = (a == b ? a : b ? c : d); Then it is evaluated like this: z = (a == b ? a : (b ? c : d)); Surely it would make more sense to evaluate from left to right? If it evaluated from left to right, it'd look like this: z = ((a == b ? a : b) ? c : d); That is, it would use the result of the first conditional ( a

C# conditional AND (&&) OR (||) precedence

喜你入骨 提交于 2019-11-27 00:54:32
We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up. According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical coworker? http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx bool result = false || true && false; // --> false // is the same result as bool result = (false || true) && false; // --> false // even though I know that the first statement is evaluated as

One-line list comprehension: if-else variants

♀尐吖头ヾ 提交于 2019-11-26 23:30:29
It's more about python list comprehension syntax. I've got a list comprehension that produces list of odd numbers of a given range: [x for x in range(1, 10) if x % 2] This makes a filter - I've got a source list, where I remove even numbers ( if x % 2 ). I'd like to use something like if-then-else here. Following code fails: >>> [x for x in range(1, 10) if x % 2 else x * 100] File "<stdin>", line 1 [x for x in range(1, 10) if x % 2 else x * 100] ^ SyntaxError: invalid syntax There is a python expression like if-else: 1 if 0 is 0 else 3 How to use it inside a list comprehension? shx2 x if y

Ternary operators and Return in C

社会主义新天地 提交于 2019-11-26 22:57:02
问题 Why can't we use return keyword inside ternary operators in C, like this: sum > 0 ? return 1 : return 0; 回答1: return is a statement. Statements cannot be used inside expressions in that manner. 回答2: Because a ternary operation is an expression and you can't use statements in expresssions. You can easily use a ternary operator in a return though. return sum > 0 ? 1 : 0; Or as DrDipShit pointed out: return sum > 0; 回答3: The ternary operator deals in expressions, but return is a statement. The

Why doesn't the conditional operator correctly allow the use of “null” for assignment to nullable types? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:26:20
问题 Possible Duplicates: Nullable types and the ternary operator. Why won’t this work? Conditional operator assignment with nullable<value> types? This will not compile, stating "Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and ''" task.ActualEndDate = TextBoxActualEndDate.Text != "" ? DateTime.Parse(TextBoxActualEndDate.Text) : null; This works just fine if (TextBoxActualEndDate.Text != "") task.ActualEndDate = DateTime

Why is this code invalid in C#?

亡梦爱人 提交于 2019-11-26 22:12:44
The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To fix this, I must do something like this: string foo = "bar"; Object o = foo == null ? DBNull.Value : (Object)foo; This cast seems pointless as this is certainly legal: string foo = "bar"; Object o = foo == null ? "gork" : foo; It seems to me that when the ternary branches are of different types, the compiler will not autobox the values to the type

if/else vs ternary operator

两盒软妹~` 提交于 2019-11-26 22:04:17
问题 Considering the evaluation time, are following two equivalent? if(condition1) { //code1 } else { //code2 } condition1 ? code1 : code2 Or they are just syntactically different? 回答1: 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

Conditional operator used in cout statement

早过忘川 提交于 2019-11-26 22:01:18
问题 By trying, I came to know that it is necessary to put parentheses around a conditional operator in a cout statement. Here a small example: #include <iostream> int main() { int a = 5; float b = (a!=0) ? 42.0f : -42.0f; // works fine std::cout << b << std::endl; // works also fine std::cout << ( (a != 0) ? 42.0f : -42.0f ) << std::endl; // does not work fine std::cout << (a != 0) ? 42.0f : -42.0f; return 0; } The output is: 42 42 1 Why are these parentheses necessary? The resulting type of the

Is the conditional operator slow?

ⅰ亾dé卋堺 提交于 2019-11-26 20:33:23
问题 I was looking at some code with a huge switch statement and an if-else statement on each case and instantly felt the urge to optimize. As a good developer always should do I set out to get some hard timing facts and started with three variants: The original code looks like this: public static bool SwitchIfElse(Key inKey, out char key, bool shift) { switch (inKey) { case Key.A: if (shift) { key = 'A'; } else { key = 'a'; } return true; case Key.B: if (shift) { key = 'B'; } else { key = 'b'; }