Relational and Logical Operators

Reading Time: 3 minutes

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

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.

Operators Expression Description
== 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

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.  

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 1 Average: 5]

4 thoughts on “Relational and Logical Operators”

  1. I have been browsing on-line greater than three hours today,
    but I by no means discovered any attention-grabbing
    article like yours. It is beautiful price sufficient
    for me. In my opinion, if all web owners and bloggers made
    excellent content material as you did, the web will be a lot
    more helpful than ever before. I am sure this paragraph
    has touched all the internet viewers, its really really fastidious paragraph on building up
    new website. I’ve been surfing online more than 3 hours
    these days, but I never found any attention-grabbing article like yours.
    It’s pretty value enough for me. In my view, if all webmasters
    and bloggers made good content material as you did, the net can be a lot more useful than ever before.
    http://alexa.com/

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications