In computer science, a relational and logical operators are a programming language constructs or operators which tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3). In programming languages that include a distinct boolean data type in their type system, like Pascal, Ada, or Java, these operators usually evaluate to true or false, depending on if the conditional relationship between the two operands holds or not.
Relational operators are binary operators that are used to determine whether one of the operand is smaller, greater or equal than other operand.We have following relational operators in kotlin.
|==||a == b||a is equal to b or not|
|!=||a != b||a is not equal to b or not|
|>||a > b||a is greater than b|
|<||a < b||a is smaller than b or not|
|>=||a >= b||a is greater than or equal to b or not|
|<=||a <= b||a is smaller than or equal to b or not|
Let’s see examples.
Logical operators && (and), || (or), ! (not) are used to combine conditional statements forming a compound boolean expression that returns true or false as the result. The && operator (logical AND) returns true if all boolean expressions evaluate to true.
The || operator (logical OR) returns true if any one of the boolean expressions evaluates to true.
In first example a is not equal to 4, but b is smaller than 8, so the if statement will be executed. In the second example a is equal to 3, but b is not smaller then 7. In this case the if statement will be also executed. The ! operator (logical NOT) returns the reverse of the state of a boolean expression. NOT true returns false and NOT false returns true.
In first example a is not equal to 4, so it is true, and b is smaller than 8. The result is the if statement will be executed. In the second example a is equal to 3, but b is not smaller then 7. Whit the ! operator it is also true. The if statement will be executed.
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂