Advertisement
<boolean-expression> && <boolean-expression>
class A
{
public static void main(String... ar)
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 && a>10) //a is less than 15 and a is greater than 10
System.out.println("a<15 && a>10, is true");
if(b<25 && b>19) //b is less than 25 and a is greater than 19
System.out.println("b<25 && b>19, is true");
if(ch1=='a' && ch2=='b') //ch1 has 'a' and ch2 has 'b'
System.out.println("ch1=='a' && ch1=='b', is true");
if(d>2 && ch2=='b') //d is greater than 2 and value in ch2 has 'b', is true
System.out.println("d>2 && ch2==b, is true");
if(a<=10 && d<=2.5) //a is less than or equal to 10 and d is less than or equal to 2.5
System.out.println("a<=10 && d<=2.5, is true");
if(a<10 && ch1=='a') //a is less than 10 and ch1 has 'a'
System.out.println("a<=10 and b>=20, is true");
}
}
b<25 && b>19, is true
ch1=='a' && ch1=='c', is true
d>2 && ch2=='b', is true
a<=10 && d<=2.5, is true
Advertisement
<boolean-expression> || <boolean-expression>
class A
{
public static void main(String... ar)
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 || a>10) //a is less than 15 or a is greater than 10
System.out.println("a<15 || a>10, is true");
if(ch1=='a' || ch2=='b') //ch1 has 'a' or ch2 has 'c'
System.out.println("ch1=='a' || ch1=='b', is true");
if(b<25 || b>19) //b is less than 25 or a is greater than 19
System.out.println("b<25 || b>19, is true");
if(d>2 || ch2=='b') //d is greater than 2 or value in ch2 has 'b', is true
System.out.println("d>2 || ch2==b, is true");
if(a<=10 || d<=2.5) //a is less than or equal to 10 or d is less than or equal to 2.5
System.out.println("a<=10 || d<=2.5, is true");
if(a<10 || ch1=='a') //a is less than 10 or ch1 has 'a'
System.out.println("a<10 || ch1=='a', is true");
}
}
a<15 || a>10, is true
ch1=='a' || ch1=='c', is true
b<25 || b>19, is true
d>2 || ch2=='b', is true
a<=10 || d<=2.5, is true
a<=10 || ch=='a', is true
<boolean-expression> && <boolean-expression>
class A
{
public static void main(String... ar)
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 & a>10) //a is less than 15 and a is greater than 10
System.out.println("a<15 & a>10, is true");
if(b<25 & b>19) //b is less than 25 and a is greater than 19
System.out.println("b<25 & b>19, is true");
if(ch1=='a' & ch2=='b') //ch1 has 'a' and ch2 has 'c'
System.out.println("ch1=='a' & ch1=='b', is true");
if(d>2 & ch2=='b') //d is greater than 2 and value in ch2 has 'b', is true
System.out.println("d>2 & ch2=='b', is true");
if(a<=10 & d>=2.5) //a is less than or equal to 10 and d is greater than or equal to 2.5
System.out.println("a<=10 & d>=2.5, is true");
if(a<10 & ch1=='a') //a is less than 10 and ch1 has 'a'
System.out.println("a<10 & ch1=='a', is true");
}
}
b<25 & b>19, is true
ch1=='a' & ch1=='c', is true
d>2 & ch2=='b', is true
a<=10 & d>=2.5, is true
Note: There should never be any space between > and = of >= operator, or a compile error is thrown.
<boolean-expression> | <boolean-expression>
class A
{
public static void main(String... ar)
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 | a>10) //a is less than 15 and a is greater than 10
System.out.println("a<15 | a>10, is true");
if(b<25 | b>19) //b is less than 25 and a is greater than 19
System.out.println("b<25 | b>19, is true");
if(ch1=='a' | ch2=='b') //ch1 has 'a' and ch2 has 'b'
System.out.println("ch1=='a' | ch1=='b, is true");
if(d>2 | ch2=='b') //d is greater than 2 and value in ch2 has 'b', is true
System.out.println("d>2 | ch2=='b', is true");
if(a<=10 | d>=2.5) //a is less than or equal to 10 and d is greater than or equal to 2.5
System.out.println("a<=10 | d<=2.5, is true");
if(a<10 | ch1=='a') //a is less than 10 and ch1 is equal to 'a'
System.out.println("a<10 | ch1=='a', is true");
}
}
Output -
a<15 | a>10, is true
b<25 | b>19, is true
ch1=='a' | ch1=='c', is true
d>2 | ch2=='b', is true
a<=10 | d<=2.5, is true
a<10 | ch1=='a', is true
Note: There should never be any space between = and = of == operator, or a compile error is thrown.
!<boolean-expression>
class A
{
public static void main(String... ar)
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(!(10>15))
System.out.println("!(10>15), is true ");
if( !(b<25 || b>19) ) //Invert of (b is less than 25 and a is greater than 19)
System.out.println("!(b<25 | b>19), is false");
if( !(ch1=='a' && ch2=='b') ) //Inverting the boolean value of (ch1=='a' && ch2=='b')
System.out.println("!(ch1=='a' && ch2=='b'), is false");
if(d>2 & !(ch2=='b')) //Inverting the boolean value of an expression by, !(ch2=='b')
System.out.println("d>2 & !(ch2=='b'), is true");
if( !(a<=10 | d>=2.5) ) ///Inverting the boolean value of (a<=10 | d>=2.5)
System.out.println("a<=10 | d<=2.5, is true");
if(!(a<10)) //Inverting the boolean value of (a<10)
System.out.println("!(a<10) is true");
if(!true) //Inverting boolean value true to false
System.out.println("this won't be printed");
if(!false) //Inverting boolean value false to true
System.out.println("false is inverted to true");
}
}
!(10>15), is true
!(a<10) is true
false is inverted to true
Advertisement
Advertisement
Please check our latest addition
PYTHON and DJANGO
Advertisement