ternary-operator

Why do I have to typecast an int in a ternary expression? [duplicate]

只谈情不闲聊 提交于 2019-12-18 07:02:45
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation and want to know why I have to do it. I'm using .NET 3.5. This works: short foo; if (isValid) foo = -1; else foo = getFoo(); This does not work: short foo; foo = isValid ? -1 : getFoo(); I have to typecast -1: short foo; foo = isValid ? (short)-1 : getFoo(); What does the ternary expression do differently? It considers the -1 to be

Why do I have to typecast an int in a ternary expression? [duplicate]

那年仲夏 提交于 2019-12-18 07:01:45
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation and want to know why I have to do it. I'm using .NET 3.5. This works: short foo; if (isValid) foo = -1; else foo = getFoo(); This does not work: short foo; foo = isValid ? -1 : getFoo(); I have to typecast -1: short foo; foo = isValid ? (short)-1 : getFoo(); What does the ternary expression do differently? It considers the -1 to be

C++11 constexpr function compiler error with ternary conditional operator (?:)

大兔子大兔子 提交于 2019-12-18 06:59:41
问题 What is wrong with this piece of code? #include <iostream> template<unsigned int N, unsigned int P=0> constexpr unsigned int Log2() { return (N <= 1) ? P : Log2<N/2,P+1>(); } int main() { std::cout << "Log2(8) = " << Log2<8>() << std::endl; return 0; } When compiling with gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) , I get the following error: log2.cpp: In function ‘constexpr unsigned int Log2() [with unsigned int N = 0u, unsigned int P = 1023u]’: log2.cpp:5:38: error: template

Javascript shorthand ternary operator

不羁岁月 提交于 2019-12-17 21:41:53
问题 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? 回答1: var startingNumber = startingNumber || 1; Something like that what you're looking for, where it defaults if undefined? var foo = bar |

Required casting using C# ternary conditional operator

与世无争的帅哥 提交于 2019-12-17 21:31:23
问题 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

How to use ternary operator instead of if-else in PHP

橙三吉。 提交于 2019-12-17 19:24:30
问题 I’m trying to shorten my code using the ternary operator. This is my original code: if ($type = "recent") { $OrderType = "sid DESC"; } elseif ($type = "pop") { $OrderType = "counter DESC"; } else { $OrderType = "RAND()"; } How can I use the ternary operator in my code instead of if s/ else s? $OrderType = ($type = "recent") ? "sid DESC" : "counter DESC" ; This is the code I tried, but have no idea how to add an “ elseif part” to it. 回答1: This is called the ternary operator ;-) You could use

Multiple Ternary Operators

旧时模样 提交于 2019-12-17 17:54:28
问题 I need a bit of syntax help with a ternary operator which will help me to put the correct marker icons on to my good map.I have three areas 0,1 and 2 which have unique icons 0, 1 and 2. I used to have just two areas so this ternary operator worked fine; icon: (area == 1) ? icon1: icon0, Now I need to add an additional third icon (icon2) for area2. I've tried various methods but just can't seem to get it right. 回答1: The syntax would be: icon: (area == 1) ? icon1 : (area == 2) ? icon2 : icon0,

Why doesn't this method work? Java ternary operator

你说的曾经没有我的故事 提交于 2019-12-17 10:54:34
问题 What's wrong with this code: void bark(boolean hamlet) { hamlet ? System.out.println("To Bark.") : System.out.println("Not to Bark"); } 回答1: Ternary operators can't have statements that don't return values, void methods. You need statements that have return values. You need to rewrite it. void bark(boolean hamlet) { System.out.println( hamlet ? "To Bark." : "Not to Bark" ); } 回答2: You can read why in the Java Language Specification, 15.25. Conditional Operator ? : It is a compile-time error

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

╄→гoц情女王★ 提交于 2019-12-17 10:06:45
问题 This question already has answers here : Closed 9 years ago . 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? 回答1: It's the Ternary Operator. Basic usage is something like $foo = (if

iif equivalent in c#

前提是你 提交于 2019-12-17 08:54:50
问题 Is there a IIf equivalent in C# ? Or similar shortcut? 回答1: C# has the ? ternary operator, like other C-style languages. However, this is not perfectly equivalent to IIf() ; there are two important differences. To explain the first difference, the false-part argument for this IIf() call causes a DivideByZeroException , even though the boolean argument is True . IIf(true, 1, 1/0) IIf() is just a function, and like all functions all the arguments must be evaluated before the call is made. Put