Relational operators usage for data comparison – Computer Sir Ki Class

Code Learning #JAVA#3676

## Relational operators usage for data comparison

Use of equality check operator == and other relational operators like not equal to !=, less than < , greater than > , less than or equal to <= and greater than or equal to >=  operators.

## Learning Objectives

• Understanding the use of six relational operators for equality and other comparison purposes.

## Source Code

 class RelationalOP { public static void main(String[] args) { int a=10,b=10; boolean c= (a==b); System.out.println("a=10,b=10 so a==b will be "+c); c=a!=b; System.out.println("a=10,b=10 so a!=b will be "+c); c=a>5; System.out.println("a=10 so a>5 will be "+c); c=b<11; System.out.println("b=10 so b<11 will be "+c); c=a<=10;System.out.println("a=10 so a<=10 will be "+c); c=b>=10;System.out.println("b=10 so a>=10 will be "+c); } } .linedwrap { border: 1px solid #c0c0c0; padding-top: 5px; padding-bottom: 5px; border-radius: 4px; background-color:#F8F8F8; } .linedtextarea { padding: 0px; margin: 0px; background-color:#F8F8F8; } .linedtextarea textarea, .linedwrap .codelines .lineno { font-size: 12pt; font-family: monospace; line-height: normal !important; } .linedtextarea textarea { padding-right:0.3em; padding-top:0.3em; border: 0; } .linedwrap .lines { margin-top: 0px; width: 20px; float: left; overflow: hidden; border-right: 1px solid #c0c0c0; margin-right: 10px; } .linedwrap .codelines { padding-top: 5px; } .linedwrap .codelines .lineno { color:#AAAAAA; padding-right: 2px; padding-top: 0.0em; text-align: right; white-space: nowrap; } .linedwrap .codelines .lineselect { color: red; }

## Run Output

``````a=10,b=10 so a==b will be true
a=10,b=10 so a!=b will be false
a=10 so a>5 will be true
b=10 so b<11 will be true
a=10 so a<=10 will be true
b=10 so a>=10 will be true

``````

## Code Understanding

int a=10,b=10; //integer variables a and b are initialised with 10

boolean c= (a==b);
Since relational operators  result in true or false so we shall use a boolean variable c to store the comparison result. Here we made a equality comparison.

System.out.println(“a=10,b=10 so a==b will be “+c);
Since both values are same equality comparison will result in true

c=a!=b; System.out.println(“a=10,b=10 so a!=b will be “+c);
For the same variables being same not equal to will result in false

c=a>5; System.out.println(“a=10 so a>5 will be “+c);
Variable a is set as 10 so its comparison of greater than 5 will result in true

c=b<11; System.out.println(“b=10 so b<11 will be “+c);
Variable b is set as 10 so its comparison of lesser than 11 will result in true

c=a<=10;System.out.println(“a=10 so a<=10 will be “+c);
Variable a is set as 10 so its comparison of lesser than or equal to 10 will result in true

c=b>=10;System.out.println(“b=10 so a>=10 will be “+c);
Variable b is set as 10 so its comparison of greater than or equal to 10 will result in true

## Common Errors

• Some students have problem in remembering the < and > than sign usage. Just a small tip for them. Remember that Hungry Alligator’s mouth simile. The hungry mouth always opens towards the bigger chunk. If 3 < 5 then mouth seems to open towards 5 so 5 is bigger. Since we read from left to right we will say 3 is smaller or lesser than 5. So the symbol will be called less than. Some people remember it by Less than means Left side is pinched.

Suggested Filename(s): RelationalOP.java

sunmitra| Created: 3-Mar-2018 | Updated: 3-Mar-2018|

×