JAVA- Conditional operator statement

The conditional operator statement is used with the combination of ? and : and this is known as conditional operator.

Syntax:                         conditional expression ? expression1 : expression2

The conditional expression is evaluated first and if the result is nonzero then the expression1 is returned as the value of the expression, otherwise the value of expression2 is returned.

For example:

If(a<1)

t=0;

else

t=1;

Here the above code can be written as:

t = (a<1) ? 0:1;

Leave a Reply

Your email address will not be published. Required fields are marked *