Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 1: Use Data Types & Operations

06. Combine Logical Operators

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Challenge: Booleans Next episode: 07. Branch with If Expressions & Scopes

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 06. Combine Logical Operators

In the previous set of episodes, you saw how you can work with Boolean values, and how you could use comparison operators such as less than, greater than, equal to and not equal to, to compare values to each other.

In this episode, you’ll learn about another set of operators that are designed for working with Boolean values and expressions: these are known as “logical operators”.

Logical operators lets you do things like check if at least one Boolean value in a set is true, if all Boolean values in a set are true, or even to check if a Boolean value is not true.

Sounds strange? That’s OK - you’ll learn how these work as you work through the demo.

To get started, declare the following constants:

val passingGrade = 50
val studentGrade = 50
val chrisGrade = 49
val samGrade = 99

Now, lets define some Booleans (through type inference) that check whether or not our popular students passed. You’ll use these values as you work through some exercises about logical operators. Paste in the following code:

val studentPassed = studentGrade >= passingGrade
val chrisPassed = chrisGrade >= passingGrade
val samPassed = samGrade >= passingGrade

The first logical operator is simple: the not operator. It’s represented in kotlin as a single exclamation mark, placed before the constant or variable name. If I want to check the opposite, or inverse, of whether Sam passed, I simply write “not samPassed” like so:

println(!samPassed)

I’ll go ahead and just hover over the condition. And this tells me that this is false, because Sam did pass. And I can also check to see if Chris did not pass, by doing the same thing here: “not chrisPassed”:

println(!chrisPassed)

And in this case, it’s true because Chris didn’t pass. It may seem strange to check the inverse of a Boolean, but when you start writing more complicated logic later on, you’ll often want to run a particular block of code only when a particular value or set of values isn’t true.

So to avoid writing weird code like this:

println(chrisPassed == false)

You can simply use the not operator to make the meaning more clear. I’ll go ahead and undo this code.

Now, this not operator only applies to Booleans. What do you think happens if you try to use it on something like a String value? Let me set up a constant called catName and set it to “Jaspurr”:

val catName = "Jaspurr"

If I try to say “not catName”,

println(!catName)

You can see that the IDE complains about the not operator and if you hover over it or try to run the program, you’ll see it says Unresolved reference: !. And this is because not operations cant be performed on Strings.

Let’s comment that out, since that code doesn’t make sense.

// val catName = "Jaspurr"
// println(!catName)

The next logical operator is the AND operator. It’s pretty similar to the way you’d think about the word “AND” in English. For example: Do you want ice cream AND sprinkles? You represent AND in kotlin with two ampersand (&&) symbols - that’s right, the AND sign!

// AND Operator
// &&

You can use the AND operator to see if both Chris and Sam passed. Let me define a constant called bothPassed, and I want this to be true if both chrisPassed and samPassed are true:

val bothPassed = chrisPassed && samPassed
println(bothPassed)

And you can see that no, both Sam and Chris didn’t pass. This expression will only be true if both values on either side of the AND operator are true. In this case, we know that chrisPassed is false, so this entire expression can’t be true. This means that bothPassed would return false.

Now, what if you just want to know if one or the other has passed? That is, you don’t care who passed, just that either Sam or Chris passed?

That brings you to the next logical operator, OR. And again, the OR operator is somewhat analogous to the word OR in English. Are you having pie or cake for dessert?

Now, the one small difference is that in English, we usually mean “or” as exclusive in that you want either pie or cake. But the OR operator in Kotlin simply means “if any one of these things are true, then this whole expression is true”. So if you choose either pie or cake, the expression returns true.

Alright, let’s see it in code.

Demo 2

The OR operator is two vertical lines, or pipes like this:

// OR Operator
// ||

Lets define a constant called eitherPassed and set that to chrisPassed OR samPassed:

val eitherPassed = chrisPassed || samPassed
println(eitherPassed)

Run the project.

And in this case, you see that at least one of those two passed, so the result is true.

Even if both students had passed, the result would still be true. But if not a single person in that group had passed, then the result would be false.

You aren’t limited to just two values for OR and AND operators. You can combine as many together as you like. For instance, I have this anonymous student who has a grade and a passed value. We declared this variable earlier. What I want to know is, if any one of these three people passed?

For this, create a constant, anyonePassed, and set it to chrisPassed OR samPassed OR studentPassed, like so:

val anyonePassed = chrisPassed || samPassed || studentPassed
println(anyonePassed)

Here, we know at least one person passed, so this entire expression is true.

What if you wanted to know if everyone passed, that is, are all of the values in a statement true? You can do that with multiple AND operators, just like you did with multiple OR operators. Let’s declare a constant, everyonePassed, and set it to chrisPassed AND samPassed AND studentPassed:

val everyonePassed = chrisPassed && samPassed && studentPassed
println(everyonePassed)

And you see that no, not everyone passed. Because just one of these values is false, chrisPassed, and this makes the entire expression becomes false.

What’s nice about building up these Boolean expressions this way is that you can mix and match them with the comparison operators you learned about in the previous episode. So you can use things like greater than or less than operators along with these new AND and OR operators.

To see this in action, let’s assume that any student who has perfect attendance AND a mark over 90, gets a special merit award. Lets capture that in two constants: one, the meritAwardGrade of 90:

val meritAwardGrade = 90

…and let’s capture Sam’s perfect attendance in a constant, samHasPerfectAttendance:

val samHasPerfectAttendance = true

It must be nice to be Sam the perfect student :]

So, we need to build up an expression that determines:

  • (a) if Sam has perfect attendance, and
  • (b) if Sam has a mark over 90.

Lets create another constant to hold the result of this called samIsMeritStudent. Then I’ll set that to samHasPerfectAttendance AND samGrade is greater than meritAwardGrade like so:

val samIsMeritStudent = samHasPerfectAttendance && samGrade > meritAwardGrade
println(samIsMeritStudent)

Run the project to see the output.

And you see that yes, Sam has perfect attendance AND his mark is over 90.

Kotlin first calculates the value of samGrade greater than meritAwardGrade to be true, and then it goes on to evaluate the other Boolean values in the expression. Since both are true, then the entire expression is true as well.

Congratulations, Sam!