Conditional Operator
The conditional operator is a ternary operator (it has three operands) and is used
to evaluate boolean expressions, much like an if statement except instead of
executing a block of code, if the test is true, a conditional operator will assign a
value to a variable. In other words, the basic purpose of the conditional operator is to decide
which of two values to assign to a variable. This operator is constructed using a ?
(question mark) and a : (colon). The parentheses are optional. Its structure is:
x = (boolean expression) ? value to assign if true : value to assign if false
Let's take a look at a conditional operator in code:
class Employee {
public static void main(String [] args) {
int pets = 3;
String status = (pets<4) sizeofyard =" 12;" numofpets =" 4;" status =" (pets<4)?"> 8)? "Pet limit on the edge"
:"too many pets";
System.out.println("Pet status is " + status);
}
remember that conditional operators are sometimes confused with assertion statements, so be
certain you can tell the difference.
Useful Links:-
Assignment Operators
Arithmetic Operators
Logical operators
Relational Operators
Bitwise Operator Intro
Bitwise Logical operator
Bitwise Left shift Operator
Bitwise Right shift Operator
Sponsored Links :-
Get Your SCJP preparation Kit free!!!!!!!
Get Your SCWCD preparation Kit free!!!
Are you Intelligent check here


















0 comments:
Post a Comment
Post a Comment