 Operators

• Print
This chapter is from the book

Relational Operators

A very necessary part of computer programming is performing certain actions based off the value of a variable; for example if the nuclear reactor is about to blow up, then shut it down. The next chapter will speak at length about the mechanism for implementing this type of logic, but this section addresses the mechanism for comparing two variables through a set of relational operators.

Relational operators compare the values of two variables and return a boolean value. The general form of a relation operation is

`LeftOperand RelationalOperator RightOperand`

Table 3.1 shows all the relational operators.

Table 3.1 Relational Operators

 Operator Description == Is Equal; returns a true value if the two values are equal != Not Equal; returns a true value if the two values are not equal < Less than; returns a true value if the left operand has a value less than the that of the right operand > Greater than; returns a true value if the left operand has a value greater than that of the right operand <= Less than or equal; returns a true value if the left operand has a value less than or equal to that of the right operand >= Greater than or equal; returns a true value if the left operand has a value greater than or equal to that of the right operand

For example:

```int a = 10;
int b = 10;
int c = 20;
boolean b1 = a == c; // false, 10 is not equal to 20
boolean b2 = a == b; // true, 10 is equal to 10
boolean b3 = a < c; // true, 10 is less than 20
boolean b4 = a < b; // false, 10 is not less than 10
boolean b5 = a <= b; // true, 10 is less than or equal to 10 (equal to)
boolean b6 = a != c; // true, 10 is not equal to 20```