Ternary Operator

The PSL ternary operator ?: behaves similarly to the C conditional expression in that it connects three operands and offers an alternative way of expressing a simple if...else statement. The format for the PSL conditional expression is as follows: 

result = expression1 ? expression2 : expression3;

If expression1 is TRUE (nonzero), then expression2 is evaluated; otherwise, expression3 is evaluated. The value for the complete conditional expression is the value of either expression2 or expression3, depending on which expression was evaluated. The value of the expression may be assigned to a variable.

Conditional expressions are most useful in replacing short, simple if...else statements. For example, the if...else statement 

if (x==1) {
else {

can be replaced with the following one-line conditional statement: 

y = (x==1) ? 10 : 20;

These examples perform the same function. If x is 1, then y becomes 10 else y becomes 20.s


If ternary operator is nested in PSL, on the same line, then PSL interpreter continues
to evaluate the next ternary operator and overrides result of the first one.
For example, the result of %PSL print( 1 == 1 ? "10" : 2 == 2 ? "20" :
"30"); will be 20.
To avoid this, use brackets. Brackets takes evaluation precedence.
For example, %PSL print( 1 == 1 ? "10" : (2 == 2 ? "20" : "30")); 

Where to go from here

PSL Operators

Was this page helpful? Yes No Submitting... Thank you