ternary-operator

How to turn if, else if logic into a ternary operator?

不打扰是莪最后的温柔 提交于 2019-11-29 00:49:31
问题 I was just wondering if this was possible because i started using ternary operators to reduce lines of code and i am loving it. if (x==y) { z += x; } else if (x==z) { z += y; } else { z += 1; } i can do this now if there is only one if statement like this: z = x == y ? z += x : z += 1; 回答1: It would be like this: z = x == y ? z + x : x == z ? z + y : z + 1; If you use z += x as an operand it will end up doing z = (z += x) . While it works in this special case, as the result of the expression

Is it safe to create a const reference to result of ternary operator in C++?

吃可爱长大的小学妹 提交于 2019-11-28 23:05:34
There's something quite non-obvious going on in this code: float a = 1.; const float & x = true ? a : 2.; // Note: `2.` is a double a = 4.; std::cout << a << ", " << x; both clang and gcc output: 4, 1 One would naively expect the same value printed twice but this isn't the case. The issue here has nothing to do with the reference. There are some interesting rules dictating the type of ? : . If the two arguments are of different type and can be casted, they will by using a temporary. The reference will point to the temporary of ? : . The example above compiles fine and it might or might not

Simple PHP isset test

扶醉桌前 提交于 2019-11-28 22:45:15
This below does not seem to work how I would expect it, event though $_GET['friendid'] = 55 it is returning NULL <?PHP $_GET['friendid'] = 55; $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty'; echo $friendid; exit; ?> Remove the ! . You don't want to negate the expression. $friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 'empty'; As of PHP 7's release , you can use the null-coalescing operator (double "?") for this: $var = $array["key"] ?? "default-value"; // which is synonymous to: $var = isset($array["key"]) ? $array["key"] : "default-value"; In PHP 5.3+, if

Javascript shorthand ternary operator

耗尽温柔 提交于 2019-11-28 16:02:35
I know that in php 5.3 instead of using this redundant ternary operator syntax: startingNum = startingNum ? startingNum : 1 ...we can use a shorthand syntax for our ternary operators where applicable: startingNum = startingNum ?: 1 And I know about the ternary operator in javascript: startingNum = startingNum ? startingNum : 1 ...but is there a shorthand? Thanks guys! var startingNumber = startingNumber || 1; Something like that what you're looking for, where it defaults if undefined? var foo = bar || 1; // 1 var bar = 2; foo = bar || 1; // 2 By the way, this works for a lot of scenarios,

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

旧街凉风 提交于 2019-11-28 15:45:32
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. {{ (ability.id in company_abilities) ? 'selected' : '' }} The ternary operator is documented under ' other operators ' You can use shorthand syntax as of Twig 1.12.0 {{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }} {{ foo ? 'yes' }} is the same

python ternary operator behaviour

冷暖自知 提交于 2019-11-28 14:23:18
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. 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)) as ternary operator is read from left to right and + has lower precedence than conditional operators. So, these two are equivalent: >>> 0 if True else 1

Required casting using C# ternary conditional operator

我们两清 提交于 2019-11-28 13:35:33
I am trying to figure out why casts are required in the following examples: bool test = new Random().NextDouble() >= 0.5; short val = 5; // Ex 1 - must cast 0 to short short res = test ? 5 : 0; // fine short res = test ? val : 0; // error short res = test ? val : (short)0; // ugly // Ex 2 - must cast either short or null to short? short? nres = test ? val : null; // error short? nres = test ? (short?)val : null; // ugly short? nres = test ? val : (short?)null; // ugly short? nres = test ? val : default(short?); // ugly The first example just seems crazy to me. If short i = 0; compiles, why can

Precedence: Logical or vs. Ternary operator

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:59:27
Consider the following: (EDIT: I've amended the function slightly to remove the use or braces with the ternary operator) function someFunction(start,end,step){ var start = start || 1, end = end || 100, boolEndBigger = (start < end); // define Boolean here step = step || boolEndBigger ? 1:-1; console.log(step); } someFunction() // step isn't defined so expect (1<10) ? 1:-1 to evaluate to 1 someFunction(1,10) // again step isn't defined so expect to log 1 as before The problem: someFunction(1,10,2) //step IS defined, shortcut logical OR || should kick in, //step should return 2 BUT it returns 1

Throw and ternary operator in C++

一曲冷凌霜 提交于 2019-11-28 12:18:04
The following code compiles with G++ 4.6.1, but not with Visual Studio 2008 return (m_something == 0) ? throw std::logic_error("Something wrong happened") : m_something; The fact is the Visual Studio compiler performs an internal crash. I want to know if this is standard C++ and why it doesn't compile with Visual Studio, but does with G++? It is standard C++. Either (or both) of the then/else expressions in a conditional expression is allowed to be a throw-expression instead (C++98 5.16/2). If Visual Studio crashes when compiling it... that would seem to be unfortunate! Comeau compiles it

Does the C/C++ ternary operator actually have the same precedence as assignment operators?

南楼画角 提交于 2019-11-28 12:15:01
Almost all C/C++ operator precedence tables I have consulted list the ternary conditional operator as having higher precedence than the assignment operators. There are a few tables, however, such as the one on wikipedia , and the one at operator-precedence.com , that place them on the same precedence level. Which is it, higher or same? In the C++ grammar, assignment-expression: conditional-expression logical-or-expression assignment-operator initializer-clause throw-expression conditional-expression: logical-or-expression logical-or-expression ? expression : assignment-expression initializer