Java Reference
In-Depth Information
Chapter 8
Other Java Operators
Logical Operations
The relational operators, described in the Chapter 7, are used to evaluate
whether a condition relating two operands is true or false. However, by
themselves, they serve only to test simple relationships. In programming,
you often need to determine complex conditional expressions. For exam-
ple,todetermineifauserisateenageryou testwhetherthepersonisolder
than twelve years and younger than twenty years.
The logical operators allow combining two or more conditional state-
ments into a single expression. As is the case with relational expressions,
expressionsthatcontainlogicaloperatorsreturntrueorfalse. Table8-1
lists the Java logical operators.
Table 8-1
Java Logical Operators
OPERATOR
ACTION
&&
logical AND
||
logical OR
!
logical NOT
For example, if a =6, b =2,and c = 0, the boolean variable result evalu-
ates to either true or false, as follows:
boolean result;
inta=6;
intb=2;
intc=0;
result=a>b&&c==0; //Case 1 - result is true
result=a>b&&c!=0; //Case 2 - result is false
result=a==0||c==0; //Case 3 - result is true
 
Search WWH ::




Custom Search