ternary-operator

If statement with ? and : [duplicate]

半腔热情 提交于 2019-12-01 17:18:42
This question already has an answer here: What does the question mark and the colon (?: ternary operator) mean in objective-c? 13 answers I heard about a kind of If statement which use ? and : in C I dont know how to use it and I cant find anything about it. I need to use it in order to shorten my code any help would be appreciated. ?: is ternary operator in C (also called conditional operator). You can shorten your code like if(condition) expr1; else expr2; to condition ? expr1 : expr2; See how it works: C11: 6.5.15 Conditional operator: The first operand is evaluated; there is a sequence

Ternary Operator and unexpected NullPointerException

倾然丶 夕夏残阳落幕 提交于 2019-12-01 17:09:50
问题 I am getting NullPointerException from the below line sometimes. System.out.println("Date::"+ row != null ? row.getLegMaturityDate() : "null"); After adding brackets, it is fine. System.out.println("Date::"+ (row != null ? row.getLegMaturityDate() : "null")); Please clarify me the behavior. Thanks in advance. 回答1: "Date::" + row is never null, although row sometimes is. That is, "Date::"+ row != null is equivalent to ("Date::"+ row) != null which is always true. 回答2: It's a matter of operator

Ternary expression with “defined?” returns “expression” instead of value?

怎甘沉沦 提交于 2019-12-01 16:51:51
I'm pretty new to Ruby and Rails but even after searching stack overflow and google I couldn't find an answer to this. I've got a simple Ruby shorthand if statement that should return an integer like so: # in the context of this erb document `amount` is defined as 5. @c = ( defined? amount ? amount : r( 1,4 ) ) r() is a custom helper function that returns a random number between in this case 1 and 4. The way I intend this to work is that if amount is defined, then use the number defined as amount , else generate a random number between 1 and 4 and use that instead. When printing out @c however

Ternary expression with “defined?” returns “expression” instead of value?

感情迁移 提交于 2019-12-01 16:35:49
问题 I'm pretty new to Ruby and Rails but even after searching stack overflow and google I couldn't find an answer to this. I've got a simple Ruby shorthand if statement that should return an integer like so: # in the context of this erb document `amount` is defined as 5. @c = ( defined? amount ? amount : r( 1,4 ) ) r() is a custom helper function that returns a random number between in this case 1 and 4. The way I intend this to work is that if amount is defined, then use the number defined as

Why ternary operator in swift is so picky?

大城市里の小女人 提交于 2019-12-01 15:28:06
The question is very simple, but I just could not find the answer! Why doesn't return x == 0? "" : "Hello" compile but return x == 0 ? "" : "Hello" does? This is really weird because all the other operators don't need an extra white space. e.g. let x = 1+1 let y = 1 + 1 are the same. I think it has something to do with optionals. But when you use a ? operator on a variable, it must be used like this: let s: String? = nil let x = s?.startIndex I mean it must follow another operator, right? I think it has something to do with optionals. It does. The documentation on operators says: There is one

Bizarre ternary operator behavior in debugger on x64 platform

走远了吗. 提交于 2019-12-01 15:14:29
I'm using a very simple ternary expression in my C# code: helperClass.SomeData = helperClass.HasData ? GetSomeData() : GetSomeOtherData(); In both cases, the functions on each path of the expression return a non-null object, but if I look at the result in the debugger, it is null until I reference it in the code such as using an assert: Debug.Assert(helperClass.SomeData != null); This only appears to happen if I use an "x64" or "Any CPU" platform setting in Debug mode. It's fine in "x86" mode. I try to be very cautious before assuming I've found a bug in the compiler or debugger, but I can't

Multiple conditions in the ternary operator safe?

我们两清 提交于 2019-12-01 15:04:30
I have seen advice that says the ternary operator must not be nested. I have tested the code below and it works okay. My question is, I haven't seen the ternary operator used like this before. So, is this as reliable as it were used in an if or could something like this come and bite me later(not in terms or readability, but by failing). $rule1 = true; $rule2 = false; $rule3 = true; $res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true)) ? true : false; if($res) { echo "good"; } else { echo "fail"; } Thanks! If the results you are returning from the ternary operator are only "true"

How to make quick judgement and assignment without isset()?

旧时模样 提交于 2019-12-01 14:23:01
I am tired of using code like: $blog = isset($_GET['blog']) ? $_GET['blog'] : 'default'; but I can't use: $blog = $_GET['blog'] || 'default'; Is there any way to do this without using isset() ? You have to wait for the next version of PHP to get the coalesce operator // Uses $_GET['user'] if it exists -- or 'nobody' if it doesn't $username = $_GET['user'] ?? 'nobody'; // Loads some data from the model and falls back on a default value $model = Model::get($id) ?? $default_model; Write a helper function. function arrayValue($array, $key, $default = null) { return isset($array[$key]) ? $array[

Bizarre ternary operator behavior in debugger on x64 platform

夙愿已清 提交于 2019-12-01 14:07:31
问题 I'm using a very simple ternary expression in my C# code: helperClass.SomeData = helperClass.HasData ? GetSomeData() : GetSomeOtherData(); In both cases, the functions on each path of the expression return a non-null object, but if I look at the result in the debugger, it is null until I reference it in the code such as using an assert: Debug.Assert(helperClass.SomeData != null); This only appears to happen if I use an "x64" or "Any CPU" platform setting in Debug mode. It's fine in "x86" mode

Multiple conditions in the ternary operator safe?

℡╲_俬逩灬. 提交于 2019-12-01 13:54:13
问题 I have seen advice that says the ternary operator must not be nested. I have tested the code below and it works okay. My question is, I haven't seen the ternary operator used like this before. So, is this as reliable as it were used in an if or could something like this come and bite me later(not in terms or readability, but by failing). $rule1 = true; $rule2 = false; $rule3 = true; $res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true)) ? true : false; if($res) { echo "good"; }