Table of Contents

Language - Conditional operator (ternary)

About

The ternary is a:

Many people call it the ternary operator, because it's the only ternary (three operand) operator in many language.

The first expression must be of type boolean or Boolean, or a compile-time error occurs.

Ternary means that the number of argument (arity) of this operator is three. See Arity

Syntax

The conditional operator has three operand expressions:

firstBooleanOperand ? SecondOperand : ThirdOperand

where:

At run time, the first operand expression of the conditional expression is evaluated first. The resulting boolean value is then used to choose either the second or the third operand expression:

Example:

String myName = mustBeShortName ? getShortName() : getLongName();

Associativity

The conditional operator is syntactically right-associative (it groups right-to-left).

a?b:c?d:e?f:g

means the same as

a?b:(c?d:(e?f:g))

Specification

Documentation / Reference