ternary-operator

Ternary operator in PowerShell

爱⌒轻易说出口 提交于 2019-12-17 07:12:53
问题 From what I know, PowerShell doesn't seem to have a built-in expression for the so-called ternary operator. For example, in the C language, which supports the ternary operator, I could write something like: <condition> ? <condition-is-true> : <condition-is-false>; If that doesn't really exist in PowerShell, what would be the best way (i.e. easy to read and to maintain) to accomplish the same result? 回答1: $result = If ($condition) {"true"} Else {"false"} Everything else is incidental

Ternary operator left associativity

送分小仙女□ 提交于 2019-12-17 06:36:11
问题 In the PHP manual, I find the following 'user contributed note' under "Operators". Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity. You cannot write code like this (as you may have accustomed to in C/C++): <?php $a = 2; echo ( $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 4 ? 'four' : 'other'); echo "\n"; // prints 'four' I actually try it and it really prints four . However I could not understand the

PHP multiple ternary operator not working as expected

我只是一个虾纸丫 提交于 2019-12-17 06:15:08
问题 Why is this printing 2 ? echo true ? 1 : true ? 2 : 3; With my understanding, it should print 1 . Why is it not working as expected? 回答1: Because what you've written is the same as: echo (true ? 1 : true) ? 2 : 3; and as you know 1 is evaluated to true . What you expect is: echo (true) ? 1 : (true ? 2 : 3); So always use braces to avoid such confusions. As was already written, ternary expressions are left associative in PHP. This means that at first will be executed the first one from the

PHP multiple ternary operator not working as expected

谁都会走 提交于 2019-12-17 06:15:04
问题 Why is this printing 2 ? echo true ? 1 : true ? 2 : 3; With my understanding, it should print 1 . Why is it not working as expected? 回答1: Because what you've written is the same as: echo (true ? 1 : true) ? 2 : 3; and as you know 1 is evaluated to true . What you expect is: echo (true) ? 1 : (true ? 2 : 3); So always use braces to avoid such confusions. As was already written, ternary expressions are left associative in PHP. This means that at first will be executed the first one from the

One-line list comprehension: if-else variants

谁说我不能喝 提交于 2019-12-17 05:21:32
问题 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

Stacking Multiple Ternary Operators in PHP

最后都变了- 提交于 2019-12-17 04:53:32
问题 This is what I wrote : $Myprovince = ( ($province == 6) ? "city-1" : ($province == 7) ? "city-2" : ($province == 8) ? "city-3" : ($province == 30) ? "city-4" : "out of borders" ); But for every field I got the value city-4 . I want to use ternary operators instead of switch/if because I want to experiment and see how it would be done. What's the problem with this code? 回答1: Others have already suggested the right way of doing it but if you really want to use ternary operator you need to use

Error: lvalue required in this simple C code? (Ternary with assignment?)

萝らか妹 提交于 2019-12-17 02:38:24
问题 I have : #include<stdio.h> int main() { int a=5,b=6; (a>b)?b=a:b=b; // Here is the error return 0; } But if I replace : (a>b)?b=a:b=b; // Error with (a>b)?(b=a):(b=b); // No-Error I understand the lvalue is a value to which something can be assigned and how is it different from rvalue , but why is the extra parenthesis making the difference. 回答1: Assignment has a lower precedence than the ternary operator so the line evaluates like: ((a>b)?b=a:b)=b; use: b=(a>b)?a:b; 回答2: Actually, in C, this

Error: lvalue required in this simple C code? (Ternary with assignment?)

蹲街弑〆低调 提交于 2019-12-17 02:38:08
问题 I have : #include<stdio.h> int main() { int a=5,b=6; (a>b)?b=a:b=b; // Here is the error return 0; } But if I replace : (a>b)?b=a:b=b; // Error with (a>b)?(b=a):(b=b); // No-Error I understand the lvalue is a value to which something can be assigned and how is it different from rvalue , but why is the extra parenthesis making the difference. 回答1: Assignment has a lower precedence than the ternary operator so the line evaluates like: ((a>b)?b=a:b)=b; use: b=(a>b)?a:b; 回答2: Actually, in C, this

Ternary operator in AngularJS templates

£可爱£侵袭症+ 提交于 2019-12-17 02:06:12
问题 How do you do a ternary with AngularJS (in the templates)? It would be nice to use some in html attributes (classes and style) instead of creating and calling a function of the controller. 回答1: Update : Angular 1.1.5 added a ternary operator, so now we can simply write <li ng-class="$first ? 'firstRow' : 'nonFirstRow'"> If you are using an earlier version of Angular, your two choices are: (condition && result_if_true || !condition && result_if_false) {true: 'result_if_true', false: 'result_if

Ternary operator in AngularJS templates

非 Y 不嫁゛ 提交于 2019-12-17 02:06:05
问题 How do you do a ternary with AngularJS (in the templates)? It would be nice to use some in html attributes (classes and style) instead of creating and calling a function of the controller. 回答1: Update : Angular 1.1.5 added a ternary operator, so now we can simply write <li ng-class="$first ? 'firstRow' : 'nonFirstRow'"> If you are using an earlier version of Angular, your two choices are: (condition && result_if_true || !condition && result_if_false) {true: 'result_if_true', false: 'result_if