FutureBasic Logo

<<    Index    >> FutureBasic

Ternary Operator   conditional



Syntax
expression1 = ( conditional ) ? expression2 : expression3

Description
This is a two-part conditional operator with three (thus the 'Ternary' name) operands.
The Ternary operator can be used when you have a variable to which you want to assign one of two possible values.
It is a shorthand way to express a form of the if else statement.

When executing, conditional is evaluated. (note: conditional requires parenthesis around it)
If conditional is true (non-zero),expression2 is assigned to expression1
If conditional is false (zero), expression3 is assigned to expression1

Example

long max // store the maximum of a or b here
long a = 23
long b = 42
max = ( a > b ) ? a : b // parenthesis around conditional are required
print max

program output: 42

See also
if else