contents   index   previous   next



Conditional operator

 

The conditional operator, "? :", provides a shorthand method for writing if statements. It is harder to read than conventional if statements, and so is generally used when the expressions in the if statements are brief. The syntax is:

 

test_expression ? expression_if_true : expression_if_false

 

First, test_expression is evaluated. If test_expression is nonzero, true, then expression_if_true is evaluated, and the value of the entire expression replaced by the value of expression_if_true. If test_expression is false, then expression_if_false is evaluated, and the value of the entire expression is that of expression_if_false.

 

The following fragment illustrates the use of the conditional operator.

 

foo = ( 5 < 6 ) ? 100 : 200; // foo is set to 100

Screen.write("Name is " + ((null==name) ? "unknown" : name));

 

 


Exception handling