Again relating to our block based control palette, we have three kinds of ways that we can control what the computer does with our program. First is sequences or straight line, that's the exact same behavior every time you execute the code. Last unit we looked at selection where we skipped some instructions but do others. And then there is repetition or loops where we repeat some instructions and that's what is all about in unit four. So we're going to be looking at two different types of loops that are offered in Java. The first is called While loop. We going to cover that first. And next we going to cover second because it's a little easier I think for us to do For loops. So while loops are loops that are conditional. We don't necessarily know how many different times they going to run when we write them. Well, we'll see, there is a caveat to that. For loops are counted loops. And that means they're going to go a certain number of times. Now, that said, in addition to looking at each of these types of loops, we are going to do a little bit more depth. We're going to look at using both of those types of loops to manipulate and do things with strings. We're going to particularly take for loops and look at the nested versions of those. If you ever made a turtle or a kitty-cat or anything move in a box pattern, that was a nested loop. And then, the very last section will be a small section about analysis of loops. You're counting how many times a set of loops will run with sort of a very small preview toward algorithm analysis that we'll get into more when we do searching and sorting. Let's start with for loops or counted loops. This is, again, from Snap. You could have it in Scratch or many others. Or I often just use the repeat loop, repeat something ten times. Snap, unlike Scratch, actually has one that's sort of leading you on toward Java, which gives you an iterator for i equal 10, 1 to 10. So I'm going to go ahead and start with that one. That's the same as the repeat ten. But it makes it easier to sort of relate what's going on in the block with actual Java code. So this would be the equivalent Java code for this for loop. There's a lot of text there. Let me break it down for you. Well, sorry, before I'll do that, let me tell you about what's more difficult here with the Java. And that is we're going to be managing a lot more details of the iterating. Even compared to the for i equal 1 to 10 that's going to handle a lot of things. Repeat 10 times, that just takes care of everything, it just says I'm good, don't worry, don't worry just put all your instructions here you want have happened 10 times. But we are going to have control over the iterator i, it can be anything, we tend to use i. And these for loops can do everything from what can be done in Scratch or Snap, a very simple, do this one up to some number of times or do this some number of times. But we can do way more complex things with that. All right, now let's break down what this text is all saying. So there's this for instruction, notably has parentheses. And it has three parts to the loop header there. They're each separated by semicolons. So the first part, int i=1, before that first semicolon. This is called the loop control variable initialization. I is going to be our loop control variable. We are saying we want it to be initialized, or to start at the value 1. The next part of the three parts of our for loop is the loop control expression. This says, under what condition do I keep looping? And this case is, you can think of while if you remember, well, you may not have done while yet. Anyway, we'll get there. While i is less than equal to 10, if i less equal to 10 is true, we're going to go ahead and loop again. Now the weird thing here is that it may not be completely clear to you that the block based 1 for i, 1 to 10, that really was start at 1 and go up to and including the number 10. Repeat 10 times. So tell your students to use fingers, 1-2-3-4-5-6-7-8-9-10, okay 10. So that's why our loop control expression is i less than or equal 10. I start at 1, i less than or equal to 10. Again, fingers, use your fingers. It's worth it. The last part of that for loop header there is kind of a confusing thing. This is what's called our loop control variable update. So i is our loop control variable, also known as an iterator. And this is saying how do you want to update the value in that? But the thing is this actually happens at the very end of the repeated instructions in the for loop. We just write it all at the top because they thought it was easier, and it is generally easier when you're designing a for loop to think of all of the conditions about where does it start? How long does it run? How does it update each time as you were writing all one line, than having to remember to put it at the bottom. But the point being, we're going to start with i equal to 1. Is 1 less than or equal to 10? Yes, so we're going to repeat those instructions. And it's not until we get done with that first set of repeated instructions, right before we even go back up and look at it, we're going to increase i by one. So we're going to look at that in more detail. So the key thing here is to understand when these things are all executed. Because even though they're written all on one line, unlike pretty much anything else we've seen, those things get executed at different times. With an if statement, I mean if you said that if A is less than B, well, anytime you came to the if statement, you did that right there. This is unique, and probably confusing to [LAUGH] students, because the location in the line does not indicate when it gets executed. Let me give you the overview, and then we're going to do a graphic to see how it works. So this initialization thing, the int i equal 1, that is executed once, actually before the loop even begins. Well, we only want to initialize once. We don't want to set i to 1 every time, that misses the point. The next thing, this is a Boolean expression that executes multiple times before we go and do the repeated instructions again. And it's our expression that we check to see if we should keep looping. And if it's true, we should go and do another set of our repeated instructions. And then of course this week, as we talked about i++, that's executed every time through the loop. But it's not executed at this point, it's executed mysteriously as the last instruction in the loop. So, if we need to translate this into something more representative of where things actually happen, I would have int i equal, sorry that should be 1, before the loop even starts. And then I put this in italics because we don't actually, this isn't Java code that's legal. But we're basically saying, check this Boolean expression, i less than or equal to 10. And you know what, if it's true, do all the things below me in the open bracket, closed bracket. Which can take sys of whatever repeated instructions that we gave it once we want it to repeat. And then the very last thing is the i++. So that's inside the open bracket, closed bracket. But what we don't necessarily, we've never seen before, is actually every time we hit the closed bracket of the loop, we go back up and try that test if true again. So there's this automatic switch of control that goes back up. Now, from the repeat block that might be something that kids understand. But especially with a Boolean expression that this might turn off, so they just need to remember that at the end of the block we go back up and do a test before we go on again. Here's another way to look at it, and a really good thing to do, we call it unrolling the loop. So int i equals 0, 0 should be 1. The initialization is the first thing, it happens only once. And then if we had like this test if true thing, that would be but we'd have our repeated instructions, i++ or repeated instructions i++, etc, etc. And you can see this, you can even draw for students, like this is the loop that gets repeated, okay? There's this test, and then we have the repeated instructions in the increment i++, test, repeat instructions, increment, etc. And I've shown four of those loops here because we know this runs ten times, that's actually going to happen six more times for a total of ten.