Questions tagged [conditional-operator]

The conditional operator is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is also commonly referred to as the ternary operator or inline if. Different languages have different syntax for the same construct, but all select between one of two options based on a condition.

The conditional operator is a that is part of the syntax for a basic conditional expression in several programming languages. It is also commonly referred to as inline if.

The basic format of the conditional operator is this:

condition ? first_expression : second_expression;

A number of languages have a ternary operator consisting of ? and :. "Conditional operator" is the proper name for this operator, at least in C and C-like languages.

Syntax examples:

MySQL

This is MySQL specific and not SQL standard:

IF(condition,value_when_true,value_when_false);

C / C++ / C# / Java / PHP

(condition) ? value_when_true : value_when_false ;

Python

value_when_true if condition else value_when_false

VB / VBA

IIF(condition, value_when_true, value_when_false)

The conditional operator is not the only ternary operator, so don't use that name for this operator. Any operator that takes 3 inputs (operands) is a ternary operator, like the SQL BETWEEN operator and the Python extended slice syntax.

1922 questions
7779
votes
32 answers

Does Python have a ternary conditional operator?

Is there a ternary conditional operator in Python?
Devoted
  • 177,705
  • 43
  • 90
  • 110
1318
votes
5 answers

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

How do I write an if-then-else statement in Python so that it fits on one line? For example, I want a one line version of: if count == N: count = 0 else: count = N + 1 In Objective-C, I would write this as: count = count == N ? 0 : count +…
Abizern
  • 146,289
  • 39
  • 203
  • 257
822
votes
34 answers

Kotlin Ternary Conditional Operator

What is the equivalent of this expression in Kotlin? a ? b : c This is not valid code in Kotlin.
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
725
votes
23 answers

Ternary operator (?:) in Bash

Is there a way to do something like this int a = (b == 5) ? c : d; using Bash?
En_t8
  • 7,595
  • 5
  • 20
  • 14
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
551
votes
14 answers

PHP short-ternary ("Elvis") 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
523
votes
20 answers

How do you use the ? : (conditional) operator in JavaScript?

What is the ?: (question mark and colon operator aka. conditional or "ternary") operator and how can I use it?
muudless
  • 7,422
  • 7
  • 39
  • 57
498
votes
16 answers

Is there a way to perform "if" in python's lambda?

In Python 2.6, I want to do: f = lambda x: if x==2 print x else raise Exception() f(2) #should print "2" f(3) #should throw an exception This clearly isn't the syntax. Is it possible to perform an if in lambda and if so how to do it?
Guy
  • 14,178
  • 27
  • 67
  • 88
493
votes
5 answers

Is there a conditional ternary operator in VB.NET?

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?
Jim Counts
  • 12,535
  • 9
  • 45
  • 63
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 }
364
votes
7 answers

?: operator (the 'Elvis operator') in PHP

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…
alpha_juno
  • 3,643
  • 2
  • 16
  • 6
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
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
290
votes
13 answers

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.
danielreiser
  • 5,292
  • 5
  • 31
  • 43
1
2 3
99 100