Get Adobe Flash player

Saturday, January 14, 2012

Ternary Operator

The ternary operator ?, operates on three operands. This operator can replace simple if-else statements. The general form of using this operator os:

expression1 ? expression2 : expression 3 

The expression1 always should result in a Boolean value TRUE or FALSE. If the value is TRUE then expression2 will executed otherwise expression3 will executed.

For example:

class TernaryOpe{
public static void main(String args[]){
int mark=77;
String result;
result=(mark>40)? “Pass” : “Fail”;
System.out.println(“Your mark = ”+mark);
System.out.println(“And your result is : ”+result);




}
}


The above program gives the following output:



Your mark = 77

And your result is : Pass

0 comments:

Post a Comment