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);
}
}
No comments:
Post a Comment