18


The ? operator

The ? (ternary condition) operator is a more efficient form for expressing simple if statements. It has the following form:

expression1 ? expression2 : expression3

It simply states:

if expression1 is true then do expression2 ,in any other case do expression3

For example to assign the maximum of a and b to z:

z = (a>b) ? a: b;

which is the same as:

if (a>b) {

} else {

}


drago@scri.fsu.edu
Jan. 1997