ternary-operator

Java - ternary operator weird behaviour

我只是一个虾纸丫 提交于 2019-11-29 11:00:31
问题 I was trying to remove the fractional part from a double in case it is whole using: (d % 1) == 0 ? d.intValue() : d And encountered the following behavior which i don't understand: public static void main(String[] args) { Double d = 5D; System.out.println((d % 1) == 0); // true System.out.println((d % 1) == 0 ? d.intValue() : "not whole"); // 5 System.out.println((d % 1) == 0 ? d.intValue() : d); // 5.0 } As you can see on the third line, the operator chooses the else value - 5.0 even though

Array initialization with a ternary operator?

折月煮酒 提交于 2019-11-29 10:05:20
I don't have access to the C11 specification, therefore I can't investigate this bug. The following declaration rises an error during compilation: int why[2] = 1 == 1 ? {1,2} : {3,4}; The error is: expected expression before { and: expected expression before : This is not valid C11. You can only initialize an array with an initializer-list not with an expression. int why[2] = { ... }; // initializer-list {} Moreover, 1 == 1 ? {1,2} : {3,4} is not a valid C expression because {1, 2} is not a C expression. Just for information using compound literals you can have something close to what you want

How to use ternary operator for this statement in c#

核能气质少年 提交于 2019-11-29 09:34:39
int five = 5; when the variable five is equal to 5, write true when the variable five is not equal to 5, write false How do I write a statement for this in ASP.NET using C#? int five = 5; string answer = five == 5 ? "true" : "false"; I see that you want to use this to write the values out in ASP.NET, the answer string will hold your desired value, use that as you please. The ternary operator in just about every language works as an inline if statement: Console.WriteLine((five == 5) ? 'true' : 'false'); (You shouldn't strictly need the inner parens, but I like to include them for clarity.) If

unusual ternary operation

烂漫一生 提交于 2019-11-29 09:20:39
I was asked to perform this operation of ternary operator use: $test='one'; echo $test == 'one' ? 'one' : $test == 'two' ? 'two' : 'three'; Which prints two (checked using php). I am still not sure about the logic for this. Please, can anybody tell me the logic for this. Nicholas Wilson Well, the ? and : have equal precedence, so PHP will parse left to right evaluating each bit in turn: echo ($test == 'one' ? 'one' : $test == 'two') ? 'two' : 'three'; First $test == 'one' returns true, so the first parens have value 'one'. Now the second ternary is evaluated like this: 'one' /*returned by

Ternary operator: bad or good practice? [duplicate]

假如想象 提交于 2019-11-29 09:06:16
I'm looking for reasons to use/not to use it and for original ideas (in their use and to replace them). Duplicate: To Ternary Or Not To Ternary Related (but does not address the question being asked): Which coding style you use for ternary operator? For the sake of readability, I only use a ternary if it fits into one 80-char line. Good for short tags in templating languages like PHP, e.g: <form> <input type='radio' name='gender' value='m' <?=($gender=='m')?"checked":""?>>Male <input type='radio' name='gender' value='f' <?=($gender=='f')?"checked":""?>>Female </form> Good for switches in

why I can set primitive types to null in ternary operations

我怕爱的太早我们不能终老 提交于 2019-11-29 06:12:35
I always thought that primitive types in Java cannot be null , as it is a compile time error if i attempt to do something like this: int test = null; However in a ternary operation, it seems to be allowed: int test = something != 0 ? 5 : null; Isn't a ternary operation just short for (in this case): int test; if (something != 0){ test = 5; } else { test = null } which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException due to autoboxing. So why the java-compiler doesn't fetch nonsense like this? What happens is that the Java compiler first

Django Template Ternary Operator

梦想的初衷 提交于 2019-11-29 04:24:52
问题 I was wondering if there was a ternary operator (condition ? true-value : false-value) that could be used in a Django template. I see there is a python one (true-value if condition else false-value) but I'm unsure how to use that inside a Django template to display the html given by one of the values. Any ideas? 回答1: Why would you need a ternary operator within a template? {% if %} and {% else %} are all you need. Or you could try the firstof tag: {% firstof var1 var2 var3 %} which outputs

Ternary operators in C#

戏子无情 提交于 2019-11-29 04:04:34
With the ternary operator, it is possible to do something like the following (assuming Func1() and Func2() return an int: int x = (x == y) ? Func1() : Func2(); However, is there any way to do the same thing, without returning a value? For example, something like (assuming Func1() and Func2() return void): (x == y) ? Func1() : Func2(); I realise this could be accomplished using an if statement, I just wondered if there was a way to do it like this. Weird, but you could do class Program { private delegate void F(); static void Main(string[] args) { ((1 == 1) ? new F(f1) : new F(f2))(); } static

Why doesn't the ternary operator like generic types with bounded wildcards?

不羁岁月 提交于 2019-11-29 02:37:48
问题 The following class defines two methods, both of which intuitively have the same functionality. Each function is called with two lists of type List<? super Integer> and a boolean value which specifies which of those lists should be assigned to a local variable. import java.util.List; class Example { void chooseList1(boolean choice, List<? super Integer> list1, List<? super Integer> list2) { List<? super Integer> list; if (choice) list = list1; else list = list2; } void chooseList2(boolean

PHP Shorthand ternary operator “?:” Parse error unexpected “:”

寵の児 提交于 2019-11-29 01:04:22
I've just uploaded some old PHP files to a new server and am getting parse errors (Unexpected ':') on shorthand ternary ops. eg: $y = $x ?: "Some default"; php version is 5.2.16 The code is littered with these shorthand ?:, so before changing them all I thought I'd see if anyone knows anything about this as I've not used PHP for a while now. azat This is only available since PHP 5.3 The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE , and expr3 if expr1 evaluates to FALSE . Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.