conditional-operator

Conditional operator differences between C and C++

空扰寡人 提交于 2019-11-26 13:06:28
I read somewhere that the ?: operator in C is slightly different in C++, that there's some source code that works differently in both languages. Unfortunately, I can't find the text anywhere. Does anyone know what this difference is? The conditional operator in C++ can return an lvalue, whereas C does not allow for similar functionality. Hence, the following is legal in C++: (true ? a : b) = 1; To replicate this in C, you would have to resort to if/else, or deal with references directly: *(true ? &a : &b) = 1; Also in C++, ?: and = operators have equal precedence and group right-to-left , such

C# if-null-then-null expression

天涯浪子 提交于 2019-11-26 12:57:12
问题 Just for curiosity/convenience: C# provides two cool conditional expression features I know of: string trimmed = (input == null) ? null : input.Trim(); and string trimmed = (input ?? \"\").Trim(); I miss another such expression for a situation I face very often: If the input reference is null, then the output should be null. Otherwise, the output should be the outcome of accessing a method or property of the input object. I have done exactly that in my first example, but (input == null) ?

Type result with conditional operator in C#

微笑、不失礼 提交于 2019-11-26 12:47:02
问题 I am trying to use the conditional operator, but I am getting hung up on the type it thinks the result should be. Below is an example that I have contrived to show the issue I am having: class Program { public static void OutputDateTime(DateTime? datetime) { Console.WriteLine(datetime); } public static bool IsDateTimeHappy(DateTime datetime) { if (DateTime.Compare(datetime, DateTime.Parse(\"1/1\")) == 0) return true; return false; } static void Main(string[] args) { DateTime myDateTime =

Why does std::istringstream appear to resolve differently to std::ifstream in the ternary (?:) operator?

南楼画角 提交于 2019-11-26 12:46:40
问题 I am used to writing little command line tools that take either a file name or read from std::cin , so I have been using this pattern for quite a while: int main(int argc, char* argv[]) { std::string filename; // args processing ... std::ifstream ifs; if(!filename.empty()) ifs.open(filename); std::istream& is = ifs.is_open() ? ifs : std::cin; std::string line; while(std::getline(is, line)) { // process line... } return 0; } After reading a question on Stack Overflow, I tried to modify my

What is the difference between logical and conditional AND, OR in C#? [duplicate]

拜拜、爱过 提交于 2019-11-26 12:13:23
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: What is the diffference between the | and || or operators? Logical AND and OR: (x & y) (x | y) Conditional AND and OR: (x && y) (x || y) I\'ve only known about conditional operands up to this point. I know what it does and how to apply it in if-statements. But what is the purpose of logical operands? 回答1: I prefer to think of it as "bitwise vs. conditional" rather than "logical vs conditional" since the general

Why does the ternary operator unexpectedly cast integers?

落爺英雄遲暮 提交于 2019-11-26 11:48:15
I have seen it discussed somewhere that the following code results in obj being a Double , but that it prints 200.0 from the left hand side. Object obj = true ? new Integer(200) : new Double(0.0); System.out.println(obj); Result: 200.0 However, if you put a different object in the right hand side, e.g. BigDecimal , the type of obj is Integer as it should be. Object obj = true ? new Integer(200) : new BigDecimal(0.0); System.out.println(obj); Result: 200 I presume that the reason for this is something to do with casting the left hand side to a double in the same way that it happens for integer

Operator precedence with Javascript Ternary operator

感情迁移 提交于 2019-11-26 11:42:42
I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? ' error' : 'error' The way i think this code works is as following: h.className = h.className + h.className ? ' error' : 'error' But that isn't correct because that gives a error in my console. So my question is how should i interpet this code correctly? Kobi h.className = h.className + (h.className ? ' error' : 'error') You want the operator to work for h.className , better be specific about it. Of course, no harm should come from h.className += ' error'

How to check if my string is equal to null?

♀尐吖头ヾ 提交于 2019-11-26 11:34:06
I want to perform some action ONLY IF my string has a meaningful value. So, I tried this. if (!myString.equals("")) { doSomething } and this if (!myString.equals(null)) { doSomething } and this if ( (!myString.equals("")) && (!myString.equals(null))) { doSomething } and this if ( (!myString.equals("")) && (myString!=null)) { doSomething } and this if ( myString.length()>0) { doSomething } And in all cases my program doSomething in spite on the fact that my string IS EMPTY. It equals to null . So, what is wrong with that? ADDED: I found the reason of the problem. The variable was declared as a

Java ternary (immediate if) evaluation

烂漫一生 提交于 2019-11-26 11:20:53
问题 I can\'t find the relevant portion of the spec to answer this. In a conditional operator statement in Java, are both the true and false arguments evaluated? So could the following throw a NullPointerException Integer test = null; test != null ? test.intValue() : 0; 回答1: Since you wanted the spec, here it is (from §15.25 Conditional Operator ? :, the last sentence of the section): The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.

Booleans, conditional operators and autoboxing

佐手、 提交于 2019-11-26 11:13:30
Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean b = true ? returnsNull() : false; // NPE on this line. System.out.println(b); } public static Boolean returnsNull() { return null; } while this doesn't public static void main(String[] args) throws Exception { Boolean b = true ? null : false; System.out.println(b); // null } ? The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean --which isn't possible. But that isn't the question. The question is why ? Are there any references in JLS which