Saturday, 7 March 2015

Question Mark Operator In Java

Java includes a special three way operator that can replace if-else statement this operator works much like it does in C,C++ and C#.Its general form is shown below :
expression 1 ? expression 2 : expression 3
Here expression 1 is a boolean value.If expression 1 is true then expression 2 is evaluated otherwise expression 3 is evaluated.This program illustrate  the working of question mark operator

CODING:

class question
{
public static void main(String args[])
{
int i,k;
i=10;
//get absolute value of i
k=i < 0 ? -i:i;
System.out.println("Absolute Value of");
System.out.println(i+" is "+k);
i=-10;
//get absolute value of i
k=i < 0 ? -i:i;
System.out.println("Absolute Value of");
System.out.println(i+" is "+k);
}
}

OUTPUT:


No comments:

Post a Comment