Next, we're going to look at the while statement or while loops that really take us to infinity in computation. Now, the point of the while loop is to execute certain statements repeatedly until certain conditions are met. So, like the if statement, we evaluate a Boolean expression. If it's true, we execute a sequence of statements and then we just repeat that until it's the case that the Boolean expression is not true. So, here's a simple example that we're going to look at in a lot more detail that shows a while statement with curly braces and three statements that get executed as long as the Boolean expression is true. We'll look at this one in more detail in a minute. This is what the control flow looks like. It's like a straight line program except that there's a loopback for the case. Once the statements are done, there's a loopback to check the Boolean expression. If the Boolean expression is eventually false, then we go and continue the control flow after the while statement. It's a very natural way to express a computation that you want to repeat again and again. Let's look more closely at what happens. So, this is that same code wrapped in a full Java program with a main and so forth. It takes, as from the command line, an integer that we put in a variable, n, and then sets two variables, i and v, and goes into a while loop. What this program does is print powers of two. Nowadays, if you Google powers of two, you might have come across this game. So, what happens? So, we set i to zero and v to one. Here's a trace of this program and operation. Do the Boolean expression i less than or equal to n, in this case, n is six, i is less than or equal to n. So, the statement is true so we go ahead and perform the statements inside the while loop. First thing says to print out the value of v, so that's a one. Now, we increment i to two and we multiply v by two, that gives us the next power of two. Then we check is i less than or equal to n, it's still less than or equal to n so we print out v, that's the next power, to increment i. Compute the next power of two. Are we at i equals six yet? No, it's still true that i is less than or equal to six so we go ahead and print v, increment i, compute the next power of two, and we just keep doing this until we get to i equals six. Now, we're at four, we print out 16. Now, we're at five, compute 32, it's still true. Print out the 32 and now we're at six, and we compute 64, it's still true so we print it out. Now, we get to i equals seven. We go ahead and compute the next power of two. But now, we test, is i less or equal to seven, in this case, it's false. So, that means we do not do the statements in the while loop and we just go to the end, and that's the end of this program. So, that's a program that prints powers of two. We can put any integer in it and get a list of the powers of two. That's prototypical of lots of different programs where you might want to print out a value and then a function evaluated at that value. Okay, so just to get used to the idea of a while loop, but take a look at this code and see if you see anything wrong with it. Well, of course, what's missing in this code is the braces. It's not uncommon for beginning programmers to forget about the braces. In some programming languages like Python, it's enough to indent, you don't need the braces. But in Java, you definitely need the braces. If you do that, make that mistake, it's worthwhile to think about what might happen. So, that's the next thing is what does this program actually do when we run it. If you were to type in that program and forget the braces by accident, you have to cope with the situation. Well, what it does is it says it sets the i to zero and, say, n is six. As long as zero is less than or equal to n, it's going to print out v. That's an infinite loop, there's no way for it to get out of the loop. If you compile that program and run it, it'll just keep printing ones. So, one of the first things that you want to think about before writing a program that uses loops is what do you do to stop your machine in the case that it's in an infinite loop. In old machines, we just pulled the plug or power off. Nowadays, depending on the computer and the system you're using, usually something like Control C would work to stop it, but that's something that you're going to have to look up for your own particular machine. Let's look at an actual application where having a while loop is going to be able to enable us to perform a computation. It's a classic problem called implementing the square root function. So, how does Math.sqrt implement it? So, I want to type java Sqrt, this big eight-digit number and I want it to tell me it's 7777 or square root of two, 1.4142136 like that. So, how am I going to implement that function? Well, there's a famous method due to Newton for computing square root of C that is a fine method for this problem. So, what we do is we initialize the variables, so we'll call it t_0 here to C. Then what we're going to do is we're going to compute t_1, t_2, t_3, and so forth. Each time, what we're going to do is set the next one to be the average of the previous one and C over that one. We're going to keep going until t_i is almost equal to C over t_i up to the desired precision. Now, notice if we get them to be exactly equal, if t equals C over t, then t squared equals C so t is the square root of C. So, we're getting towards the square root of C. So, here's just an example for square root of two to seven places. So, we start with i equals 0, t_i equals two, initialize t_0 to C. Two over t_i is one, and then we average and that gives us t_1. So, 1.5 is our first approximation to the square root of two. So then, t_1 is 1.5, two over 1.5 is four-thirds, 1.3333, and if we average those, we get 1.4166667. That's already square root of two to a couple of places. You do it one more time, then t_2 is 1.4166667, two over that is 1.4117, and we average them and now we have square root of two out to about five places. One more iteration gives the square root of two out to seven places, where t_i and two over t_i are equal out to seven places. So, that's the method that we're going to use and that's a classic method. It's important to remember that computation was studied way before the onset of the computer. Scientists throughout classical antiquity, for many centuries, have needed to perform computations in order to understand their mathematical and scientific models. Without a computer, they were very interested in efficient methods because all these calculations were being done by hand. So, we definitely want to benefit from the knowledge that was developed over the previous centuries about calculations and this is a fine example of that. So, let's look at what the method looks like. It's just implementing in code using a while loop, this basic iteration of the Newton's method. So, we're going to take a variable, Epsilon, or capital EPS and, say, 15 places, we have a computer, might as well let it run and get us a nice accurate answer. We'll take the number that we want to take the square root of from the command line, so that's a double, and that'll be C. We start our variable, t, to be C, and then we just continue doing the while loop. What's the condition where we terminate the while loop? That's when t minus C over t, say the absolute value of that, as long as that's bigger than Epsilon times t itself, then we're not within Epsilon of the exact answer, so we want to perform the iteration. The iteration is just average C over t and t, so let's add them and divide by two and then go ahead and print the answer. So, that's Newton's method and that achieves the goal of computing a square root, in this case, two to 15 places. A very simple computational method solves an interesting computational problem with just a single while loop. I'll admit some math or those of you that are interested in the math can fill in some of the details. Why does this method work? It actually works to find the root of any function, f of x. That's the value of x for which f of x equals 0. So, for square root, we'd say in this formulation, f of x equals x squared minus C. So, this is a drawing of the curve. What this method actually does in our general method that reduces to the one that we just showed for square root is draw a line tangent to the curve. So, that's our first guess, we draw a line tangent to the curve and that brings us a little closer to the actual root. So, that's our next guess, and again, we draw a line tangent to the curve and actually, rather quickly, we get an estimate of the root. You can do the math to see the basis for the method and see that the calculation that I just showed for the square root of two gets the job done. That's a little bit of math but our main point for this section is the while loop is extremely useful for performing computations that required iterative calculation.