ternary-operator

C Language Operators [closed]

不打扰是莪最后的温柔 提交于 2019-12-14 03:30:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . #include <stdio.h> int main() { int a=-1?2:5 + 8?4:5; printf("%d\n",a); return 0; } The output of above program is 2. But why ? Please explain 回答1: Write human-readable and understandable code. ( Atleast, try to... ) int a=-1?2:5 + 8?4:5; is the same as int a = (-1) ? 2 : ( 5 + ( 8 ? 4 : 5) );

Easy way to understand nested ternary operators?

懵懂的女人 提交于 2019-12-14 02:42:09
问题 Is there a simple heuristic for understanding how to read nested ternary operators? I came across this in someone's source code and can't grok it. A simple ternary is easy: isRed = color == 'red' ? true : false But how do you read the following? Can I just line up the first with the last and the second with the second to last, or must I parse this into an if/else tree in my head? var offset = ( hasFrozenRows ) ? ( options.frozenBottom ) ? ( row >= actualFrozenRow ) ? ( h < viewportTopH ) ? (

What does this syntax ( page = $page ? $page : 'default' ) in PHP mean?

十年热恋 提交于 2019-12-14 01:19:33
问题 I'm new to PHP. I came across this syntax in WordPress. What does the last line of that code do? $page = $_SERVER['REQUEST_URI']; $page = str_replace("/","",$page); $page = str_replace(".php","",$page); $page = $page ? $page : 'default' 回答1: It's an example of the conditional operator in PHP. It's the shorthand version of: if (something is true ) { Do this } else { Do that } See Using If/Else Ternary Operators http://php.net/manual/en/language.operators.comparison.php . 回答2: That's the

PHP: Using the ternary operator for something else than assignments – valid use case?

旧巷老猫 提交于 2019-12-13 16:29:33
问题 Unfortunately I haven't found any official resource on this. Is it allowed to use the ternary operator like this, to shorten and if/else statement: (isset($someVar) ? $this->setMyVar('something') : $this->setMyVar('something else')); In the PHP documentation, the ternary operator is explained with this example: $action = (empty($_POST['action'])) ? 'standard' : $_POST['action']; This makes me believe that my use case might work, but it not really valid because a setter function does not

How to do “nothing” in the else part of ternary operator

核能气质少年 提交于 2019-12-13 06:01:35
问题 I want to filter values of a list based on whether or not they are contained in some other list. If an element is in the list I will select it, else I want to skip it or basically do nothing. Below is what I tried to do. How can I achieve this? List<string> sheetNames = new List<string>() {"1","10"}; List<string> projects= new List<string>() {"1","2","3","4","5","6","7"}; IEnumerable<string> result = sheetNames.Select(x => projects.Contains(x) ? x : /*Want to do nothing here */); 回答1: You can

corresponding nested ternary operator in php?

拥有回忆 提交于 2019-12-13 04:36:01
问题 I want to convert following if else condition to nested ternary operator. if ($projectURL) { echo $projectURL; } elseif ($project['project_url']) { echo $project['project_url']; } else { echo $project['project_id']; } I have written like following. echo ($projectURL)?$projectURL:($project['project_url'])?$project['project_url']: $project['project_id']; But it is found as not working properly.Is this not a right way? 回答1: Ternary operators are tricky thing in PHP, as they are left-associative

Could someone explain ternary operators in plain English or pseudocode?

夙愿已清 提交于 2019-12-13 00:44:53
问题 I don't understand the syntax used in the following lines, except that it follows a basic structure of what seems to be called a ternary operator. string path = args == null || args.Length == 0 ? @"C:\GENERIC\SYSTEM\PATH" : args[1]; I'm new to this syntax. Would someone help me translate it into real English (or pseudocode), much in the way an if statement can be turned into "if this then that"? EDIT: Thank you everyone for your answers, you've all been extremely helpful. Unfortunately I can

PHP Simplify a ternary operation

心已入冬 提交于 2019-12-12 19:31:02
问题 In PHP, is there a way to simplify this even more, without using an if() ? $foo = $bar!==0 ? $foo : ''; I was wondering if there was a way to not reassign $foo to itself if the condition is satisfied. I understand there is a way to do this in Javascript (using &&, right?), but was wondering if there was a way to do this in PHP. 回答1: Yup, you can use the logical and ( && ) operator in PHP as well. $bar === 0 && $foo = ''; 回答2: In PHP 5.3 the short form of the ternary operator has finally

Nested ternary operators

岁酱吖の 提交于 2019-12-12 14:26:29
问题 I have this code: _viewModel.PhoneDefault = user == null ? "" : (string.IsNullOrEmpty(user.PhoneDay) ? (string.IsNullOrEmpty(user.PhoneEvening) ? (string.IsNullOrEmpty(user.Mobile) ? "" : user.Mobile) : user.PhoneEvening) : user.PhoneDay); Is there a better way to write this to make it more readable? 回答1: In your case you can write a helper function, like this: // return the first parameter that isn't null or empty public static string CoalesceStrings(params string[] src) { foreach (var str

Conditional operator in C# and return types [duplicate]

白昼怎懂夜的黑 提交于 2019-12-12 13:43:20
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why does null need an explicit type cast here? Nullable types and the ternary operator. Why won't this work? Attempting to do the following: sqlCmd.Parameters.Add("@DateCreated", System.Data.SqlDbType.DateTime).Value = myObject.DateCreated == DateTime.MinValue ? DBNull.Value : myObject.DateCreated; I am getting this error: Type of conditional expression cannot be determined because there is no implicit