Now, we're going to look at an alternative to the while statement called the for-loop. It's just another way of formulating loops that sometimes make a program clearer and easier to read, just more compact and understandable code. So, with the for-loop, it has a number of components. The first thing that we do is execute an initialization statement, then we evaluate the Boolean expression. If it's true, we execute a sequence of given statements, and then we execute an increment statement, and we repeat that. It's a language structure that is set up in this way that is actually equivalent to a while loop. So, here's an example of the use of a for-loop to print the powers of two, from two to zero to two to the n. Initializations statement, well we have a variable V equals one that's outside the for-loop. But the for-loop itself, inside the parentheses, has initialization statement, Boolean expression. Inside the braces is a sequence of statements, and then the last thing inside the parentheses is the increment, and it does these things in this order. So, actually, every for-loop has an equivalent while loop, where there's initialization statement, that in the case of the a while happens right before the while loop. There's a Boolean expression, in both cases it gets tested, and there's an increment statement that appears after the sequence of statements within the body of the loop and repeat. So, it's just a little bit more compact and understandable code, and we'll look at a few examples. So, here's computing the sum of the first N integers. For (i=1; i<=N; i++) sum+=i. It's very simple computation. So, this is a trace, and it works just exactly the same way as a while loop. Compute N!, same thing with multiplying. So again, very simple computation and naturally expressed with the for-loop. Very often, in a for-loop, we're really keeping track of something by counting it, and so the i=1, i<=N; i++ is quite common. So, this is just an extension of that. Maybe a table of function values, and this is 2 pi k over N, for k from 0 through N. That way, we can quickly get the values of interests printed out with just one statement. That's a for-loop. Here's another example of the use of a for-loop. The problem that we looked at last time for printing subdivisions of a ruler. So, we want to print out this 12131214123121. So, given N inches for every value of i from one to n, we want a sandwich i between two copies. Let's take a look at what it looks like. So, this is the computation that we looked at last time, where we simply had a new statement every time we wanted to expand the thing. Now, we put it within a for-loop. We start with a blank. For i from one to N, we put i between two copies of the last ruler that we computed. So, for the last one, where i is four, in this case we already have ruler equal to 1213121, we put a copy of that ruler, and then we put the value of i, and then we put another copy. Then, once we're done, we just print out the whole string. So, this is just the value of ruler shown in the trace after each value of i. So, type Java ruler 4, we get our answer. That's fine. Now, what's interesting about this computation and worth reflecting about all the time when you use loops is that, you can really express a huge amount of computation with a small amount of code. In this case, pretty easy to produce a huge amount of output with this program. It's a tiny program. What happens if you say Java ruler 100? Well, you're going to run out of memory because what you're saying is print out 2 to the 100th minus one integers in your output, and that's way more integers that could possibly be printed out in this universe. It's worth reflecting on that. That's what I meant. When we have loops, were going to infinity, and just to cement your understanding of loops, or to think about it, let's say, how do you figure out what this program prints? Well, again for the first many, many loops that you write, what you're going to want to do is trace the operation of the program by writing a table with the values of all the variables as the program proceeds. After awhile, you'll probably see a pattern or at least develop an understanding of what the loop is supposed to do. So, in this case, here's what it prints out, and if you look at those numbers, you might recognize the Fibonacci numbers, and then maybe find some other way to convince yourself that that computation, f=f+g; g=f-g, actually winds up by giving us the Fibonacci numbers. But for now, it's just another example of the use of a for-loop, and we're going to see many, many more examples. So, it's worthwhile to cement your understanding of what a for-loop does.