 Hey, welcome to intro to AP computer science for new AP teachers topic today is iteration which is perhaps one of the most important and also one of the most difficult topics for students So in this video, I want to go over what is iteration talk a little bit about for loops straining traversal what I call the pattern enhanced for loops while loops and a quick introductory exercise But talk a little bit about pseudocode in this case. So what is iteration? So this is basically repeating a code based on some sort of condition So until your homework is done, you keep doing your homework ideally A lot of this times this will involve traversing a collection And we'll see that in this lesson in traversing a string It is absolutely a key concept in multiple choice and free response questions If you cannot do iteration properly, you cannot do traversal properly You're gonna have some serious problems. So it's really important that students get on board with this particular topic and again This is from my own experience. This is the hardest subject for beginners. It's harder than methods It is harder than classes. It is harder than basically anything I don't know why it just it just seems to be very very complicated And once you get it, you get it, but it is a very complex topic for beginners So for loops, so basically, you know, you just want to go over the basic structure, you know for I equals 0 You know, then you have your condition I should probably should written that in here and then you have your increments So again, assuming you already know this and then yeah, do a little bit about counting up counting down Looking at different types of ending conditions and typically one of the things, you know You students really want to use all times They want to use greater than or equal to rather than greater than or they want to use less than than equal to Rather than less than typically I just show them I really emphasize the pattern for I equals 0 I is less than You know, whatever that maximum number is usually it's a length because we're iterating through some sort of collection could be a size or You know that sort of thing, but I strongly recommend the students basically stick with that pattern and Not not given to using a less than or equal to greater than or equal to Although they should be aware of it because some of the free or some of the multiple choice questions We'll have that in them And if they if they don't pay careful attention to that then they'll be off by one and they won't get the correct answer So it's a little bit tricky there Traversal strange reversal in this case traversal means to go across Okay, so we're starting at one end and we're going to the other end Typically we're starting at the beginning so that would be index zero and we're going to the end Which is the index length or the length minus one is the final index So it's important to go over that and you'll see here I have the expression starting with the zero with letter and going to the end So not the first letter the zero with letter It's a little confusing because of course in our heads We think the first letter is the first letter, but that's index zero. I think helps the students to get the idea Okay, it's zero and then What's really important here is that you're looking at one letter at a time and doing something with it So you could be checking to see is it is this a capital letter Is this a lowercase letter if you're counting or something if you're filtering We only are counting how many vowels there are is it a vowel. We're going to check these types of things So this this is very much a standard pattern and that's which brings me to the pattern and this is just Really just the most important pattern a PCSA. I Hammer this starting with this unit and I constantly bring it up over and over and over again I do explicitly teach this as a pattern So the steps are you're starting with the collection now in this particular case The only collection type we know is a string. I know it's technically not a collection But you can treat it as such you iterate through it. That's your for loop You pull one element out at a time now so like You know string is made up usually characters or letter. We want to call it so, you know character equals You know string dot substring i comma i plus one and then we have the letter Then all then we don't think about the iteration anymore All we're gonna do is say well if this is a vowel we add one to our vowel count If it's a consonant we add one to our consonant count But this pattern if the students can get this down it will go a real long way to Just doing well on an AP exam and also once they get to the array unit once they get to the array list unit They already know how to do this and it's just really just you know We'll learn new algorithms and different types of those types things you got to do there and enhance for loop This is not really Something I do here in the string unit, but I do mention it. It's in my book as well This is a special type of for loop So in which you don't use indices, but you still in iterate through from start to finish However, in the case of strings you need to use the char care data type and since that's not on the AP I don't really talk about it here too much But it's something maybe you want to you want to you might want to bring up depending on where you're at And you know your personal teaching style so for this reason I as you see here I wait until you raise unit to introduce this topic generally speaking And then basically I do all the for loop stuff I just all I talk about is for loops all I talk about is for loops all I talk about is for loops We get the idea of iteration we get the idea of traversal down we do counting You know you can do some filtering all the only sorts of different things once they master that basic concept Then I go on to while loops So then we just go through everything well How would we do this with a while loop and what are the structural differences in the big? I think one of the big things they have to watch for especially with regards the multiple choice questions on the AP exam is You know where does that increment or go because sometimes you'll see you know I plus plus it comes before Let's say a print statement or sometimes it comes after a print statements Sometimes it comes before an if state in a conditional sometimes it comes after and that really like changes the output for those multiple choice Questions and they're looking that's they're trying to see if you understand You know what how that affects the output of the program? So you really got to know that type of level of detail, but it's something definitely emphasize and for the International exercise basically what I do is have the students brainstorm a long list of things that they did that day Or could be that week doesn't really matter and then we categorize them into fun and not fun so this this is really more of a thought experiment than a Coding experiment and because we can't really do it until we get to Arrays so then I introduced the idea of pseudocode which is code that looks like code But isn't it it's really just explains how the code works You'll see an example on the next slide and so then what we do is we calculate fun versus not fun So for how fun that day is based on the number of activities that are fun versus the number activities that are not fun And then looking at it so you can see activities This would be our list of activities and then I would go through okay Here's our list we start fun is zero not fun is zero and then for each activity So this is our loop indentation is important if the activity dot is fun equals equals true So it looks a little bit like code that they're used to then we add one to fun Otherwise we add one to not fun. Yeah, this is not code. That's meant to run It's code. That's meant to explain an algorithm later in the arrays unit where we do Selection sort and insertion sort. I do give the students the actual pseudocode for those algorithms And from that pseudocode they have to actually write codes. This is something I do test It's not in the AP pseudocode is not in the AP But I think it's important to for the students to see that process and how everything breaks down And especially you see here the indentation is really useful in giving clues to how this would work And then basically each student can say well my day was you know I had a 10 score of fun and but a 20 score of not fun or whatever whatever that particular thing was Or you do it as a class a collective and just gets the students talking and thinking About the process of doing that sort of thing then later, of course, you're gonna turn that into code Topics so to review talked about what iteration is and just how important that is for students to understand For loops strange reversal and the pattern if they're if nothing else getting the students to understand this pattern Because it comes up over and over and over again Again, he has for loops I'd probably wait till the arrays unit just because we don't use the care type in AP But that's up to you and then boom redoing everything with a while loop And finally a little exercise to kind of get the students thinking about that process of you know looking at one item at a time Here's an activity. Hey, is it fun or not fun? Okay, tallied up. We come back around look at the next activity It's helpful if you you know walk through that that pattern that process, you know step by step with the students And I think that is it for this one. Thanks for watching