The title of this lesson is iteration lesson. Iteration, now we're getting into some more interesting aspects of coding. We're not just inputting data and saving it and moving it around. Now we're getting into some things that allow us to do more interesting things with that code and produce more interesting geometric effects in Rhino. Iteration is the first in our control flow family. The name of it is exactly what it does. It controls the flow of data through the code based on a set of rules set up by you the programmer. Iteration is and sometimes called looping, is the execution of certain lines of code more than once. It's really one of the most powerful forms that we have within coding. You're really going to see that when we get into some of the software tutorials, really the advantage that it holds. It's a very efficient structure but you can do quite a bit with it. There are two fundamental types of looping, a conditional or indefinite loop and a incremental or definite loop, and we're going talk about both of those. One could describe the logic of each in a fairly straightforward way. Conditional is you keep doing some operation until some property switches from true to false. In incremental, we have a set number of times that we go through the loop and we have a number of different ways that we can set that. It could be the length of a list or it could be, let's say a number that we give it that we want it to loop to. I'm going to start with conditional looping, but we're not going to use conditional looping that much in this course. It's good to know about it, but it's really not the common one that we use. Although there are some traits to the looping structure that both types do share. This is our line 2 here, to point out. Line 2 is our looping line that initiates our loop, and it always ends in a colon and that's the same for both types. Then anything that's indented below that is part of our loop. Now, for some reason you forget that colon when you're typing this in the editor. When you hit return, you're not going to get the indent. If you don't get the indent, you realize that something is wrong. Let's talk a little bit about the logic of this loop, what it's doing. A conditional loop is asking a question and it's looking for a true or false answer. It has some expression in it and this case it's asking, is x less than four? This is going to return either a true or false Boolean. The conditional loop is saying while x is less than four, while the statement is true, then do what is within the loop, which is the indented part of the code. If I was to get a true statement, if x is less than four, I'm going to go and I'm going to run first line, 3 and then line 4, and I always runs those in sequence. Then when it gets to the bottom of the loop, where does it go from there? Well, it doesn't exit the loop yet. It goes back to the top of the loop. It goes back to my line 2 to that statement and it asks the question again. It's going to continue to ask that question and it's going to continue to do what's in the loop, while that state the answer to that statement is true. This line 4, we can see is adding one to x every time it goes through the loop. Eventually, x is going to be greater than four. I'm going to get a false statement and then it's going to jump out of the loop and it's going to run this, whatever code is after the loop it's going to run, and so here it's saying print done. Now, line 4 might seem a strange statement that I could get x to equal itself. The way that works though in Python is that we could think of that equals as a firewall. It always does the right side of the equation first. I'm allowed to add something to x to do something with x and then overwrite x with that. I do however need line 1, x has to exist prior to this reassignment or else I would get an error because it wouldn't know what x was. That is conditional looping. Our most common form of looping is incremental looping. In this case, I have a list that I've set up of these four, excuse me three fruit names, and fruit is our incremental variable. What that's doing is I'm looping through this list fruits, and what fruit is holding is each one of the elements in the list one at a time. It goes through these and we can see in the print out goes through apple, peach, and pear. What this is asking is this is asking a question just like the while loop, but what it's asking is, are you done looping through this list yet? Each time it goes through it prints out what's in fruit, and then it goes back to line 2 and then loads the next fruit in the iteration variable and it asks, are you done yet? It's not done until after it gets passed pear, pear is the last one, and then it jumps out of the loop and then prints done. Often in incremental looping we use a Python function called the range function. The range function is simply a counter. If I just after a range, if I write one, give it one integer, and it has to be an integer. That is my stopping point within my loop. This is also asking the question, have you reached five yet? If you have reached five, then stop the loop, jump out of the loop, and go to the next line. In this example, is my iteration variable and that's going to be a very common variable that we use in iteration, incremental looping when we're using a range function. The counting unless I define it otherwise, always goes starts with zero by default within a range using a range function. Often we're using the range function in combination with the LEN function. Now we've talked about the LEN function before. The LEN function returns the number of items that are in a list. In this case, LEN would be returning three and it's so it's looping through three times and this is giving me the index number of each element. That's 0, 1, 2 those are my index numbers, and when it gets to three here in the range, and then it jumps out. It's just printing out each one of these calling up the fruit 0, then fruits 1, and then fruits 2, and then when it's done, when it reaches three it jumps out of the loop and it prints done. Often we'll want to print out the index number of the item within the loop. It can be very helpful to see the structure of the lists using an iteration loop or incremental loop to do that. In this I've just added print i, and I can separate things that I want to print out by commas and then just for legibility, I've added a colon here to separate those. Now I'm getting the index number and the data that's being held within the loop. The range function, we can do a couple of other things with the range function has a couple of other abilities. If I add a second number here that creates my start position. Now, my start begins at two instead of zero. I can if I add a third number, that is my step. Now it's starting at two, it's stopping when it reaches 10 or more. I get a count that starts at two and then goes by threes, and then of course the next one here would be 11. When it gets to 11, it's already out. It stop because it's greater than 10. Then a shorthand for remembering our start stop step is the shorthand for remembering the range function. Remembering that stop comes first and then start and then step. Often people ask well, how do I get a step if I want to start at zero? Well, you just have to do zero and then whatever your stop is, and then your step. That's iteration, it can seem a bit confusing at first because there are a number of things that are involved with it, but we're going to go through a lot of examples and we're going to use it quite a bit in the course, and so you're going to get the hang of it in no time and you're really going to see the advantage of using it.