Programming in Kotlin: Fundamentals

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

Part 2: Manage Control Flow

13. Work with For Loops

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: 12. Challenge: Use While Loops Next episode: 14. Challenge: Use For Loops

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.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Previously, you learned which loop exists in Kotlin. You implemented a way to iterate over values, using while and do-while loops. Those loops are generally used when you're not sure about how many iterations you're going to have. Which is why you have to set the condition for the loop to know when to stop iterating. The third and the last loop to learn is the for loop. For loop, compared to while and do-while loops, are used when you know the number of iterations you have within the loop. To achieve this, they use the concept of ranges. A range is what its name suggests, a range of numbers, from a starting number to the end number. In this example, a range from zero to 10, is a range of all the whole numbers between them, including zero and 10. When you look through the range, there's a fixed number of times you can execute the code, before the loop finishes. Having this rule, make sure you never have infinite loops. If you followed the your first Kolin Android app course, then you must have seen ranges in action. The next int function and the random class we used, actually takes in two integers, and generates random numbers within the given range. It's called generate a number within the range of one to 100, because it uses ranges behind the scene. Okay, let's implement a few for loops to iterate over specific ranges. You'll use ranges and for loop to iterate over values. Let's start off with a regular range. You create a range from zero to 10, just like the example in the slide. Enter the following code. To create a range, you use the double dot syntax, to connect two integer numbers together. This is known as inclusive, or close the range. It takes in both sides of the expression, and includes them as numbers in the range. Run the project to print out the results. You can see that this range includes both zero and 10, but sometimes you don't want to include the last number of the range. This is called half open range, and you can create one you then the until function, like so. Run the project to print out the range. You should see that now it doesn't include number 10. Creating ranges is one thing, but what about using its iterates with the help of a for loop? A for loop has the following syntax. It consists of the main section, where you define an index, or an iterator, just like the counter when you used a while loop. It then has the range you wants to iterate through. Then in the body, you use that index to achieve some behavior. In this case, you are printing out the index. And notice, we're just using the print function, and not the print element function, which prints out to a new line. So, this will be printed side by side. At the end, you're printing out an empty string in a new line to act as a separator. Run the project, and you should see each numbers from the range printed out. Cool, you've successfully created your first for loop. In some programming languages, you have to write in more code to use a basic for loop. You manually have to write in a condition to check when to stop the loop, and also manually increment the index. But in Kotlin, using ranges saves us from writing additional boiler plate code. Ranges also have helper functions, which lets you change the way you iterate through them. A good example is when you want to iterate every second, or third, or fourth item in a range. You can achieve this using the following code. You are using step here. You're using step to define how much the index is going to be increased after each iteration. In this case, you are saying that you want the index to be zero at first, then two, then four, and so on, or, in simpler terms, you're going through every second element. Run the project, and you should see every second element printed out. There's another way you can create ranges, you can create a reverse, or decreasing range. Add in the following code. The IDE complains about the code we just entered, hover over it to see the warning. And it says, this range is empty, and suggests to us if you meant to use the down to function, but what's really happening, you might ask? Why am I not allowed to use the double dot syntax for creating a reverse range? Well, there's some rules for creating ranges. If you were to implement a decreasing range to be used in the loop, using the following double dot syntax, you'll get an empty range. This is because the default step in a range is plus one, and there's no way to get from a larger number to a lower one by increasing the number. So, for reverse ranges, Kotlin provides us with a down to function. Change the for loop to use it like so. Its step is negative one, or minus one, and it can decrease the number gradually. Run the project to see the range decreasing. Cool, loops can also be used to iterate over something called collections. Not to worry, you'll learn about collections in the next course on the learning path. Now that you're equipped with all the tips and tricks for using ranges with loops, it's now time for your challenge. I'll see you in the next episode.