Transcript for:
Mastering For Loops in Carol

hi in this video we're going to look at four loops in Carol so let's say you're getting really excited with all the new carol commands that you've learned and you want to do something crazy you want to put down 100 tennis balls so how would you do that you think we would do something like this football football football football and keep going a hundred times but probably wouldn't want to do that so what we're going to want to do is introduce the for loop the for loop let's carol repeat a section of code a fixed number of times so let's repeat that again a for loop means repeating a fixed number of times for loops are an important part of programming it in all programming languages have four loops so what does a for loop look like well it looks like this there's a lot of syntax but there's only one important part for you to focus on that's the count variable that tells our computer how many times to repeat what we're saying is repeat the indented code count number of times similar to our functions all the code that we want in the loop to get repeated gets indented one level and just like functions once we stop indenting that code is no longer part of our loop so looking at a specific example if we want to place ten tennis balls down then we write for I in range then we put the number ten inside parentheses followed by a colon next we put our code that we want to repeat indented in this case football if we want to repeat 100 times it would look very similar only the number in parentheses needs to change so let's take a look at this in the code editor okay so let's take a look at our for loop in the editor so what we want to do is have Carol move across our screen so we could just type move and then kind of copy and paste this down and we'll see Carol move I say nine times across the screen and we do that and Carol kind of moves across the screen but we don't want to necessarily do this where we have to type out these commands what we want to do is put this into a loop so we're going to delete all of these or just leave that one right there go back and reset and now we're going to put our loop in here so and remember our syntax says for I in range and then we put our number of times that we want to repeat inside our parentheses followed by a colon then we tap that out and now if we run we see karo does the same thing nine times perfect okay so let's take a look at another example of Carol putting down tennis balls so again we're gonna use our loop so for I in range and we'll start with let's say putting ten tennis balls down so now if I want to put ball down we'll say put ball okay now remember if we do this we're not going to necessarily see him so we want to got Carol to move off of that spot so we can see how many balls that Carol put down and so what we need to do is have them move after the loop now we don't want that move to be inside the loop so notice how we put it right here not indented and so the indented part is what runs in our loop this will run after our loop completes so let's go ahead and give that a try so there we go and after ten Carol moves off that spot and we can see we have ten tennis balls again really easy if we want to change that to 100 and reset and it will speed that guy up we can go there and you get always going to put down 100 tennis balls and then move off our spot there we go and that's it now it's your turn to play around