conditional-operator

Nested inline if statement

China☆狼群 提交于 2019-12-12 03:47:31
问题 In VB .Net it is possible to use inline If statements, like this if a Then ret = "A" Else ret = "Not A" I know it is also possible to nest these statements. I know this might not be good practice as readability drops down... If a Then If b Then ret = "A & B" Else ret = "A & Not B" Else ret = "Not A" which will be evaluated like this : If a Then If b Then ret = "A & B" Else ret = "A & Not B" End If Else ret = "Not A" End If Now if I remove the last Else statement, I get this : If a Then If b

goto not working with ?: operator in C

蹲街弑〆低调 提交于 2019-12-11 20:07:25
问题 For learning purposes, I wrote the following code snippet: for(int i=0;i<10;i++) { for(int j = 0;j<5;j++) { //(i==j && i==3)? (goto found) : printf("stya here\n"); if(i==j && i==3){goto found;} else {printf("stay here\n");} } } found: printf("yes I am here"); But I wondered when I discovered the omitted statement inside the inner loop not gives error and now I am confused about if-else is not always replaceable with ?: operator. What is the fact here? Why does the commented statement give an

declaring a variable within conditional expressions (ternary operator)

我只是一个虾纸丫 提交于 2019-12-11 12:38:40
问题 Is it possible to declare the variable within a conditional expression? for example: The code below return a syntax error (because I've declared the variable x within the conditional expression?). var a = document.getElementById("userData"); var d = a.value; function() { (d.length>15)?( alert("your input was too long")):( var x = parseInt(d).toString(2), a.value=x ); } obviously this can be fixed by simply adding var x; outside the statement, but is it possible for variables to be declared

Ternary operator

你离开我真会死。 提交于 2019-12-11 05:51:25
问题 I have an array d = ['foo', 'bar', 'baz'] , and want to put its elements together into a string delimited by , and and at the last element so that it will become foo, bar and baz . Here is what I'm trying to do: s = '' d.each_with_index { |x,i| s << x s << i < d.length - 1? i == d.length - 2 ? ' and ' : ', ' : '' } but the interpreter gives an error: `<': comparison of String with 2 failed (ArgumentError) However, it works with += instead of << , but the Ruby Cookbook says that: If efficiency

What do fellow .NET developers think about the conditional operator? [duplicate]

风流意气都作罢 提交于 2019-12-11 04:49:36
问题 This question already has answers here : To ternary or not to ternary? [closed] (54 answers) Closed 4 years ago . I really really like the conditional operator in C#. It makes my life very much easier in writing logic such as this: public string FormattedFileName { get { return string.Format("{0}_{1}_{2}_{3}.xls", DateTime.Now.Month.ToString().Length == 1 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString().Length == 1 ? "0" + DateTime.Now.Day

typescript infer string literal from ternary conditional

大憨熊 提交于 2019-12-11 04:10:55
问题 This is a simplified example: function doSomething(animal: 'bird' | 'fish'){ } let flies=true; const animal = flies ? 'bird' : 'fish' doSomething(animal); Typescropt infers type 'bird' | 'fish' in the assignation to animal from the ternary conditional. (if animal weren't const it would complain as it would infer type string not being assignable to 'bird' | 'fish') But const parms ={ animal: flies ? 'bird' : 'fish' } doSomething(parms); /* Argument of type '{ animal: string; }' is not

Is there a simple way to test if you match one of a set of enumerations?

隐身守侯 提交于 2019-12-10 23:08:56
问题 Consider this code... switch(testValue) { case .ValueA, .ValueB, .ValueC: return 40 default: return 0 } Now if I was just checking for a single enumeration value, I could do this... return (testValue == .ValueA) ? 40 : 0; But I'm wondering how I can have something like the latter, but testing for multiples like the former, similar to this pseudo-code... return (testValue is in [.ValueA, .ValueB, .ValueC]) ? 40 : 0; I know I can do it with an inline array, like so... return ([SomeEnum.ValueA,

ternary operator and assignment operator

天涯浪子 提交于 2019-12-10 21:11:19
问题 in Does the C/C++ ternary operator actually have the same precedence as assignment operators? Luchian Grigore's answer says that cases like a ? b : c = d will always be inferred as a ? b : ( c = d ) because both = and ?: associate right to left so in c++ k = 21 > 3 ? j = 12 : j = 10; and k = 1 > 3 ? j = 12 : j = 10; both are fine. In C k = 21 > 3 ? 12 : j = 10 returns error invalid lvalue in assignment. Shouldn't above be inferred as (and return no error) k= 21 > 3 ? 12 : ( j = 10 ) I assume

Getting error “lvalue required as left operand of assignment”

◇◆丶佛笑我妖孽 提交于 2019-12-10 18:37:30
问题 I am a novice in C and having the following problem while compiling a simple code: #include <stdio.h> int main() { int i, a, b; i = 3; a = b = 0; printf("Before: "); printf("%d %d\n", a, b); i == 3 ? a = 4 : a = 10; /* Line 9 */ printf("After: "); printf("%d %d\n", a, b); return 0; } Gives me error: #gcc some.c In function ‘main’: some.c:9: error: lvalue required as left operand of assignment I cannot understand it. What am i doing wrong? 回答1: This operator i==3 ? a=4 : a = 10; is equivalent

casting inside conditional operator in Java

你说的曾经没有我的故事 提交于 2019-12-10 13:44:30
问题 This give an error in eclipse IDE.(Error symbol appearing near the line number) String[] allText = null; After this i have done some stuff like initialing the array and so on. But according to some conditions. So I want to use a conditional operator like below. List<String> finalText = (allText != null) ? Arrays.asList(allText) : (List<String>) Collections.emptyList(); If I put my casting just after the equal sign, it works well.(wrapping the full ternary operation) What is the purpose of