 Welcome to intro to Java with an emphasis on AP computer science a with Tokyo ed tech That is me our topic today is iteration, which means repeating sections of code This is one of the most difficult concepts for beginners to really master So it's important that you really take your time and make sure you understand how these things work So this lesson would go over for loops using for loops to count up using for loops to count down Using for loops to traverse string. You'll see what that means in a minute How to count items? So let's say we can count the number of spaces in the string for example And I'll talk about that in as it regards to what I call the pattern And this is like the most important pattern for coding I think in AP because it comes up over and over again and not only the multiple choice questions But especially the free response questions. I'm talking about some other loop types Enhanced for loops while loops break and continue and we'll talk about nested loops So let's go ahead and get started with the code, which is I'm sure what you're here for so counting up a for loop So take the following code so system dot out dot print L n and let's say we're gonna do the zero and As I like to say I'm kind of lazy So anytime I could copy and paste on the happy guy and we'll go up to five So let's go ahead and fix this to five four three two and one So now if I go ahead and compile that and I run it I think see obviously we get zero one two three four and Five which okay, we could we could code it that way no big deal But what if we wanted to print zero to a thousand or zero to a million? quickly see that As convenient as copying and pasting is it's not gonna be that convenient in that case So what we can do is we can create a loop in stead So the basic for loop is we do for Int I doesn't have to be I I is commonly used In this context It's called the loop control variable now. We're looking at our starting value, which is zero So for I equals zero Semi-colon and the condition so how when do we want to do this? So as long as I is less than six Now I could have put less than or equal to five. I strongly recommend getting used to using less than you'll see why later and then What is the increment? How is I changing in the loop? So you guys here? It's going plus one plus one plus one plus one so we do I plus plus Okay, so then in our loop we do You can do now do system dot out that print ln and we're going to print I delete that Save it compile it Execute it and we have the same result. So what happens here is That I starts at zero Then we ask ourselves is I less than six? Yes, it is so then we execute this code block So system out print l and I I is zero We reach the end of the code block. We come back around We increment I by one I is now one is one less than six. Yes, it is execute the code block One come back around Increment to two is less than six three four five Then we get to six is six less than six. No, of course, it's not Code block is not executed and we go down to the next line after that code That's it pretty easy. Beasy. So let's go ahead and try this again Counting up by twos should be fairly straightforward. So instead of plus plus we do I plus equals two We go ahead and compile that Run it and you'll see zero two and we start at zero go to two Four then we hit six and since six is not less than six The loop is over We can do the same thing counting down. I think that's a little bit more interesting in a way So thinking about that counting down from five to zero. So our starting value is five Our ending value is zero But keep in mind we're counting down. So greater than I'm gonna put negative one here And we're counting down So it's minus minus I'm gonna compile that execute it and you'll see here. You've got five four three two one zero Notice how I had to reverse the sign because we want this to keep executing while I Is greater than negative one now get I could have done greater than or equal to zero It would have worked just as fine. You should for the multiple choice questions You should be familiar with both styles, but again, I personally think This is more consistently and easily used based on how we use the coding later when you'll see here anything So string traversal so traversal means to go across and so what string traversal means is to Go through each letter or each character in a string one at a time I'm gonna go ahead make a little string here So same thing for int I equals zero Because we're starting at the zero with letter and this should be familiar from previous units So zero one two three four five six seven Okay, so while I is less than this is why I recommend less than name dot length And the length is one two three four five six seven eight. So while I is less than the length We're gonna go up by one. And so what we want to do is we're gonna say string Letter that's a character. It's probably more accurate character Equals name dot substring and if you remember it's gonna be I So in this case it'd be zero one and then oops and one two then two three three four four five So the pattern is I I plus one And then just for now, we're just gonna go ahead and print that out print L and Compile it and run it And name that like cannot find simple name dot length because it is a method. So I need Precy, so hopefully maybe some of you caught that before I did. Okay, so you can see here So we start out I is zero Is zero less than the length which is eight? Yes, it is so the character equals substring zero and One which is T So we print out that character we come back around I plus plus I becomes one So we pull out this character so one and then one plus one is two That's gonna give us the H Print the character and then we go around and round around until we reach the end of the string Now this is The basic pattern you're going to see this especially later when we get to arrays and array lists and 2d arrays You're gonna see this pattern over and over and over again one of the things that lets us do Let's go ahead and just copy that And Here's best It's pictures of you for example So Same thing if I run this now Oops, it's already defined a major. Ah, so because string was already declared up here. I just go ahead and change it to Don't need to clear it again can't declare it again And so you can see how we're going through one at a time. What this lets us do is Let's say I'm gonna say count They count equals zero. Let's say I wanted to count the number of spaces in this string So what I would do is instead of printing you know, so if characters Dot equals quote space quote score plus plus Then after the loop system Dot out dot print ln. There are quote plus count Let's put space spaces So I'm gonna compile it make sure it works before I explain it system system spilled system wrong system Compile it again Count count oops. I forgot to put into oh my gosh Score all right. I guess that's a little early in the morning for me All right, there are seven spaces. Thank God So one two three four five six seven. So again, we are iterating through Okay, we are traversing the string. We're starting with T We pull that out. Does T equal space? No, it doesn't go back around H E we get to a space does space equal space? Yes, it does. We add one to the count And we just keep going all the way to the end of the string And then we report in this case where we report the result So this is what I call the pattern You will see this pattern time and time and time again If you can understand this pattern and how to use it effectively You will do very very well. I think any AP this that it comes up surprisingly often so Let me explain it real quick. We have in this case a collection of characters Later we'll have collections of integers collections of objects and things like that but for now we have a collection of Characters they're combined into a string But we can't just look at the string as a whole we need to look at it piece by piece by piece So we use this pattern So for i in this case i is going to stand for index although it doesn't have to be i could be q nobody cares But stick with i So we start at zero which is here. We go all the way to the length basically minus one And we want to look at every character. So it's i plus plus. So we start here next character next character So this is the key part we pull out I just use the word I say pull out we take out how we want to say it We pull out the little piece of information that we need in this case. It is one single character Then we test it, you know, whatever we're doing with that particular character We test it. So at this point you can really just kind of forget all about this stuff As long as you're going through all of the different parts of this collection, you're going to be fine So in this case, we're looking for a space if it's a space we add one to count Then once the loop is completely over we've checked through the entire string. Sorry Then we print out in this case print out the results So I cannot overemphasize How important this pattern is because you will see it over and over and over again Good, um enhance for loops There's something called an enhanced for loop in ap We can't really use it much here because there is a data type that it requires that we don't really use um called I'm not sure it's called char or care, but uh You know, you might find it useful. I don't know. Um So basically what we got to do is You'll see it later in the arrays unit. Um, so for care I'll say you know character in The string so it was name But we have to do like two. I think it's like two carer array That's why I don't really talk about it much. Um, I can say system dot out that print That print out and character I think that'll work. Let's try it first That's always a good sign and then you see it work there So basically what this does it says for each character in name now We had to convert it to the care type or char type. Whatever they call it using dot to care array That's again, the ap doesn't require you to know this data type So I usually save this with my students at least till the Arrays unit But I thought I'd mention here for those of you who are interested because not everybody is an ap computer science a student so let's have a look out for and you'll see it again later now There is another type of loop called a while loop and everything that we can do with a for loop We can do with a while loop. Sometimes while loops are easier to do Sometimes for loops are easier depends on the situation So just looking at our for loop. We have a starting value condition Sorry condition and an increment your while loop will have the same thing So I can say int i equals zero While i is less than six so the condition is the same system dot out that print l and i I can go ahead and do i plus plus Same thing. It's just in a different everything's just in a different spot Okay, so let's go ahead and test that And you see zero one two three four and five We can do the same thing counting down So it's fairly easy to convert from a for loop to a while loop And vice versa. So again, we're at start of five Well, i is greater than negative one and our increment in that case is negative one compile oops now I got an error here because It's already declared here. You're probably wondering why well, it didn't matter Here and something called scope, which I will get into I think in a later video um So we do methods so basically you can get rid of that to redefine i And there we go five four three two one zero String conversion can be done with while loops. So again int i equals zero While i is less than say name dot length and I'm going to refer the parentheses this time Now the same thing so string character Name dot substring i comma i plus oops i plus one And let's go ahead and try that all right to find Oh, what did I do there? Okay? Now if you ever get stuck like this, I'm stuck in a loop here It's running over over you hit control z And then see this turn if this turns red if you're in that part just hit enter I forgot the iterator. I forgot the increment That would just give me the first letter because I never changed Kiddoki it has crashed. Okay. I'm going to go ahead and close that for a second And I'm gonna go ahead and reopen it pilot and run it and now it's working. Okay. That does happen from time to time Especially when you get stuck in a loop I'm gonna see that happen There are a couple more commands that work with loops One is called break so Let me go back up to here and pull this out So break is used to end a so we say system Out print ln I found a space at index Okay, so I'm gonna go ahead and compile I think we got rid of that since it's already up top Now now it's interesting because I was declared outside Up here now. I can't use it inside. We're just kind of annoying. Uh, so it was For now, I'll just do that. Um So you'll see here Same thing we're traversing the string We're looking for a space and it says I found a space at index three. So zero one two three Let me actually take out break that I'll comment that out to show you what happens Run this So you can see here it it goes through and tells us the index of every single space, which is great Um, if we need it, however, let's say we only did know if there is where is the first space So we find the first space and we want to be done So break will break the current loop. So we hit three. We found a space break Loop is done. We jump down to the next line so, you know Basically, this is like in a case where you just need to know if something exists It doesn't matter how many or you'll only want to find the first one We don't want to keep searching especially if something is long if you're doing like dna analysis You know base pairs can be billions of characters long Let's say you find it early. You don't want to keep searching speeds up your code And there's also something called continue which we can use So I'll just use four I equals zero and I is less than let's say 20 doesn't matter I plus plus And so let's say we want to print the words Or print the numbers that are not divisible by three And so so if I percent three Equals zero A print l and I so let's go ahead and compile that run it. I'll show you what's gonna happen here So you see oops, we've got One two three is divisible by three equally. So we don't want to print that four five six Is also divisible seven eight nine, of course 10 11 12 is missing 13 14 15 is divisible by three 16 17 and 18 is divisible by three evenly divisible I should say Okay, so what continue does so we start our loop And then let's say we get the three 3 3 is zero we continue What happens is instead of going to here and then coming back around it goes straight from here Back to here. So continue. So whatever is after continue is not executed So that's how that works. So break ends the loop Continue continues the loop, but skips whatever comes after it pretty cool, huh? Okay, and the final topic is a nested loop nested just means a loop inside of a loop Okay, so for example for uh, int y Uh equals let's say y equals zero y is less than 10 y plus plus let's see here for int x equals zero x is less than 10 x plus plus And then what we can do here is we could do let's say system print l n y plus race Let's so if we compile that we run it Now we get quite a lot of the output here So you see here, this is y This is x So we come down y is zero The inner loop repeats Okay, y doesn't change. So we go all the way up to nine It's less than 10 Then as soon as we get to 10 this loop ends we come back around And now y is one We come back into here. We do this 10 times zero through nine boom boom boom boom all the way down to the end Just as a quick rule of thumb. So if you think about this, this is going to execute 10 times zero to nine This is going to execute 10 times zero to nine. So since these are nested This will print out how many times? 10 times 10 So this will print out a hundred different times. So if I put like count Equal zero and I just did count plus plus system Out print l n is a count And plus count and run it we should see 100 at the end Okay, so that is a simple way to calculate how many times this will be executed And that's something you you'll probably see in a multiple choice question or two in the AP Alrighty, I think that's it. I think we covered everything Again, this doesn't cover every single topic in this unit My book has my ebook that will be linked in the comments Has some more details. There's a few more things on the AP as you go through the materials You'll see but this covers I think most of the important stuff So we looked at for loops how to count up how to adjust the condition for counting down Strain traversal counting and the pattern again. This is the core of In my mind AP java This you just do it all the time especially those FRQ questions You'll you'll see this coming up time and time again We looked briefly at enhanced for loops. We'll come back to those in a later unit And then we also looked at while loops while loops can be converted to for loops For loops can be converted to while loops. Usually it's just a personal preference There's some cases where a while loop makes more sense But for the AP you don't really run into that too too often Then we looked at break and continue a couple of useful commands that you may find handy here and there And we talked about nested loops and calculating execution The number of times a certain line is executed based on the nesting of the for loops So yeah, that is it. Thanks for watching