 Okay, welcome to Introduction to AP Computer Science A Unit 4 iteration. Iteration is just kind of a fancy term for repeating or for going through a set of items one at a time. Generally one at a time, I should say. So I've got my boilerplate Java structure here. I've got my class, I've got my main method, and we're going to get started with loops. In this case, we're going to start with a while loop. And a while loop is just what it sounds like. Something, it loops or repeats while something happens. So the first thing we might see is what's known as an infinite loop. And an infinite loop, true to its name, never ends. So we would say, well here's a structure, let's say while true. And let's see if we can do system, out, oops, out, there. Out.print, ln, infinite loop. And yeah, leave it at that I suppose. And so what should happen here is while true. So true is always true. So while this is true, so as it says true, it's always true, it's going to repeat what's here in the braces. So if we run that, see it's just printing infinite loop over and over again. So I'm going to click end. That's not a particularly helpful program. But if you were making a game, for example, you would have some type of loop like this that would keep the game going until the game ended. So I'm going to go ahead and just comment that out because we don't want that messing up things. The next one is a finite loop. So a finite loop does have an end. So here is an example. Let's say we have an integer x equals 0. And what we want to do is say while x is less than 10, you don't need to put spaces there, you can do it that way as well. What we want to do is we want to print the value of x. Now before I run this, what do you think is going to happen? What am I going to see on the screen? It might not be what you think. So let's see here. We see x is 0 and it never changes because I missed a very important step. What step is that? So I'm going to end that. What we have to do is we have to make sure that we increment the variable, the control variable here. So let's run that and see what happens. So let's scroll the screen over. We got x is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. So one of the key things you'll see on the AP exam with this type of problem is a lot of times does this start at 0 or does it start at 1? Does this end at 9 or does it end at 10? And those are the types of little things you have to be careful about and to know to look for. So let's walk through this a little bit. So x equals 0. So is 0 less than 10? Yes, that's true. So I'm going to print x plus x, which is 0. Then I'm going to add 1 to x, come back around. x is now 1. So is 1 less than 10? Yes, so on and so forth. And then when we get to 9, so is 9 less than 10? Yes, we'll print it. Add 1, 10. Is 10 less than 10? No. The loop ends and the program continues. Now since there's nothing here at the end, we don't see anything, but we could have more of our program. So one thing to keep an eye on though is if I put the x plus plus, I put the increment before the print statement, how does that change our output? In that case, we started at 1 and ended at 10. So that's one of those tricky little things they try to get you on the AP exam. So that was counting up with a finite loop. Let's try changing this from if x is less than 10 to x is less than or equal to 10. In that case, what do we get? So you see we went to 11 on that case. Now if I switch this back to... Usually typically you'll see the increment comes at the end. Not always, of course. Depends on the problem at hand, but typically you'll see it like this. Then you'll see this started at 0 and went to 10. Because 10 is equal to 10, it prints the 10, x plus plus makes it to x11. 11 is not less than or equal to 10. So when you're doing these types of problems on the exam, you really want to look at the starting value, the condition in here. Is it less than, greater than, or is it less than or equal to, greater than or equal to? And then what's the increment going on? Is it before or after whatever output that you're going to see? Now we could take this and instead of counting up, we can actually count down. So instead of that, what we could do is int x equals 10. So we want to start at 10 and while x is greater than 0, we want to print the output. Now let's see what happens. I know this is not going to work, but just something to be aware of. So you can see x is just jumping up, jumping up, jumping up. You can see how fast Java is. So I'm going to hit end. x should be going up or should be going down because we're counting down. So we want to use the decrement. In that case, we'll have a terminating loop. So again, we started at 10. We went down all the way to 1. So once we got to 1, x minus minus 0 is 0 greater than 0? No. Since it's not greater than 0, it doesn't print this line out. So that's a while loop. So that's one of the basic loop types. So what I'm going to do is I'm just going to go ahead and delete all this. And I'm going to go ahead and do 4.2, which is the for loop. And to count up, let's count up with this for loop. So watch what I do here. So for int, I can use x, but I'm going to use y in this case. y equals 0. So that's my starting value. While y is less than 10, increment y, then I'm going to go system.out.println, y is... So we're starting at 0. And while y is less than 10, we're going to do this and then increment each time. So it's a very compact form of what we just saw. So we started at 0. And while y was less than 10, it did the same thing. So that is a for loop. We can also do counting down. So for int, I'm going to use z here, or z depending where you're from. Start at 10. And while z is greater than 0, I say z minus minus. I'll do that the old style there. I'll keep that. So notice that we don't put the decrement in the body of the loop. It's up here in the first line. So we say system.out.println, z plus z. And let's see what we got. So z starts at 10, which is what we wanted. It goes down by 1 each time. And it gets down to 1. Now we can also do this. So minus equals 2. So it will count down by 2s. So we're going from 10 to 8 to 6 to 4 to 2. The next one, of course, is 0. But 0 is not greater than 0, so it never prints out. That is kind of how loops work. So I just want to talk real quick. That's basically it. There is also a do while loop, but since it's not on the AP exam, I'm not going to talk about it here. But this unit goes on to talk about section 4.3, which is developing algorithms. And it says using strings. I'm not going to go too much into that here. It's something we'll do in class. But just here's an idea. So for example, if we want to count, for example, the number of letters in a string that meet a certain criteria. So let's say, for example, we had a string and it was the grades in our class. So we had an a, we had a b, b, c, a, a, b, c. I like that there's no d's or f's. And we'll leave it at that. So we have a string of grades. So let's say we want to find out how many a's there are. So this is kind of a very standard approach to this problem. So we say int num of a equals zero. So how many do we know we have? So so far we know we have zero. So what we need to do is to iterate through the string one at a time. So to do that, we're going to go for int i equals zero. And I'm using i here as the index. While i is less than grades dot length. So how many items are in this string or how many letters in this case? i++, we're going to go through one at a time. We're going to iterate through each letter. So what we're going to do is compare each letter to a. So if, and this goes back to the, we talked about substrings earlier in an earlier video, grades dot substring i to the index, i plus one dot equals a. So if the substring, if that letter equals a, we're going to increment the number of a's. We'll add one. And then once we've iterated through every single letter, we have to check each individual letter. We type system dot print ln number of a's and num of a. So let's run that and see if it works. And number of a's is six. Let's go up and make sure that was correct. Eight, two, three, four, five, six. And there you go. So this is again a very common type of programming exercise, figuring out how many numbers are inside of, or sorry, how many of something are in a on a list or on an array at least in Java. And we'll stop there for now because basically we can use a string as our example. We want to iterate through each character one at a time. Yeah, we'll stop at that one. So then for the next one, and there's more to do with that, but that'll get you started, I think. 4.4, we have something called nested inter iteration. And nested just means something inside of something. So if you think about the Russian Matroska dolls, I think they're called, a little doll inside of a big doll, or a little doll inside of a doll, inside of a doll, inside of a doll. So a nested iteration is something like this. So for int a, we'll zero. I'll say a is less than five. I'll say a and we'll increment a. So what we want to do is we're going to put another loop inside. So for int b, let's say it's b consistent here. So b is less than, well, we do five as well. So b++, let me say system.out.currentln. So a, let's do a like this. So a plus, and let's see what's going to happen there. Okay, so if I scroll up, remember we got a in the first column. So a, zero, zero, zero, zero, zero. So there's one, two, three, four, five. Zero, one, two, three, four. Because we started at zero, we said a is less than five. So zero, one, two, three, four. That's five items, but we started at zero. So that's nested. Now we could also, we could put another one, we could put c in here, and we could nest it all the way down, basically as far as we wanted, last thing is, I think it's the last thing, is informal code analysis. So I'm just going to keep this example above, that we just did. int a is zero, a is less than five, a++. So that line, so four, I'm just going to copy it. Let's see how we're going to do this. Let's see if it, let's just do that or not. If not, I want to change the variable names. Let's do it, okay. That was pretty cool. So notice how it let us do a equals zero again, even though we had already done that. And that is because this a is local only to this for loop. So it doesn't have a wider existence. We'll talk about scope and that sort of thing in a later video. But for now, I think we're okay. So if I cut this out, let's try it this way. num of iterations equals zero. So if I take this and then cut that out for now, and I'll say num of iterations plus plus. So basically each time through the loop I'm going to add one to number of iterations. So system.out.println and I'll say num of iterations plus. So how many times is this code executed? Let's run that. Hopefully you'll get five. Okay. So we started at zero. Went to one, two, three, and four. That's five times. So we're analyzing this code. So if I change five to ten, how many times is it going to iterate? So drumroll. And we got ten. Now where it gets complicated, well one of the ways it gets complicated is if we then go ahead and put int b equals zero. And b is less than five. b plus plus. Notice this is an informal evaluation of how much code and how much code is executing. Or how many lines are going to start executing. So what do you think is going to happen here? So this part executes ten times. This part executes five times. So what do you think the number of iterations going to be? So we have our drumroll. And we had an error because I forgot the parenthesis. And drumroll again. And we got 50. So it's the inner loop. This is called the inner loop. It executes five times. Times the outer loop. However many times that executes, which in this case is ten. So that kind of tells us that when we use nested loops, the number of iterations goes up greatly. Now in a small program we don't really worry about it, but let's say we had to go through a million lines and 5,000 times each. So how many iterations would that be? Ooh. I can tell you exactly how many that would be. Quite a lot. Actually, you can see how actually the number is weird. It's gone negative. That is because we've actually gone too far outside the size of an int. We've gone over the size of an int and it's rolled over. So we cut a few of those out. We get a more palatable number. Okay, so that makes a little bit more sense. That's 50 million. Now again, notice how fast that was. Java is very fast. But this is something to think about when you're writing code, especially complicated code that does a lot of things, you want to try to eliminate loops if possible to execute more efficiently and quickly. I think we can stop there for that one. So just real quick, although I deleted it, we started with a while loop and we could have an infinite loop there if we weren't careful. We had four loops and we were able to count up starting at here. This is our condition and this is our increment. Now in most cases we use plus-plus or minus-minus, but depending on the problem counting by twos or counting by threes, et cetera, et cetera. Notice when we count down we use greater than and minus. When we count up we use less than and plus. So we need to keep those things in mind. I thought very briefly about developing algorithms. There are a lot of standard type things that you'll be doing quite often. One thing, as I mentioned here, for example, was that we might be trying to find out a number of items that match a certain criteria. So to do that we have to check every single one. One by one and I started at the beginning and went all the way to the end to check and see if it was indeed an A. If it was I added num of A plus-plus, added one to that and then once the loop is done then I printed out the result. You see that pattern over and over again. There are other patterns we'll talk about in class. Finally, not finally, but nested iteration. So it's a loop inside of a loop and if you have a small problem space like you have here just five items and five items not a big deal but as the numbers grow if you're trying to go through a million line database you might want to try to improve your code efficiency to execute faster. And then finally we talked a little bit about informal code analysis a little bit related to what you saw there. How many times is the code going to execute? Just informally. How many different lines of code do you see executing? Also don't forget this line. This is a line by itself so that's one. You have to add that one to the final tally. We'll talk more about that later.