# 12634571 ```Relational Operator and Operations
•
There are six binary relational operators in Java.
•
They are used to form conditional expressions.
•
They are used with primitive numeric and the character types only.
Relational Operators &amp; Operations
Operator
Operation
Usage pattern
Example
&lt;
Strictly less than
x&lt; y
100 &lt; 200
x &lt;= y
100 &lt;= 200
&lt;=
Less than or equal to
&gt;
Strictly greater than
x&gt;y
200 &gt; 100
&gt;=
Greater than or equal to
x &gt;= y
200 &gt;= 100
==
Is equal to
x == y
200 == 100
!=
Not equal to
x != y
200 != 100
Relational expression
•
Relational expression is a combination of two primitive types separated by a
relational operator.
•
The result from evaluating the expression is one of the boolean values, true or
false.
•
The general form of simple conditional statements is:
•
Left_Operand Relational_Operator Right_Operand
Example:
• 10 &gt; 15
• ‘a’ != ‘A’
Evaluating Relational Expressions
•
•
•
When evaluating relational expressions, you must:
First resolve all arithmetic expressions to get the primitive values.
Then compare the primitive values as specified by the relational operator
Example: Use the following declaration to evaluate the relational expressions
int x = 10, y = 20;
double z = 5.0;
Evaluate the following relational expressions.
1. x / y &lt; z
2. x%y - 10 &gt;= x - y/x - z
Evaluate Relational Expression: x / y &lt; z
int x = 10, y = 20;
double z = 5.0;
x
/
y
0
true
&lt;
z
Evaluate Relational Expression: x%y - 10 &gt;= x - y/x - z
int x = 10, y = 20;
double z = 5.0;
x
%
y
-
10 &gt;=
x
-
10
y
/
x
-
2
0
8
3.0
false
z
Evaluate Relational Expression: x%y – (10 &gt;= x ) - y/x - z
int x = 10, y = 20;
double z = 5.0;
X % y – ( 10 &gt;= x ) - y/x - z
Evaluate Relational Expression: x%y – (10 &gt;= x ) - y/x - z
int x = 10, y = 20;
double z = 5.0;
x
%
y
10
-
( 10 &gt;=
true
???
?
x)
-
y
/
x
-
z
Evaluating Relational Expressions
•
The value from a relational expression can be assigned to a Boolean variable.
The format is:
boolean id = relational_expression.
For example :
int x = 120;
double y = 20;
boolean flag = 2 * x &gt; y + 2;
•
Note:
 The relational expression must be evaluated first.
 The resulting value is assigned to the Boolean variable flag.
 In this case the value true is assigned to the variable flag.
Logical Operator and Operations
•
Java has three logical operators:
 logical-AND ( &amp;&amp; ) – Determines the validity of two relational expressions
 logical-OR ( || ) - Determines the validity of one or both two relational
expressions
 logical-NOT ( ! ) – Negates the result of a boolean value
•
This means that the relational expressions must first be evaluated, before
logical operators can be applied.
Evaluate Logical Expression: x + y &gt; z &amp;&amp; x – y &lt; z
int x = 10, y = 20;
double z = 5.0;
x
+
y
&gt;
z
&amp;&amp;
30
x
–
y
-10
true
true
true
&lt;
z
Evaluate Logical Expression
int x = 10, y = 20;
double z = 5.0;
x
y
&gt;
z
||
x
-10
–
y
&lt;
-10
false
true
true
z
Evaluate Logical Expression: !(x + y &gt; z &amp;&amp; x – y &lt; z)
int x = 10, y = 20;
double z = 5.0;
!(x
+
y
&gt;
z
&amp;&amp;
30
x
–
y
-10
true
true
true
false
&lt;
z)
```