- Overview
- Transcript
3.3 Ranges and the Double Dot Operator
In this lesson, I'll show you how ranges work in Kotlin. We'll look at ascending and descending ranges and the various range operators. I'll also show you how ranges are helpful when you're coding loops and iterators.
1.Introduction3 lessons, 07:48
1.1Introduction01:16
1.2Kotlin Overview04:06
1.3Installing IntelliJ IDEA02:26
2.Get Started With Kotlin7 lessons, 41:00
2.1Your First Program09:11
2.2Using the REPL03:03
2.3Comments02:52
2.4Variables and Data Types07:20
2.5Classes and Functions06:29
2.6Imports and Constructors04:31
2.7Inheritance07:34
3.Features and Syntax7 lessons, 40:53
3.1Goodbye Null Pointer Exception07:58
3.2String Templates and Interpolation07:44
3.3Ranges and the Double Dot Operator03:03
3.4`if` and `when` as Expressions07:08
3.5Loops and Iterators: the `for` Loop03:28
3.6Loops and Iterators: the `while` and `do`...`while` Loops04:57
3.7Loop Control Statements With Labelled `for` Loops06:35
4.Some Advanced Features2 lessons, 13:26
4.1Lambdas and Higher-Order Functions09:11
4.2Interoperability04:15
5.Conclusion1 lesson, 01:17
5.1Conclusion01:17
3.3 Ranges and the Double Dot Operator
Hello, in this lesson, we will be learning about ranges and double dot operators in Kotlin. This is going to be a very short lesson starting with the ranges. Let's say we have var range one, let me represent it as r1, is equal to 1..5. So here we have the variable r1, which has its range from 1 to 5. It will have the value, starting from 1 to 5. So this is the double dot operator, and the range of variable is 1, which ends at 5. Another way you can represent this range of r1 is, this is the range of r2. It is from 1 to 5. Same as the previous range, it is just a different representation. Now, what if we want the range in the descending order? It will be written like this. Say r3 =5..1 which will vary from 5, 4, 3, 2, 1. And now the other representation of this means 5 down to the other int value or we can say the to value is 1. Here instead of integer we can also have the characters. The value of this range will be varying from a, b, c, up to z. These ranges and double dot operators will be very helpful when we use it with the loop. Another way we can use these ranges and dot operators is if we want to have some steps in our ranges. Let's say if we want 1, 3, and 5, 1, 3, and 5. And we do not want 2 and 4. So how do we do that? 1..5 step 2. It means 1, the second one will be ignored, 3, fourth one will be ignored, and then 5. Which means at the step of 2, the values will be assigned to the ranges. Same in case of the down function. We will have var r7 = 5..1 step 2. Values will be 5, 3, 1. Same way instead of double dot operator you can use rangeTo to define the steps for your ranges. And similarly, for the descending order, you can use the downTo function and use the step with it. So here we are, we are done with the ranges and the double dot operators. In the next lesson, we will be learning about implementing these ranges and the double dot operators with the help of loops. So stay tuned for the upcoming videos. I hope you're having a great time. Keep smiling, and have a good day.