Language - Conditional operator (ternary)

Card Puncher Data Processing

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:

  • the first operand expression must be of type boolean or Boolean
  • the second or the third operand expression must not be an invocation of a void method.

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:

  • If the value of the first operand is true, then the second operand expression is chosen.
  • If the value of the first operand is false, then the third operand expression is chosen.

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





Discover More
Model Funny
Function - Arity

The number of argument in a function signature is called the arity 0-ary - - 0 argument 1-ary - - 1 argument 2-ary - - two arguments 3-ary - Ternary - three arguments. Example: three-valued...
Java Conceptuel Diagram
Java - Flow

if then else The return statement exits from the current method, and control flow returns to where the method was invoked.
Java Conceptuel Diagram
Java - Operator

Operators Precedence postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND...
Javascript - Flow (Control Structures)

Flow / Conditional expression in Javascript in Javascript See loop JavaScript_basics
Card Puncher Data Processing
Language - Control Flow Statement (If, Switch, For) - Conditional Operators - Execution order - Branch Conditionals (aka decisions)

conditional are expressions that: determines the next statement to execute by evaluating a comparison expression Ultimately, they are predicate that returns a boolean. They model a flow of statements....
Card Puncher Data Processing
Python - Comprehension (Iteration)

Comprehension are a way to loop over elements of: a , , , tuple, , or . Comprehensions permit to build collections out of other collections. Comprehensions allow one to express computations...
React - Rendering (Lifecycle) Mount and Unmount

This page is rendering in React (ie update of the DOM) To make the UI interactive, you need to be able to trigger changes to your underlying data model. The root element is the element passed to...



Share this page:
Follow us:
Task Runner