conditional-operator

What does the question mark and the colon (?: ternary operator) mean in objective-c?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-25 23:02:26
问题 What does this line of code mean? label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect; The ? and : confuse me. 回答1: This is the C ternary operator (Objective-C is a superset of C): label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect; is semantically equivalent to if(inPseudoEditMode) { label.frame = kLabelIndentedRect; } else { label.frame = kLabelRect; } The ternary with no first element (e.g. variable ?: anotherVariable ) means the same as (valOrVar != 0) ?

Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-25 22:59:26
问题 This question already has an answer here: Conditional operator assignment with Nullable<value> types? 5 answers Why doesn't this C# code compile? 4 answers I just came across a weird error: private bool GetBoolValue() { //Do some logic and return true or false } Then, in another method, something like this: int? x = GetBoolValue() ? 10 : null; Simple, if the method returns true, assign 10 to the Nullable int x. Otherwise, assign null to the nullable int. However, the compiler complains: Error

Does Python have a ternary conditional operator?

狂风中的少年 提交于 2019-11-25 22:51:44
问题 If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs? 回答1: Yes, it was added in version 2.5. The expression syntax is: a if condition else b First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition . If condition evaluates to True , then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored. This allows

Conditional operator cannot cast implicitly?

拟墨画扇 提交于 2019-11-25 22:47:36
问题 I\'m a little stumped by this little C# quirk: Given variables: Boolean aBoolValue; Byte aByteValue; The following compiles: if (aBoolValue) aByteValue = 1; else aByteValue = 0; But this will not: aByteValue = aBoolValue ? 1 : 0; Error says: \"Cannot implicitly convert type \'int\' to \'byte\'.\" And of course, this monstrosity will compile: aByteValue = aBoolValue ? (byte)1 : (byte)0; What\'s going on here? EDIT: Using VS2008, C# 3.5 回答1: This is a fairly frequently asked question. In C#, we

What is the Java ?: operator called and what does it do?

余生颓废 提交于 2019-11-25 22:16:36
问题 I have been working with Java a couple of years, but up until recently I haven\'t run across this construct: int count = isHere ? getHereCount(index) : getAwayCount(index); This is probably a very simple question, but can someone explain it? How do I read it? I am pretty sure I know how it works. if isHere is true, getHereCount() is called, if isHere is false getAwayCount() is called. Correct? What is this construct called? 回答1: Yes, it is a shorthand form of int count; if (isHere) count =

Question mark and colon in JavaScript

你。 提交于 2019-11-25 21:49:09
问题 I came across the following line hsb.s = max != 0 ? 255 * delta / max : 0; What do the ? and : mean in this context? 回答1: It is called the Conditional Operator (which is a ternary operator). It has the form of: condition ? value-if-true : value-if-false Think of the ? as "then" and : as "else". Your code is equivalent to if (max != 0) hsb.s = 255 * delta / max; else hsb.s = 0; 回答2: Properly parenthesized for clarity, it is hsb.s = (max != 0) ? (255 * delta / max) : 0; meaning return either

Return type of &#39;?:&#39; (ternary conditional operator)

心不动则不痛 提交于 2019-11-25 20:57:13
Why does the first return a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; While the second does not? int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second did not compile at all - "not lvalue left of assignment". CB Bailey Expressions don't have return types, they have a type and - as it's known in the latest C++ standard - a value category. A conditional expression can be an lvalue or an rvalue . This is its value category. (This is somewhat of a simplification, in C++11 we have lvalues, xvalues and prvalues.) In very broad and simple terms, an lvalue refers to an