ternary-operator

Assign only if condition is true in ternary operator in JavaScript

一曲冷凌霜 提交于 2019-11-27 12:36:06
Is it possible to do something like this in JavaScript? max = (max < b) ? b; In other words, assign value only if the condition is true. If the condition is false, do nothing (no assignment). Is this possible? Bergi Don't use the ternary operator then, it requires a third argument. You would need to reassign max to max if you don't want it to change ( max = (max < b) ? b : max ). An if-statement is much more clear: if (max < b) max = b; And if you need it to be an expression, you can (ab)use the short-circuit-evaluation of AND : (max < b) && (max = b) Btw, if you want to avoid repeating

Where can I read about conditionals done with “?” and “:” (colon)? [duplicate]

你。 提交于 2019-11-27 09:54:18
Possible Duplicate: Reference - What does this symbol mean in PHP? I've been doing conditionals with if/else or a year or so now. Looking at some new code, I'm seeing a conditional that appears to use ? and : instead of if and else. I'd like to learn more about this, but I am not sure what to google to find articles explaining how it works. How can I do it? It's the Ternary Operator . Basic usage is something like $foo = (if this expressions returns true) ? (assign this value to $foo) : (otherwise, assign this value to $foo) It can be used for more than assignment though, it looks like other

Why can't I use a “break” statement inside a ternary conditional statement in C++?

北战南征 提交于 2019-11-27 09:44:16
Node is a very simple class with a just a constructor and a few variables: a "name" (actually just a char) and two child Node pointers called "left" and "right". I was just starting to write some code that needs to drop to the left-most node, and I was quite pleased when I came up with this: Node *current = this->root; while (true) (current->left != nullptr) ? current = current->left : break; Seems simple enough: in an infinite loop, check to see if current has a left child, if so, set current to that left child, if not, break out of the loop. It's a cool little one-liner, not too unreadable.

No implicit int -> short conversion in ternary statement

一世执手 提交于 2019-11-27 09:32:31
short s; s = (EitherTrueOrFalse()) ? 0 : 1; This fails with: error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Can anyone explain why this is so? The only thing I can think of is that the compiler doesn't look at the second value and doesn't know the range between the two, in the case I wrote something like short s; s = (EitherTrueOrFalse()) ? 0 : 65000; Correct? The only fix is with an ugly cast? Also, it seems C# does not have a type suffix for the short type. That's a pretty grave oversight IMO. Otherwise, that would've

Ternary operators in Twig php (Shorthand form of if-then-else)

懵懂的女人 提交于 2019-11-27 09:22:22
问题 Is it possible to use ternary operators in twig template? Now, for adding some class to DOM element depend on some condition I do like this: {%if ability.id in company_abilities%} <tr class="selected"> {%else%} <tr> {%endif%} Instead of <tr class="<?=in_array($ability->id, $company_abilities) ? 'selected' : ''?>"> in native php template engine. 回答1: {{ (ability.id in company_abilities) ? 'selected' : '' }} The ternary operator is documented under 'other operators' 回答2: You can use shorthand

Why doesn't the C# ternary operator work with delegates?

丶灬走出姿态 提交于 2019-11-27 09:13:29
When branching to select a function, it might make sense to use the ternary operator to select a function, but this is impossible. Why? public class Demo { protected bool branch; protected void demo1 () {} protected void demo2 () {} public Action DoesntWork() { return branch ? demo1 : demo2; } } The compiler produces the following error: Cannot implicitly convert type `method group' to `System.Action' The problem is that demo1 is not a simple expression, it is a method . And methods can be overriden, so it is not actually one method, it is a method group . Consider the following example:

PHP multiple ternary operator not working as expected

跟風遠走 提交于 2019-11-27 09:04:19
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? Leri 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 left , then the second and so on. Separate second ternary clause with parentheses. echo true ? 1 : (true ?

Speed difference between If-Else and Ternary operator in C…?

雨燕双飞 提交于 2019-11-27 08:57:09
So at the suggestion of a colleague, I just tested the speed difference between the ternary operator and the equivalent If-Else block... and it seems that the ternary operator yields code that is between 1x and 2x faster than If-Else. My code is: gettimeofday(&tv3, 0); for(i = 0; i < N; i++) { a = i & 1; if(a) a = b; else a = c; } gettimeofday(&tv4, 0); gettimeofday(&tv1, 0); for(i = 0; i < N; i++) { a = i & 1; a = a ? b : c; } gettimeofday(&tv2, 0); (Sorry for using gettimeofday and not clock_gettime... I will endeavor to better myself.) I tried changing the order in which I timed the blocks,

python ternary operator behaviour

天涯浪子 提交于 2019-11-27 08:28:41
问题 when I evaluate the following operation 0 if True else 1 + 1 if False else 1 it evaluates to 0 however when I write with brackets like ( 0 if True else 1 ) + ( 0 if False else 1 ) it evaluates correctly to 1 , what is happening in the first case. 回答1: 0 if True else 1 + 1 if False else 1 is actually: (0) if (True) else ((1 + 1) if (False) else (1)) which is definitely differs from what you want: ((0) if (True) else (1)) + ((1) if (False) else (1)) 回答2: as ternary operator is read from left to

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

久未见 提交于 2019-11-27 08:01:13
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? 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 % pre-assign out out = repmat(b,size(cond)); out(cond) = a; For a more advanced solution, there's a way to