Java program to perform Logical and Relational operations

Relational Operators in Java

Relation operators are used to compare two quantities depending on their relation and depending upon relation it takes certain decision. The relational operators determine the relationship that one operand has with other operand. The expression used relational operator to control the if statement and the various loop statements.

OperatorDescriptionExampleExplanation
==Evaluates whether the operands are equal.x==yReturns true if the value are equal otherwise false.
!=Evaluates whether the operands are not equal.x!=yReturns true if value are not equal and false otherwise.
>Evaluates whether the left operand is greater than the right operand.x>yit is true if x is greater than y and false otherwise.
<Evaluates whether the Right operand is greater than the left operand.x<yReturns true if x is less than y and false otherwise.
>=Evaluates whether the left operand is greater than or equal to the right operand.x>=yReturns true if x is greater than or equal to y and false otherwise.
<=Evaluates whether the Right operand is greater than the left operand.x<=yReturns true if x is less than or equal to y and false otherwise.
Types of relation operators in java

Logical Operators in Java

logical operators are used when we want to form compound conditions by combing two or more relations

OperatorMeaningDescriptionExampleExplanation
&&Logical ANDEvaluates true if both the conditions are true and false otherwisex=6,y=7
x>5&&y<10
The result is true if condition1(x>5)and condition2(y<10)are both true.if one of them is false,resultof condition will be false, x=6,y=7 hence condition is true in this case
||Logical OrEvaluates true if at least one of the conditions are true and false otherwise if both the condition are falsex=5,y=11
x>5||y<10
The result is true if any one i.e condition1 (x>5)or condition2 (y<10)is true.if one of x=6,y=7condition1 holds true hence condition is true in this case
!Logical NOTIt inverts the resultx=true
y=!x
The value of x=true initially.
y=!x stores false
Logical operators in java

Java Program for Relational Operator

import java.lang.*;
import java.util.*;
public class Greater
{
 public static void main(String args[])
{
int a=100 ,b=20,c=50;
if(a>b && a>c)
System.out.println("A is Greater");
else if(b>a && b>c)
System.out.println("B is Greater");
else
System.out.println("B is Greater");
}
}
Output:
A is Greater

Types of Question can be asked in exam

  • What is relational and logical operators in java explain with java example?
  • Explain relational operators in java?
  • Explain logical operators in java?
  • Write a java program to demonstrate the use of relational and logical operators in java?
  • Explain type of relational and Logical operators

Leave a Comment