Fundamentals of programming. Programming elements, looping. It is often the case that we want to repetitively execute the same passage of code. Different programming languages offer different options. But in general, there are two different types of loops. We have loops that are based on a predicate. They continue to loop for as long as that condition is true. There are loops that operate on each element from a data set. Depending on language, we may have two or more different types of loops. C and C++, for example, don't natively support looping across a data set. They simulate it with a fancy form of a conditional loop. As with branching, different languages will delineate the code blocks in different ways. A conditional loop is a loop that evaluates a Boolean expression and will continue to loop for as long as that condition is true. If the condition is never false, this would result in an infinite loop. Meaning that it would never terminate, aka stop. Now before we can talk about looping over a data set, we need to have at least one way to define a data set. For our purpose, we will define the common array. An array for our purposes is an ordered sequence of elements. Each element has an index and can be read or written using that index. Most programming languages start with an index of zero. However, some start with one. Since most languages do start at zero, that might be something you'll need to get used to. For example, the maximum index is therefore one less than the length. For this example, we would have indices of zero for the eight, one, two, three and the length is four. To access the eight, it would be pie sizes and notice this using the square brackets, zero to get the eight. Pie sizes with the square brackets of one is the 12 and so forth. Now, this example is written in Python syntax, but other languages will generally have a similar syntax. Now that we have the idea of a data set using in our case in array, we can talk about looping over that data set. Now some languages, Python included, have a for-loop, or perhaps it would be better to call it a for each loop. That loop defines a variable that each time we go through the loop will reference the next element in the data set. Now the actual mechanism by which the for-loop works can differ from language to language. In the case of Python, it's actually far more flexible and powerful than we're going into, but this is the net effect for our purpose. Other languages, C, for example do not offer such a loop over a data. C simulates it, but it's so-called for-loop is actually just a fancy conditional loop. C++, Java and ECMAScript all offer that same type of loop taken from C, which is why it's good to at least know that it's there. Java also has a second form of its for-loop that is a proper looping over a data set loop. Now there are other ways in various languages to go over a data set. We'll use the term iterate, to iterate over a data set. In many cases, I prefer them to the basic for-loops. But for this course we're just going to keep it to the simple use of a for-loop. Let's take a look at it in Python. Here's our area of circle example. I've already added the additional code we need for our demo rather than having you watch me type it. First, I added another function, price of round pizza. Notice that the function calls our existing function area of circle, because we're going to charge five dollars a pie plus five cents a nickel for each square inch of the pizza. Next, I added a data set with four different pie sizes. Next is a little bit of a contrivance, because I wanted to show a while-loop and a for-loop. This is just pedagogical. You can probably would never write this code in exactly this way. But here, my condition is simply the value of the variable show and it defaults to true. The first time we come to this while-loop, this is our conditional loop, show is true. So we enter the loop and then here is our for-loop. For each size of the pie, we will execute the loop. Each time we go into the loop, we will have a new size. We will calculate the price of a round pizza for that size. The print statement here is taking advantage of a fancy new feature in Python that makes formatting output easier. We'll leave a detailed explanation of it for if you should ever take a Python course. But for our purpose, suffice it to say that it prints a string where these braces with the symbols inside of them, will be replaced with the actual value of the symbol as we go through the loop each time. This funny looking two two F at the end with the price, is merely a means by which I can get the number to look like dollars and cents. Again, we'll leave any further explanation for a proper Python course. For now, we can just go ahead and run it. After it displays the prices, it's going to ask me a question. Would I like to see it again? I'm supposed to answer yes or no. If whatever I give back is in this set of yes or yes, upper and lowercase, then show would continue to be true. Anything other than an upper or lowercase y, and show would be false, thus terminating the while-loop. Having reviewed the code, let's go ahead and run it. Here we see an eight-inch pie would cost, and a 12, and a 16, and a 20. There's one line for each element of our data. There you see the prompt. Would I like to do it again? Let's say yes. It's done it again and asked me again. This time I'll say no and the program is complete. At this point, I'd like to say, congratulations. Look at this program. We have defined two separate functions. We've passed values into the functions using parameters. We've returned values from the functions. We have variables. We have expressions. We have branching. We have looping. These are all the fundamental programming elements. The fundamental basics of programming. Everything else will build on this foundation.