Download presentation
Presentation is loading. Please wait.
1
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.
2
Relational Operators & Operations
Usage pattern Example < Strictly less than x < y 100 < 200 <= Less than or equal to x <= y 100 <= 200 > Strictly greater than x > y 200 > 100 >= Greater than or equal to x >= y 200 >= 100 == Is equal to x == y 200 == 100 != Not equal to x != y 200 != 100
3
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 > 15 ‘a’ != ‘A’
4
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. x / y < z x%y >= x - y/x - z
5
Evaluate Relational Expression: x / y < z
int x = 10, y = 20; double z = 5.0; x / y < z true
6
Evaluate Relational Expression: x%y - 10 >= x - y/x - z
int x = 10, y = 20; double z = 5.0; x % y >= x y / x z 3.0 false
7
Evaluate Relational Expression: x%y – (10 >= x ) - y/x - z
int x = 10, y = 20; double z = 5.0; X % y – ( 10 >= x ) - y/x - z
8
Evaluate Relational Expression: x%y – (10 >= x ) - y/x - z
int x = 10, y = 20; double z = 5.0; x % y ( 10 >= x ) y / x z true ????
9
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 > 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.
10
Logical Operator and Operations
Java has three logical operators: logical-AND ( && ) – 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.
11
Evaluate Logical Expression: x + y > z && x – y < z
int x = 10, y = 20; double z = 5.0; x y > z && x – y < z true true true
12
Evaluate Logical Expression
int x = 10, y = 20; double z = 5.0; x y > z || x – y < z false true true
13
Evaluate Logical Expression: !(x + y > z && x – y < z)
int x = 10, y = 20; double z = 5.0; !(x y > z && x – y < z) true true true false
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.