Relational operators are an essential part of programming logic. They are used to compare two values and return a boolean value, that is, true or false, based on the result of the comparison. These operators are fundamental to creating conditions and making decisions in a program. We will explore these operators in detail in this chapter.
Types of Relational Operators
There are six main types of relational operators used in most programming languages. They are:
- Equality (==): This operator checks whether two values are equal. For example, the expression "5 == 5" will return true, while "5 == 6" will return false.
- Inequality (!=): This operator checks whether two values are different. For example, the expression "5 != 6" will return true, while "5 != 5" will return false.
- Greater Than (>): This operator checks whether the left value is greater than the right value. For example, the expression "6 > 5" will return true, while "5 > 6" will return false.
- Less than (<): This operator checks whether the left value is less than the right value. For example, the expression "5 < 6" will return true, while "6 < 5" will return false.
- Greater than or equal to (>=): This operator checks whether the value on the left is greater than or equal to the value on the right. For example, the expression "6 >= 5" will return true, while "4 >= 5" will return false.
- Less than or equal to (<=): This operator checks whether the value on the left is less than or equal to the value on the right. For example, the expression "5 <= 6" will return true, while "6 <= 5" will return false.
Using Relational Operators
Relational operators are used in conditional statements, such as if, else if, and switch, to make decisions based on comparisons. They are also used in loops, such as for and while, to determine the number of times the loop should be executed.
Let's consider a simple example. Suppose you are creating a program to check whether a user is of legal age. You can use the greater than or equal to operator (>=) to compare the user's age with 18. If the user's age is greater than or equal to 18, the program returns "You are of legal age". Otherwise, it will return "You are underage".
int age = 20;
if (age >= 18) {
System.out.println("You are of legal age");
} else {
System.out.println("You are underage");
}
Relational operators can also be used in combination with logical operators, such as AND (&&), OR (||), and NOT (!), to create more complex conditions.
Relational Operators and Data Types
Relational operators can be used with different types of data, including integers, floating point numbers, characters, and strings. However, it is important to note that the behavior of these operators can vary depending on the data type.
For example, when used with characters, the greater than (>) and less than (<) operator compares the ASCII values of the characters. Therefore, the expression 'a' > 'b' will return false as the ASCII value of 'a' is less than the ASCII value of 'b'.
Likewise, when used with strings, these operators compare strings lexicographically. For example, the expression "apple" < "banana" will return true as "apple" comes before "banana" alphabetically.
Conclusion
Relational operators are a fundamental part of programming logic, allowing programs to make decisions based on comparisons. They are used in combination with logical operators to create complex conditions and can be used with different types of data. Understanding these operators is essential for anyone learning to program.