This video Use Booleans & Comparison Operators was last updated on Aug 9 2022
So far in this course, you’ve learned about a few data types:
Integers, which represent whole numbers like 1, 2, 3,
Doubles, which represent decimal numbers like 3.14,
and Strings, which represent text like “Hello World”.
In this exercise, you’ll learn about a another type in Kotlin: Booleans.
Booleans, or “bools” for short, can only have two possible values:
…true, or false.
They are used to represent whether some condition is met, or not.
You might not realize it, but you’ve already used booleans if you built the Bullseye app. You’ll see what I mean before the end of this episode.
Let’s see how to create and use Boolean
s.
To declare a Boolean, you’ll add two new constant to main()
function, and name the first one isProgrammingAwesome
,
then set it to true, because programming is really awesome!!!.
val isProgrammingAwesome = true
And name the second constant isLazinessAwesome
and set it to false because being lazy is not awesome.
val isLazinessAwesome = false
Then add the following print statements:
println("Is programming awesome? $isProgrammingAwesome")
println("Is laziness awesome? $isLazinessAwesome")
Run the project and check the output. And you can see: The corresponding boolean values are printed out in the string.
Now, the power of Booleans is not that you can assign a value to be true or false. It’s power is evident when you evaluate different expressions or statements, to see if they match some criteria.
Think back to when you were working on the Bullseye app, where you wrote the code to calculate the difference between the slider value and the target value.
You wrote an if statement that said “if difference == 0”. The part that says “difference == 0” is what’s known as an expression. You can think of an expression as a unit of code that resolves to a value.
In this case, the expression resolves to either true or false. Either the difference is exactly equal to 0 - which means the expression is true, or the difference is not equal to 0, which means the expression is false.
Effectively, the result of the “difference == 0” expression is a temporary boolean value. If that expression evaluates to true, the part inside the if statement will execute. If it evaluates to false, then the part inside the if statement won’t execute.
Demo 2
To see this in action, you’ll set up a simple scenario with some students with different grades, and you’ll set up some Boolean variables depending on who passed — and who failed.
First, delete the existing code.
But wait…do you really need to?
You can just comment them out by selecting them, then you press Cmd + /
.
Next set up a constant, name it passingGrade
, and set it to 50
:
val passingGrade = 50
After that, create another constant, name it studentGrade
, and set it to, say, 74
:
val studentGrade = 74
Now, how can you determine if the student has passed using Booleans? Just as you did in the Bullseye app with other values, you can compare those two values to see if they are greater than or less than each other.
In this case, if a student’s mark is greater than the passing grade, then we consider that they have passed.
So let’s set up an expression to represent that.
Create another constant, name it studentPassed
, and set its value to studentGrade
is greater than passingGrade
then print it out:
val studentPassed = studentGrade > passingGrade
println(studentPassed)
When you run the code, you’ll see it prints out true
which means that this student passed, since 74 is greater than 50,
so it sets the value of studentPassed
to true.
You can also test the reverse situation to see if the student failed
that is, if the student’s grade is less than the passing grade.
Create another constant, name it studentFailed
, and set its value to studentGrade less than passingGrade:
val studentFailed = studentGrade < passingGrade
println(studentFailed)
Run the project.
And there you see that the value of studentFailed
is false
- which means that this student did not fail.
But there’s a small narrow case here that we haven’t covered: what if the student got exactly 50?
Go back up to where you declared the constants and comment out the one where you set the student grade to 74
.
Below that, redeclare that constant and set it to 50
.
// val studentGrade = 74
val studentGrade = 50
Take a look now at your two constants studentPassed
and studentFailed
.
They’re both false.
Well, that can’t be right!
What you haven’t taken into account here is what happens when the studentGrade
is exactly equal to the passing grade.
In English, to determine if someone passed, you’d say “if studentGrade
is more than or equal to passingGrade
”,
and kotlin provides an operator just for that.
Comment out this line:
// val studentPassed = studentGrade > passingGrade
…and replace it with this slightly different line, where we declare a constant studentPassed
and set its value to “studentGrade
is greater than or equal to passingGrade
”:
val studentPassed = studentGrade >= passingGrade
And there you go – now, if the student squeaks by with a 50, they still pass. Good for them!!! There’s also an operator for “less than or equal to” you can use them wherever you need.
Now, you can also simply compare whether something is equal or not equal to each other. Let’s take the case of Sam and Chris, our two shiny new students, and give them each a grade.
Declare the constant chrisGrade
and give Chris
a 49
:
val chrisGrade = 49
And declare another constant and give our star student, Sam
, a 99
:
val samGrade = 99
You’ve probably been used to thinking of the equals sign as this:
=
But in kotlin, like many other languages, that’s used to assign values, which is why I’ve tried not to say things like “chrisGrade equals 99” so far.
It’s actually known as the “assignment operator” in programming, not “the equals sign”. To see if two things are equal in kotlin, you use the “equality” operator instead, which are two equal signs together. Let’s see this in action.
Is samGrade
equal to chrisGrade
?
Go ahead and enter the following code to check:
print(samGrade == chrisGrade)
Run the project.
No, of course they’re not as seen in the run panel. Kotlin looks at the two values on either side of the equality operator, decides they are not equivalent, and returns “false” in this case.
Now, what if you wanted to check whether two things are not equal? Kotlin has an operator for that as well: unsurprisingly called, the “inequality” operator, which is an exclamation mark, followed by an equals sign.
Let’s use it to check if samGrade
is not equal to chrisGrade
by entering the following code:
print(samGrade != chrisGrade)
Run the project.
And yes, of course they’re not equal. It’s good to get in the habit of reading this as “not equal to” since, as you’ll see in later episodes, the exclamation mark actually has another use where you’ll read it as “not”.
Kotlin can also compare things beside numbers – including String
s.
Declare two constants: catName
, which takes the value of Ozma
, and dogName
, which takes the value of Cosmos
.
val catName = "Ozma"
val dogName = "Cosmos"
You can use those same equality and inequality operators on String
s.
Is catName equal to dogName?
Let’s check:
print(catName == dogName)
Run the project.
No! They’re not the same. Kotlin compares the contents of each String to see if they are the same or different, and in this case, they’re different.