Logical Operators
To check multiple conditions
at one time we need logical operator. These operators also known as
short-circuit logical operators.
&&
|
Logical AND
|
expr1 && expr2
|
True if both expression are true else
return false
|
||
|
Logical OR
|
expr1 && expr2
|
True if either expression is true, return false if both expressions are false
|
!
|
Logical NOT
|
!expr1
|
Return
true if expr1 is NOT true or else return false.
|
AND (&& Operator)
A multiple relational
expression can be combine using && operator. Return true if all the
condition are true or else return false if any one of the condition failed.
OR (|| Operator)
A multiple relational
expression can be combine using || operator. Return true if any one of the
condition is true or else return false if none of the condition satisfied.
Not (! Operator)
Not Operator used with any
relational expression or logical expression. It is also used with a
boolean variable. It is a compliment operator.
Example
public class Logical_Operator
{
public static void main(String
args[])
{
Logical_Operator op=new
Logical_Operator();
op.ANDOP();
op.OROP();
op.NOTOP();
}
void ANDOP()
{
int a=10,b=20,c=30;
System.out.println("\nLogical AND\n");
if(a>b &&
a>c)
{
System.out.println(a+" is bigger ");
}
if(b>a &&
b>c)
{
System.out.println(b+" is bigger ");
}
if(c>a &&
c>b)
{
System.out.println(c+" is bigger ");
}
}
void OROP()
{
int age=55;
System.out.println("\nLogical OR\n");
if(age<18 age="">60)18>
{
System.out.println(" Not elligible for the job ");
}
}
void NOTOP()
{
boolean t1 =true;
boolean t2=false;
System.out.println("\nLogical NOT\n");
if(t1)
{
System.out.println(" You hit the bulls eye ");
}
if(!t2)
{
System.out.println(" You failed to hit the bulls eye ");
}
}
}
Output
Logical AND
30 is bigger
Logical OR
Logical NOT
You hit the bulls eye
You failed to hit the bulls eye
No comments:
Post a Comment