Kotlin Logical Operators Tutorial

In this section, we will learn what the Logical Operators are and how to use them in Kotlin.

Note: we’re assuming you’re already familiar with the Kotlin operators in General and Kotlin comparison operators.

What is Logical Operators in Kotlin?

When working with comparison operators, we know that there can be only two values involve in the comparison! But what if we have more values and comparisons that we want to run all of them at the same time? What if we want to run an operation only if the result of three comparison operations were true? How can we put together the result of multiple comparisons then?

This is where the logical operators come in.

Using the logical operators, we can, for example, put together multiple comparison operations and then only if the result for all of them were true, run a specific operation.

Or we can combine multiple comparison operations and only if one of them resulted true, run a specific operation, etc.

Alright, enough with theory and let’s get into the details of each of these operators and see how they work.

List of Logical operators in Kotlin

In the table below, you can see the list of logical operators supported in Kotlin.

Operator Symbol
AND Operator &&
OR Operator ||
Reverse the Result (Not) Operator !

Kotlin AND && Operator:

The AND operator with the symbol && is used to mix two expressions that result a boolean value (For example two comparison operations) and if both expressions resulted true, the final value of the AND operator will be true as well.

Example: using AND && operator in Kotlin

fun main(){
    var age = 50 
    var money = 2000
    if (age >21 && money >= 1000){
        println("The person can enter to the bar")
    }else{
        println("This person can't enter to the bar")
    }
    println("End")
}

Output:

The person can enter to the bar

End

Here the condition of the if statement is to check the value of two variables using the comparison operators.

Now we used the AND operator to run the body of the if statement only if the result of both comparison operations were true!

Now, because both comparisons resulted true in this example, the final result of the AND operator is true too and so the body of the if statement ran as a result.

Note: both expressions of the AND operator must be true so that this operator results true too! Otherwise, if one of the expressions resulted false, then the final result will be false as well.

Also, the AND operator is known for its short circuit! This means it first checks the expression on the left side and if that expression resulted false, it won’t check the expression on the right side! This is because of the nature of this operator (everything must result true to get the final result of true). So if the first expression is false, then there’s no point in checking the second expression! Hence, it will return the value false immediately.

Kotlin OR || Operator:

The OR operator takes two expressions that result in a boolean value (either true or false) and if only one of them resulted true, then the final result of the if statement will be true as well. Otherwise, if both expressions resulted false, then the final result of the OR operator will be false as well.

Example: using OR || operator in Kotlin

fun main(){
    var language = "Kotlin"

    if (language == "Java" || language == "Kotlin"){
        println("You can apply for a position in our company!")
    }else{
        println("Sorry, we need a Java or Kotlin developer at this time. ")
    }
    println("End")
}

Output:

You can apply for a position in our company!

End

The OR operator in this example is used as part of the if statement’s condition. Here, we checked the value of the language variable to see if it’s either “Java” or “Kotlin”. Now, if any of these comparisons resulted true, then the final result of the OR operator will be true and so the body of the if statement will run as a result.

That’s how we got the message you see on the output stream.

Kotlin Not ! Operator (AKA Reverse the Result Operator):

The Not operator is used to reverse the result of an expression that returns a boolean value!

For example, if the value is true, then applying the not operator on this operator would turn that into false and vice versa.

Example: using the not operator in Kotlin

fun main(){
    if (!true){
        println("This is the body of the if statement")
    }else{
        println("This is the body of the else statement")
    }
}

Output:

This is the body of the else statement

Note that the condition of the if statement is the value true which by default you would expect that its body should run as a result of this condition! But because we’ve used the not operator to negate the boolean value, the condition becomes false and so instead, the body of the else statement ran for this example.

Note: we put the Not operator in front of an expression that result a boolean value to reverse that result.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies