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.

1768 questions
592
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
  • 51,256
  • 26
  • 134
  • 147
559
votes
15 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
  • 12,486
  • 9
  • 44
  • 62
377
votes
17 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 }
363
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
  • 4,051
  • 2
  • 14
  • 19
332
votes
8 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
  • 15,755
  • 15
  • 55
  • 72
330
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
  • 49,883
  • 70
  • 181
  • 236
309
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,664
  • 2
  • 15
  • 14
292
votes
17 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
  • 17,117
  • 17
  • 63
  • 86
241
votes
8 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,212
  • 3
  • 20
  • 28
206
votes
12 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,541
  • 4
  • 18
  • 21
196
votes
17 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
  • 13,563
  • 35
  • 109
  • 174
147
votes
7 answers

What is a Question Mark "?" and Colon ":" Operator Used for?

Two questions about using a question mark "?" and colon ":" operator within the parentheses of a print function: What do they do? Also, does anyone know the standard term for them or where I can find more information on their use? I've read that…
Deepend
  • 4,057
  • 17
  • 60
  • 101
113
votes
5 answers

Angularjs if-then-else construction in expression

Can I somehow use if-then-else construction (ternary-operator) in angularjs expression, for example I have function $scope.isExists(item) that has to return bool value. I want something like this,
111
votes
2 answers

Ternary operator in Java only evaluating one expression since Java 7 - was that different in Java 1.6 and lower?

Preparing for the Oracle Certified Associate Java SE 8 Programmer 1 exam, I came across the following paragraph about the ternary expression in the official Study Guide: Ternary Expression Evaluation As of Java 7, only one of the right-hand…
Mathias Bader
  • 3,585
  • 7
  • 39
  • 61
1
2 3
99 100