What does the question mark and the colon (?: ternary operator) mean in objective-c?
What does this line of code mean? label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect; The ? and : confuse me. 13 Answers 13
What does this line of code mean? label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect; The ? and : confuse me. 13 Answers 13
Can I write the if else shorthand without the else? var x=1; x==2 ? dosomething() : doNothingButContinueCode(); I’ve noticed putting null for the else works (but I have no idea why or if that’s a good idea). Edit: Some of you seem bemused why I’d bother trying this. Rest assured it’s purely out of curiosity. … Read more
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 … Read more
How is the conditional operator (? 🙂 used in Ruby? For example, is this correct? <% question = question.size > 20 ? question.question.slice(0, 20)+”…” : question.question %> 7 Answers 7
I saw this today in some PHP code: $items = $items ?: $this->_handle->result(‘next’, $this->_result, $this); I’m not familiar with the ?: operator being used here. It looks like a ternary operator, but the expression to evaluate to if the predicate is true has been omitted. What does it mean? 6 Answers 6
How can I use an inline if statement in JavaScript? Is there an inline else statement too? Something like this: var a = 2; var b = 3; if(a < b) { // do something } 17 Answers 17
In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator : int index = val > 0 ? val : -val Go doesn’t have the conditional operator. What is the most idiomatic way to implement the same piece of … Read more
In Perl (and other languages) a conditional ternary operator can be expressed like this: my $foo = $bar == $buz ? $cat : $dog; Is there a similar operator in VB.NET? 5 s 5 Depends upon the version. The If operator in VB.NET 2008 is a ternary operator (as well as a null coalescence operator). … Read more
Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b 14 s 14 When your first argument is null, they’re basically the same except that the … Read more
In simple words, what is the ?: (conditional, “ternary”) operator and how can I use it? 19 s 19 This is a one-line shorthand for an if-else statement. It’s called the conditional operator.1 Here is an example of code that could be shortened with the conditional operator: var userType; if (userIsYoungerThan18) { userType = “Minor”; … Read more