Operators in Kotlin
The course is part of this learning path
This course takes a deep dive into Kotlin operators, giving you a practical understanding of how to use operators in your code.
This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.
This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.
Alright. Hello my friends. On this lesson, we're going to talk about the 'rangeTo' method and the "in" operator that gets used with this method. Alright, so we're starting to have a little fun with Kotlin and this rangeTo method is used for easy identification of numbers or characters within a specified range. For example, instead of making 10 different definitions for numbers from 1-10, we could make a single definition using the rangeTo method. Or instead of making separate definitions for all letters from the letter A to letter Z, we could do this with a single definition, just by using the rangeTo method.
Okay, so why don't we have a look at the rangeTo statement? So, as with most things, there's a couple of different ways that we can use the rangeTo method. So, in the first case after defining the variable, we must write the initial value of the range that we're going to define, makes sense. Then, we have to use the rangeTo method. Then finally, we must write the ending value of the range that we are defining in parentheses. Now, for the second case, we'll need to use two dots next to each other instead of the rangeTo method. But of course, you can use either of these two methods. They both are very handy and they both work the same way. Well, they work in different ways, but you'll see what I mean.
All right. So, remember earlier I mentioned the "in" operator. So, the "in" operator is also used with a rangeTo method. But by using the "in" operator, you can find out if a desired value is in the range that you specify. So, if you have a look at the example here, we passed all letters from A to Z into the variable named myCharRange. Okay. And also, pay attention to this. The letters A and Z are included in this range, right? Okay. So, let's find out if the letter K is in this range, just by using the "in" operator. So, if the letter K exists in this range, the variable myChar is true. If not, it'll be false. Alright? So, that's the quick and dirty of the rangeTo operations.
So, let's just move on to Android studio and you'll see how to use it in practice. So first, I'll create a Kotlin file, right click on the package name, select the New Kotlin file option. And after selecting the file option here, I'll determine the file name. We're going to call it Range. After hitting the 'enter' key, I've got to create the main method. So, I'll write main, select the maina option here. Hit 'enter' and thusly, the main method gets created. Okay, so let's create a variable called myCharRange. Let's transfer the letters from the letter A to the letter J to this variable. So, I'll write 'a' between single quotation marks.
So, for this example, I'm going to use a rangeTo method and you can do this by using two dots next to each other, if you want. I'm going to write 'j' in parentheses and again in single quotation mark. And of course, the reason we use single quotes here is because the char data type likes it that way. That's the way you use the char data type. What are you looking at me for? So, if you are going to perform an integer data type operation, for example, if you're going to transfer range from 1-10 for example, you do not need to use single quotes, you must write one for the start value and 10 for the end value.
Alright, you get it. So now, let's define a variable called myChar. So, what we're going to do is check to see if the letter K is in the myCharRange variable that we defined in the previous line. So, for this we need to use the "in" operator. So, I'm typing 'k' in myCharRange here, the variable myChar will be true if this range contains the letter K, otherwise false. Now, let's print the myChar variable to the console. I'll type sout, press 'enter'. Alright, myCharRange has k and parentheses between double quote and myChar after the plus sign. Now, let's run the code.
Expected result would be seeing our value false in the console, and there you have it. There is the value false in the console because there is no letter K between the letters A through J. Anyway, now let's change the ending value of the range and make it K and run the code again. Okay. So, as you see, there is now a true value on the console. So, that is how the rangeTo method and using the "in" operator get used in Kotlin. Now, I didn't show you in this particular example, but we'll be using them mostly in the for loop operations. Well, we're not going to get to those until a little bit later, but in anyway you'll see how it actually reinforces your knowledge rapidly. Alright, so, we're going to take a short break here. Want to see in the next video because we've got a lot to do. because now the fun is going to begin. So, I'll see you in the next video.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.