Previous episode: 03. Explore Kotlin Language Basics
Next episode: 05. Challenge: Booleans
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.
So far in this course, we've learned about a few data types, integers, which represent whole numbers, like 1, 2, 3, doubles, which represents decimal numbers like 3.14, and strings, which represents text like hello all. In this example, you learn about another type in Kotlin, booleans. Booleans or boos 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 this, 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 booleans. To declare a boolean, you'll add two constants to the main function, and name the first one is programming awesome, then set it to true because programming is really awesome. And name the second constant is laziness awesome, and set it to false because being lazy is not awesome. Then add the following print statements. Run the project and check the outputs. And you can see, the correspondent 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 codes to calculate difference between the slider value and the target value. You wrote an if statement that said if difference is equal to zero, the parts that says difference equal to zero is what is known as an expression. You can think of an expression as a unit of codes that resolves to a value. In this case, the expression resolves to either true or false. Either the difference is exactly equal to zero which means that the expression is true or the difference is not equal to zero which means that the expression is false. Effectively, the result of difference equal to zero expression is a temporary boolean value. If that expression evaluates to true, the parts inside the if statements will execute, if it evaluates to false, then the parts inside the if statements won't execute. 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 commend them out by selecting them, and then you press command forward slash. Next, setup a constant, name it to passing grade, and set it to 50. After that, create another constant, name it student grade, and set it to say 74. Now, how can you determine if the student has passed using booleans? Just as you did at 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 can consider that they have passed. So, let's set up an expression to represent that. Create another constant, name it student passed, and set its values to student grade is greater than passing grade, then print it out. When you run the code, you'll see it print out true, which means that this student passed, since 74 is greater than 50. So it sets the value of students passed to true. You can also test the reverse situation to see if a student failed. That is, if the student's grade is less than the passing grade. Create another constant, name it student failed, and set its value to student grade less than the passing grade. Run the project. And there you can see that the value of student failed is false, which means that this student did not false. 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 constant, and comment out the one where you set the student grades to 74. Below that, re-declare that constant and set it to 50. Take a look now at your two constants, student passed and student failed, they are both false. Well, that can't be right. What you haven't taken into account here is that, what happens when the student grade is exactly equal to the passing grade? In English, to determine if someone passed, you'd say if students grade is more than or equal to passing grade and Kotlin provides an operator just for that. Comment out this line, and replace it with this slightly different line where we declare a constant student passed, and set it to students grade is greater than or equal to passing grade. 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 Chris grade, and give Chris a 49. And declare another constant, and give our star student Sam, a 99. You've probably been used to thinking of equal sign as this, but in Kolin, like many other languages, that's used to assign values which is why I have tried not to say things like Chris grade equals 99 so far. It's actually known as the assignment operator in programming not the equal 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 Sam grade equal to Chris grade? Go ahead and enter the following codes to check. 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're not equivalent, and returns false in this case. Now, what if you wanted to check whether two things are not equal, Kolin has an operator for that as well. Unsurprisingly called the inequality operator which is an exclamation mark followed by an equal sign. Let's use it to check if Sam grade is not equal to Chris grade by entering the following. Run the project. And yes, of course they are 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 read it as not. Kotlin can also compare things beside numbers including strings. Declare two constants, cat name which takes the value of ozma, and dog name which takes the value of cosmos. You can use those same equality and inequality operators on strings, is cat name equal to dog name? Let's check. Run the project. No, they're not the same. Kotlin compares the content of each string to see if they're the same or different. And in this case, they are different.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.