Questions tagged [ternary-operator]

A ternary operator is any operator that takes three arguments. For the ternary conditional operator " ? :" use the tag [tag:conditional-operator]. Also include the appropriate language tag.

A ternary operator is any operator that takes three arguments, as opposed to the usual two (binary operator) or one (unary operator).

The phrase "ternary operator" often refers to the most-commonly-used ternary operator, the , often denoted condition ? true_value : false_value. For questions specific to that operator, use instead.

Other ternary operators

As "Other ternary operators besides ternary conditional (?:)" determined, there are other ternary operators in various languages. These include the BETWEEN...AND operator in SQL, chained conditionals like x < y < z in Python and Common Lisp, and the slice operator start : stop : step in Python.

1799 questions
6670
votes
27 answers

Does Python have a ternary conditional operator?

If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
Devoted
  • 90,341
  • 41
  • 85
  • 110
1048
votes
5 answers

Putting a simple if-then-else statement on one line

I'm just getting into Python and I really like the terseness of the syntax. However, is there an easier way of writing an if-then-else statement so it fits on one line? For example: if count == N: count = 0 else: count = N + 1 Is there a…
Abizern
  • 129,329
  • 36
  • 198
  • 252
458
votes
1 answer

How to use the ternary operator inside an interpolated string?

I'm confused as to why this code won't compile: var result = $"{fieldName}{isDescending ? " desc" : string.Empty}"; If I split it up, it works fine: var desc = isDescending ? " desc" : string.Empty; var result = $"{fieldName}{desc}";
Nate Barbettini
  • 43,095
  • 21
  • 121
  • 135
411
votes
13 answers

PHP ternary operator vs null coalescing operator

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
balping
  • 5,859
  • 2
  • 16
  • 31
377
votes
10 answers

What is the idiomatic Go equivalent of C's ternary operator?

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…
Fabien
  • 9,808
  • 7
  • 37
  • 61
328
votes
7 answers

Ternary operation in CoffeeScript

I need to set value to a that depends on a condition. What is the shortest way to do this with CoffeeScript? E.g. this is how I'd do it in JavaScript: a = true ? 5 : 10 # => a = 5 a = false ? 5 : 10 # => a = 10
evfwcqcg
  • 13,825
  • 14
  • 51
  • 69
325
votes
14 answers

How to write an inline IF statement in JavaScript?

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 }
314
votes
7 answers

How do I use the conditional operator (? :) in Ruby?

How is the conditional operator (? :) used in Ruby? For example, is this correct? <% question = question.size > 20 ? question.question.slice(0, 20)+"..." : question.question %>
Mithun Sreedharan
  • 45,549
  • 69
  • 171
  • 232
297
votes
8 answers

Omitting the second expression when using the if-else shorthand

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…
Nikki
  • 3,544
  • 2
  • 13
  • 14
265
votes
14 answers

Ternary operator in PowerShell

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: ? :…
mguassa
  • 3,043
  • 2
  • 11
  • 18
238
votes
7 answers

Ternary operator in AngularJS templates

How do you do a ternary with AngularJS (in the templates)? It would be nice to use some in html attributes (classes and style) instead of creating and calling a function of the controller.
cricardol
  • 4,211
  • 3
  • 15
  • 28
231
votes
15 answers

Short form for Java if statement

I know there is a way for writing a Java if statement in short form. if (city.getName() != null) { name = city.getName(); } else { name="N/A"; } Does anyone know how to write the short form for the above 5 lines into one line?
Makky
  • 15,166
  • 17
  • 58
  • 81
210
votes
5 answers

One-line list comprehension: if-else variants

It's more about python list comprehension syntax. I've got a list comprehension that produces list of odd numbers of a given range: [x for x in range(1, 10) if x % 2] This makes a filter - I've got a source list, where I remove even numbers (if x %…
ducin
  • 22,544
  • 38
  • 129
  • 239
197
votes
11 answers

inline conditionals in angular.js

I was wondering if there is a way in angular to conditionally display content other than using ng-show etc. For example in backbone.js I could do something with inline content in a template like: <% if (myVar === "two") { %> show this<% } %> but in…
user1469779
  • 2,451
  • 4
  • 15
  • 21
173
votes
16 answers

What is the Java ?: operator called and what does it do?

I have been working with Java a couple of years, but up until recently I haven't run across this construct: int count = isHere ? getHereCount(index) : getAwayCount(index); This is probably a very simple question, but can someone explain it? How do…
mainstringargs
  • 11,482
  • 33
  • 101
  • 163
1
2 3
99 100