[prev] 69 [next]

Conditional Expressions

Encapsulates a common usage for an if:

if (cond) {
   v = Expr1;
} else {
   v = Expr2;
}

can be written as

v = cond ? Expr1 : Expr2;

Examples:

x = (y > 0) ? z+1 : z-1;
or
x = z + ((y > 0) ? 1 : -1);