conditional-operator

“or” conditional in Python troubles [duplicate]

ぐ巨炮叔叔 提交于 2019-11-27 05:10:13
This question already has an answer here: How to test multiple variables against a value? 21 answers I'm learning Python and I'm having a little bit of a problem. Came up with this short script after seeing something similar in a course I'm taking. I've used "or" with "if" before with success (it doesn't show much here). For some reason I can't seem to get this working: test = raw_input("It's the flying circus! Cool animals but which is the best?") x = test.lower() if x == "monkey" or "monkeys": print "You're right, they are awesome!!" elif x != "monkey" or "monkeys": print "I'm sorry, you're

Type result with conditional operator in C#

喜欢而已 提交于 2019-11-27 05:06:24
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 = DateTime.Now; OutputDateTime(IsDateTimeHappy(myDateTime) ? null : myDateTime); Console.ReadLine(); ^ } | } |

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

元气小坏坏 提交于 2019-11-27 04:58:58
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 usual pattern to suit the need to read either from a file or from a std::istringstream . To my surprise it

Java ternary (immediate if) evaluation

假如想象 提交于 2019-11-27 04:46:06
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; Michael Myers 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. Michał Króliczek I know it is old post, but look at very similar case and then vote me :P

Can Java's ternary/conditional operator (?:) be used to call methods instead of assigning values?

天涯浪子 提交于 2019-11-27 04:36:15
In pages like http://en.wikipedia.org/wiki/?: the ternary/conditional operator ?: seems to be used for conditional assignments. I tried to use it for method calling, like this: (condition) ? doThis() : doThat(); Both methods return void. Java tells me it is not a statement. So, I'm guessing I can't do conditional method calling... or can I? Think of ternary operators like a method in this case. Saying a ? b : c is (for the intents and purposes you're considering, see lasseespeholt's comment) equivalent to calling the pseudocode method: ternary(a, b, c) if a return b else return c which is why

In C# why can't a conditional operator implicitly cast to a nullable type

限于喜欢 提交于 2019-11-27 04:29:06
I am curious as to why an implicit cast fails in... int? someValue = SomeCondition ? ResultOfSomeCalc() : null; and why I have to perform an explicit cast instead int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null; It seems to me that the compiler has all the information it need to make an implicit casting decision, no? The relevant section of the C# 3.0 spec is 7.13, the conditional operator: The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then, If X and Y are the same

Short circuiting statement evaluation — is this guaranteed? [C#]

泪湿孤枕 提交于 2019-11-27 03:51:26
问题 Quick question here about short-circuiting statements in C#. With an if statement like this: if (MyObject.MyArray.Count == 0 || MyObject.MyArray[0].SomeValue == 0) { //.... } Is it guaranteed that evaluation will stop after the "MyArray.Count" portion, provided that portion is true? Otherwise I'll get a null exception in the second part. 回答1: Yes, this is guaranteed. C# Language Specification - 7.11 Conditional logical operators: The && and || operators are called the conditional logical

Ternary operator in PowerShell

不羁的心 提交于 2019-11-27 03:12:44
From what I know, PowerShell doesn't seem to have a built-in expression for the so-called ternary operator . For example, in the C language, which supports the ternary operator, I could write something like: <condition> ? <condition-is-true> : <condition-is-false>; If that doesn't really exist in PowerShell, what would be the best way (i.e. easy to read and to maintain) to accomplish the same result? fbehrens $result = If ($condition) {"true"} Else {"false"} Everything else is incidental complexity and thus to be avoided. For use in or as an expression, not just an assignment, wrap it in $() ,

Check if int is between two numbers

瘦欲@ 提交于 2019-11-27 02:31:13
问题 Why can't do you this if you try to find out whether an int is between to numbers: if(10 < x < 20) Instead of it, you'll have to do if(10<x && x<20) which seems like a bit of overhead. 回答1: One problem is that a ternary relational construct would introduce serious parser problems: <expr> ::= <expr> <rel-op> <expr> | ... | <expr> <rel-op> <expr> <rel-op> <expr> When you try to express a grammar with those productions using a typical PGS, you'll find that there is a shift-reduce conflict at the

JavaScript if alternative [duplicate]

我的梦境 提交于 2019-11-27 02:09:36
This question already has an answer here: Question mark and colon in JavaScript 7 answers What does this bit of code represent? I know it's some kind of if alternative syntax... pattern.Gotoccurance.score != null ? pattern.Gotoccurance.score : '0' Update: What's the need for this sort of coding? Is this more efficient or just a shortened version with the same efficiency? CMS It is the conditional operator, it is equivalent to something like this: if (pattern.Gotoccurance.score != null) { pattern.Gotoccurance.score; } else { '0'; } But I think that an assignment statement is missing in the code