Ternary operator - HiStructClient/femcad-doc GitHub Wiki

Ternary operator/ternary if

Syntax for basic conditional expressions. An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c.

 (condition) ? (true) : (false)

examples

A := 2
B := (A > 0) ? (True) : (False)

gives

B := true



A := [1, 2, 3, 4, 5, 6, 7]
B := A.Select( i => (i < 5) ? (0) : (i))

gives

B := [0, 0, 0, 0, 5, 6, 7]

For INEQUALITY condition, use != sign:

Result := (20 != 25) ? (True) : (False)

gives

Result := False

There can be more conditions chained one after another

 (condition 1) ? ((condition 2) ? (true 2) : ((condition 3) ? (true 3) : (false 3))) : (false 1)
 note:  e.g. false 2 is in the above code either true 3 or false 3

Code can be divided into several lines

Variable := (condition) ? (true result) : (false result) but in more complex syntax it is needed to put the whole expression into brackets ()

ResultExplanation := ( ( ResultCondition ) 

   ? ( "" ) 

   : ( " ( Result= "   + currentResultString + ", Expected= " + actualResultString + " )" ) )

Order of condition evaluation

Ternary operator uses for multiple conditions "lazy" evaluation. That means that it evaluates conditions "from left". And if there is a possibility to do fast evaluation, without knowing the right side evaluations, it does so. Typically:

TRUE or whatever = TRUE
FALSE and  whatever  = FALSE
⚠️ **GitHub.com Fallback** ⚠️