1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Layouts and Animation for Android Apps

Loops and Iteration

Start course
Overview
Difficulty
Intermediate
Duration
4h 7m
Students
1
Description

In this course, you'll start experimenting with XML code and diving deeper into layouts, namely linear and constraint layouts. We'll also look at animations and build a few fun features with them. Then, we'll take a deeper dive into the Kotlin programming language and constraint layouts, before building a fully functioning Tic-Tac-Toe game.

On top of that, we'll then build a second app which can play YouTube videos within it, and you'll learn how to work with APIs and API keys.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner-level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello, and welcome back. In the last video, we looked at some array methods we can use to manipulate them. And in this video, we'll look at how we can iterate through or a loop through an array like this or a collection such as this to perform any operation we want on its elements, like access information, change something, update something, and so on. Basically, we want the ability to look through each item in the array and that is called iterating through the elements in an array, and there are a few ways to do this. What we'll use is called a for loop. To write a for loop, first you type in the for keyword and then (). And within here, you want to specify which structure you want to loop through, in our case that's names. And you also have to think about what you want to call each of the elements as you visit them. For example, the first time in the loop, I'm going to visit this element right here, so what should I call this? Second time around this element, so third time around this element, and so on. So each time, what do I call the variable I'm working with?

So, in my case I'm simply going to call it name, okay? You can call it whatever you want, a valid variable name will do. Since I'm dealing with names, I'm going to call it name. So, for (name in names) and that's it. So, for (name in names) then {} and within this code block of {} you write whatever you want to do as you visit each element. So, let's start off with printing name. So, println(name), so what happens? The first time in the loop, it's going to come to this first element. So, name is going to be Mashrur, and then println name, so it's going to print out Mashrur. Second time, it's going to go here, print it out. Third time, its going to go here, keep going until it gets to the last point, print out Ann and then we're done with the for loop. So, let's see if this actually works. Let's run it. There we go. 

We have all of the names printed out one after the other, ending with Ann. Now, as you do something within this block, it's going to apply that to each element that it visits. For example, if I wanted to say hi so within quotes, I'm going to say, "Hi, and then name". So, this is going to say Hi and the variable that is visiting every time. So, "Hi Mashrur," "Hi Rob," "Hi Jon," and so on. There you go. So, the condition you write within this block is going to be applied to each of the elements. So, imagine you had a massive list or structure, you could go through each element or object and perform operations on each of them in a very short period of time and that kind of tells you the power of data structures such as these and the ability to iterate through them. 

Now, let's look at a simple comparison we can add. Let's say that we are going through the elements and if we come across Jon, that's without the H, so J-O-N, if we come across this name, then we're going to say, "Found the king of the north." Okay, how do we do that? Well, within my for loop, I can put in an if condition. So, if (name = "Jon") then I'm going to put this in here and instead of "Hi name" ("Found the king of the north"). All right. If I run this, there you go. Found the king of the north, because Jon is in here. If I got rid of the name, nothing happens because it couldn't find it. All right. Now, let's switch our focus to numbers. Let's create a quick list of 100 numbers, how do we do that? I'm going to get rid of this as well, actually. Okay. 

So, in order to create a list of numbers or a range of numbers, you can use the range operator and it looks something like this. Let's say if I want to go from 1-100, I can say 1..100 and that's the range object and I can iterate through a range object like this and print all of them out. So, I'll use a for loop, I'll say for (num, which is a variable name that I want to use in 1..100 and then open and close curly brace. So, each time, first time it'll be 1, then 2, then 3, all the way up to 100 and each time the number will be in my num variable, I can say println(num) and you see that we'll get a number or numbers, sorry, between 1 and 100. Now, you can do a lot of other things other than printing it out. What if you wanted to only print out odd numbers between 1 and 100, how do you do that? Well, the condition for checking if a number is odd, you can divide that number by two and see if there's a remainder, okay? 

If the remainder is zero, then it's even and if the remainder is one, then it's an odd number. Let's try it out. The way you write that condition is if (num, use a mod operator to find the remainder, so mod 2, this will give you the remainder when the number is divided by two. If this is equal to zero, then you know that this is an even number. So, I'll say println "Even number" and then I'll print the number itself. Okay. So, this gives me all the even numbers. How would I use the same condition to find odd numbers? You see this ==, this is the equality operator, you can turn it into not equals by simply changing this to an !=, this means not equal to zero. So, if the number mod 2, meaning the remainder is not zero, then you can say odd number. And this will give us what we were looking for, all the odd numbers between 1 and 100. There you go, 1, 3, 5, all the way through to 99. Perfect. And notice how we've scanned through 100 numbers and found all the odd ones pretty quickly. 

So, imagine you had a million numbers and you had to do the same, you could do that and now imagine having to do that by hand, that would take a long time. All right, so taking this a step further, what if you wanted to sum up the value of all the odd numbers between 1 and 100? So, instead of simply printing out, this is the odd number, what if you wanted 1+3+5+ all the way up to +99. Imagine how much time that would take manually. But here we can do that very easily using a variable. Let's declare a variable on top here, on top of our block and we'll say var sumOfOddNumbers and we'll set it to 0 to start. And within our for loop and in this if condition, if we find an odd number, instead of printing it out we will simply add it to sumOfOddNumbers. So, whatever our sumOfOddNumbers was to start with, we'll add sumOfOddNumbers + num, the new number if it's odd. 

All right, let's try it out. Whoops, we didn't print this. So, after the for loop we're going to print this out. So, println(sumOfOddNumbers). We want to only print it after the for loop has concluded, otherwise we would get 1 at every step, we don't want that. There you go, 2500, and you can verify this if you like. The sum of odd numbers between 1 and 100 is 2500. All right. And there's a different way you can write this line, sumOfOddNumbers = sumOfOddNumbers + num, you can simply say sumOfOddNumbers += num. Okay, this is shorthand for what we had written before. If I run this, check it out, 2500. Great. And we can also explore a different way of writing this instead of using this 1..100 for the range, we can also use until. So, num in 1, instead of this I can say until 100 and this would work as well. So, if I run this, there you go, I get the same 2500, so that works as well. 

Now, what if we wanted every fifth number instead of all odd numbers? What if we wanted to print out, let's say 0, 5, 10, 15, and so on until 100? What we can do is use step size for this. So here, for num in, let's start at 0 instead of 1. So 0 and until 100 does not include 100, so I'll use until 101. So, I get from 0 all the way up to 100 and then I can provide the step size. So, I'll say step 5. All right, I can say step 2, and it'll give me 2, 4, and all of that. Step 5 will give me 5, 10, 15 and then I can get rid of this if condition and simply println(num). I'll get rid of this as well. If I run it, there you go 0, 5, 10, all the way up to 100. Great. So, we've covered a decent amount in this video about iterating through lists using for loops and also performing many operations on each element as we iterate through them. So, we'll leave it here for this one and in the next video we'll apply what we've learned about lists in this and the prior couple of videos and apply it directly to our app and see how we can track the game state using them. See you there.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics