Advertisement
// Java - Example of < relational operator
class A
{
public static void main(String...ar)
{
if(5 < 10) //comparing an int to an int
System.out.println("5 is less than 10");
if('a' < 'b') //comparing a character with a character
System.out.println("a is less than b");
if(100 < 'z') //comparing an int with ASCII value of a character
System.out.println("100 is less than z");
if( 100.5 < 100.4) //comparing a double with double
System.out.println("100.5 is less than 100.4");
if(100 < 100.001) //comparing an int with a double
System.out.println("100 is less than 100.001");
}
}
10 is less than 5
a is less than b
100 is less than z
100 is less than 100.001
// Java - Example of > relational operator
class A
{
public static void main(String...ar)
{
if(10 > 10) //comparing an int with an int
System.out.println("10 is greater than 10");
if('b' > 'a') //comparing a character with a character
System.out.println("b is greater than a");
if(200 > 'z') //comparing an int with an ASCII value of A character
System.out.println("200 is greater than z");
if( 100.5f > 100.4999) //comparing a float with a double
System.out.println("100.5 is greater than 100.4999");
if(100 > 100.001) //comparing an int with a double
System.out.println("100 is greater than 100.001");
}
}
b is greater than a
200 is greater than z
100.5 is greater than 100.4999
Advertisement
// Java - Example of <= relational operator
class A
{
public static void main(String... ar)
{
if('a' <= 'b') //comparing a character with another character
System.out.println("a is less than or equal to b");
if('a' <= 1) //comparing a character's ASCII value with int
System.out.println("a is less than or equal to 1");
if('z' <= 'z')
System.out.println("z is less than equal to z");
if(30.5 <= 31) //comparing an double value with int
System.out.println("30.5 is less than or equal to 31");
if(20.5 <= 20.6) //comparing an double value with a double
System.out.println("20.5 is less than or equal to 20.6");
if(10.5 <= 10.5) //comparong a double with a double
System.out.println("10.5 is less than or equal to 10.5");
if(5 <= 5) //comparing an int to int
System.out.println("5 is less than or equal to 5");
if(6 <= 6.0) //comparing a int with a double
System.out.println("6 is less than or equal to 5");
if(10 <= 6) //comparing a int with a double
System.out.println("10 is less than or equal to 6");
}
}
a is less than or equal to b
z is less than equal to z
a is less than or equal to 1
30.5 is less than or equal to 31
20.5 is less than or equal to 20.6
10.5 is less or equal to 10.5
5 is less than or equal to 5
6 is less than or equal to 6.0
Note: There should never be any space between < and = of <= operator, or a compile error is thrown.
// Java - Example of >= relational operator
class A
{
public static void main(String... ar)
{
if('z' >= 'b') //comparing a character with another character
System.out.println("z is greater than or equal to b");
if('a' >= -1) //comparing a character's ASCII value with int
System.out.println("a is greater than or equal to -1");
if('z' >= 'z')
System.out.println("z is greater than equal to z");
if(50.5 >=50) //comparing an double value with int
System.out.println("50.5 is greater than or equal to 50");
if(19.7 >=21.7) //comparing a double value with a double
System.out.println("19.7 is greater than or equal to 21.7");
if(10.5 >= 10.5) //comparing a double with a double
System.out.println("10.5 is greater or equal to 10.5");
if(5 >= 5.0) //comparing an int to int
System.out.println("5 is greater than or equal to 5.0");
if(6 >= 10) //comparing a int with a double
System.out.println("6 is greater than or equal to 10");
if(100.5 >= 'a') //comparing a double with a char
System.out.println("100.5 is greater than or equal to a");
}
}
z is greater than or equal to b
a is greater than or equal to -1
z is greater than or equal to z
50.5 is greater than or equal to 50
10.5 is greater or equal to 10.5
5 is greater than or equal to 5.0
100.5 is greater than a
Note: There should never be any space between > and = of >= operator, or a compile error is thrown.
// Java - Example of == relational operator
class A
{
public static void main(String... ar)
{
if('z' == 'z') //comparing a character to another character
System.out.println("z is equal to z");
if(50.5 == 50.5) //comparing an double value with double
System.out.println("50.5 is equal to 50.5");
if(100 == 100.001) //comparing an int with a double
System.out.println("100 is equal to 100.001");
if(99.9999 == 99.999) //comparing a double with a double
System.out.println("99.9999 is equal to 99.999");
if(5 == 5.0) //comparing an int to double
System.out.println("5 is equal to 5.0");
if(10 == 10) //comparing an int to int
System.out.println("10 is equal to 10");
if(97 == 'a') //comparing an int to ASCII value of character
System.out.println("97 is equal to a");
}
}
z is equal to z
50.5 is equal to 50.5
5 is equal to 5.0
10 is equal to 10
97 is equal to a
Note: There should never be any space between = and = of == operator, or a compile error is thrown.
// Java - Example of != relational operator
class A
{
public static void main(String... ar)
{
if('z' != 'a') //comparing a character to another character
System.out.println("z is not equal to a");
if(100 != 100.001) //comparing an int with a double
System.out.println("100 is not equal to 100.001");
if(99.9999 != 99.999) //comparing a double with a double
System.out.println("99.9999 is not equal to 99.999");
if(5.00 != 5.0) //comparing an int to double
System.out.println("5.00 is not equal to 5.0");
if(11 !=10)
System.out.println("11 is not equal to 10");
if(98 != 'a') //comparing int to ASCII value of a character
System.out.println("98 is not equal to a");
}
}
z is not equal to a
100 is not equal to 100.001
99.9999 is not equal to 99.999
11 is equal to 10
98 is equal to a
Note : There should never be any space between ! and = of != operator, or a compile error is thrown.
Advertisement
Advertisement
Please check our latest addition
PYTHON and DJANGO
Advertisement