conditional-operator

How does the Python conditional operator workaround work?

廉价感情. 提交于 2019-11-26 20:25:45
问题 From what I have read, I found that a built-in ternary operator does not exist (I will be happy to know more about it.). I found the following code as a substitute: def val(): var = float(raw_input("Age:")) status = ("Working","Retired")[var>65] print "You should be:",status I couldn't understand how this code works; can anyone explain me how actually the code is working? I am also interested to know why the ternary operator doesn't exist; any references or links about this will be ore useful

CSS “and” and “or”

我怕爱的太早我们不能终老 提交于 2019-11-26 20:25:34
I've got quite big trouble, because i need to anathematise from styling some input types. I had something like: .registration_form_right input:not([type="radio") { //Nah. } But i don't want to style checkboxes too. I've tried: .registration_form_right input:not([type="radio" && type="checkbox"]) .registration_form_right input:not([type="radio" && "checkbox"]) .registration_form_right input:not([type="radio") && .registration_form_right input:not(type="checkbox"]) How to use && ? And I'll need to use || soon, and I think that usage will be same. Update: I still don't know how to use || and &&

Java conditional operator ?: result type

江枫思渺然 提交于 2019-11-26 20:23:58
I'm a bit puzzled about the conditional operator. Consider the following two lines: Float f1 = false? 1.0f: null; Float f2 = false? 1.0f: false? 1.0f: null; Why does f1 become null and the second statement throws a NullPointerException? Langspec-3.0 para 15.25 sais: Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2)

Is this ternary conditional ?: correct (Objective) C syntax?

浪尽此生 提交于 2019-11-26 20:19:58
问题 I didn't think this was possible but apparently in Objective C it is allowed: int a = b ?: c; So you see what they're doing here, they're leaving out the second part of the ternary expression, such that if b is nonzero, b is used as the second part. It's clever but as far as I know this is against K&R C, and probably ANSI C. If not, I've been missing out of a terribly clever syntax trick for years...alas! Update: It is gcc. 回答1: From http://en.wikipedia.org/wiki/%3F%3A A GNU extension to C

How to write an inline IF statement in JavaScript?

谁说我不能喝 提交于 2019-11-26 18:16:55
How can I use an inline if statement in JavaScript? Is there an inline else statement too? Something like this: var a = 2; var b = 3; if(a < b) { // do something } MattW You don't necessarily need jQuery. JavaScript alone will do this. var a = 2; var b = 3; var c = ((a < b) ? 'minor' : 'major'); The c variable will be minor if the value is true , and major if the value is false . This is known as a Conditional (ternary) Operator. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator There is a ternary operator, like this: var c = (a < b) ? "a is less than

Ternary operator (?:) in Bash

风格不统一 提交于 2019-11-26 18:04:11
Is there a way to do something like this int a = (b == 5) ? c : d; using Bash? ghostdog74 ternary operator ? : is just short form of if/else case "$b" in 5) a=$c ;; *) a=$d ;; esac Or [[ $b = 5 ]] && a="$c" || a="$d" Code: a=$([ "$b" == 5 ] && echo "$c" || echo "$d") If the condition is merely checking if a variable is set, there's even a shorter form: a=${VAR:-20} will assign to a the value of VAR if VAR is set, otherwise it will assign it the default value 20 -- this can also be a result of an expression. As alex notes in the comment, this approach is technically called "Parameter Expansion"

Method Call using Ternary Operator

别等时光非礼了梦想. 提交于 2019-11-26 17:49:11
While playing around with new concepts, I came across the Ternary Operator and it's beauty. After playing with it for a while, I decided to test its limits. However, my fun was ended quickly when I couldn't get a certain line of code to compile. int a = 5; int b = 10; a == b ? doThis() : doThat() private void doThis() { MessageBox.Show("Did this"); } private void doThat() { MessageBox.Show("Did that"); } This line gives me two errors: Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement Error 2 Type of conditional expression cannot be

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

筅森魡賤 提交于 2019-11-26 17:43:45
问题 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? 回答1: 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 %

How can I assign a Func<> conditionally between lambdas using the conditional ternary operator?

人走茶凉 提交于 2019-11-26 17:39:31
Generally, when using the conditional operator, here's the syntax: int x = 6; int y = x == 6 ? 5 : 9; Nothing fancy, pretty straight forward. Now, let's try to use this when assigning a Lambda to a Func type. Let me explain: Func<Order, bool> predicate = id == null ? p => p.EmployeeID == null : p => p.EmployeeID == id; That's the same syntax, and should work? Right? For some reason that doesn't. The compiler gives this nice cryptic message: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression' I

Ternary operator is twice as slow as an if-else block?

为君一笑 提交于 2019-11-26 17:34:12
问题 I read everywhere that ternary operator is supposed to be faster than, or at least the same as, its equivalent if - else block. However, I did the following test and found out it's not the case: Random r = new Random(); int[] array = new int[20000000]; for(int i = 0; i < array.Length; i++) { array[i] = r.Next(int.MinValue, int.MaxValue); } Array.Sort(array); long value = 0; DateTime begin = DateTime.UtcNow; foreach (int i in array) { if (i > 0) { value += 2; } else { value += 3; } // if-else