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 ternary-operator 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.