Forum

Number Of Page View:100,000+/Months
LOGIN |New User

Reply

Conditional Operator
The condition operator is is a bassicaly a tenory oprator its mainly used for the boolean expressions, its semilar as a if statement but some diff its not executing a block of code if the test is true, a conditional operator will assign a value to a variable, we can also say the goal of the conditional operator is to decide which of two values to assign to a variable.
someVariable = (boolean expression) ? value to assign if true : value to assign if false
        We have a some code for the conditional operator : 
class Salary {
public static void main(String [] args) {
int numOfPets = 3;
String status = (numOfPets<4)?"Pet limit not exceeded":"too many pets";
System.out.println("This pet status is " + status);
}
}
           A conditional operaor starts with the boolean operator followed by two possible
values for the variable to the left of the conditional operator.The first value (the one
to the left of the colon) is assigned if the conditional (boolean) test is true, and the
second value is assigned if the conditional test is false. You can even nest conditional
operators into one statement.
class AssignmentOps {
public static void main(String [] args) {
int sizeOfYard = 10;
int numOfPets = 3;
String status = (numOfPets<4)?"Pet count OK"
:(sizeOfYard > 8)? "Pet limit on the edge"
:"too many pets";
System.out.println("Pet status is " + status);
}
}
Posted By: Name:Rajesh Kr URL: Conditional Operator