Kathryn introduces loops as another way to add decision-making to your code. Then, she shows you how to implement while, do-while, and for loops in Java through an example.
- [Instructor] Loops are another way to add decision making to your program. A loop is a control flow statement that allows code to be executed repeatedly based on a boolean condition. In Java there are three main types of loops. The first loop is called a while loop, and it's flow control looks like this. Here we check a condition and if that condition evaluates to true, we do a series of statements and then check the condition again. If that condition is false, we exit the loop and continue to the rest of the code.
Looking at this example let's say we have a variable named x, and it's initialized to be three and our condition is x is greater than zero. If the condition is true we decrement x by one. Going through this program we'd start with x equals three and since three is greater than zero, the condition would be true and we would decrement x by one, making it two. Now that x is decremented we check the condition again, it remains true, so we decrement x again making it one. One is greater than zero so we decrement x again, making it zero.
Now when we check the condition, zero is not greater than zero so we exit the loop because the condition is false. This loop is called a while loop because we only execute the loop while the condition is true. Let's see what it would look like in code. Here I've added a couple of print statements to make it a little bit more visual when we run the program. We start with x equals three and create a while loop with the condition x is greater than zero. If this condition is true we run the print statement and decrement x.
Once the while condition becomes false, we print out the final x. Let's go ahead and run the program. Here we can see the full execution of this while loop. We had x equals three, three was greater than zero, so we printed out the current x here on line 15. We decremented x, again went through this loop and printed it one, two, three times and then finally when x became zero, zero is not greater than zero, we printed out that final x here on line 18.
The next loop we have is called a do-while loop and it is very similar to a while loop. Here we execute statements before checking the condition and if that condition is true, we execute the statements again. Otherwise we exit the loop. So if we keep this same example that we had before, but change our variable name to y, we would have y be equal to three and the statement would be y equals y minus one. The condition would be y is greater than zero. Here we would decrement y, making it two, and then check the condition versus the other way around.
Here's what it would look like in Java code. We start with y equals three and create a do-while loop with a print statement as well as a statement that decrements y. Then we check our condition which is while y is greater than zero. Once the condition becomes false we print out that final y, and so running this here we are, every time we enter that do we basically print out y. Here it started at three, and then when we decrement it and that condition is still true, we get to two, and one, and then finally when we decrement it that last time, one minus one is zero, zero is not greater than zero here on line 28, and then we go ahead and print out the final y on line 29, which is zero.
Moving on to the last type of loop, we have a for loop. And this loop is similar to the ones we've seen before. With this loop we initialize a variable first, check the condition, and then if the condition is true we execute a set of statements. After executing the statements we increment or decrement the appropriate variable and check the condition again. If the condition is ever false we exit the loop. Using the same example as before but with the new variable called i, we'll start by initializing i to three and having our condition be i is greater than zero.
For our statements we'll just have a print statement and we'll decrement our i variable by one. Moving over to Java here's what it would look like. We start with this keyword for, saying it's a for loop, and then we create this int i, it's our internal variable and it's going to start off as three and while i is greater than zero we're going to go ahead and do what's in between these curly brackets. And so in this case it's printing out the current i. Each time we go around and we do this body of code, we are also going to decrement i by one.
And so let's go ahead and run this. And here we have the current i started off as three. We decremented it, checked the condition. Okay, we'll go again. Current i is two, we decrement, check the condition again. Go ahead and print out current i equals one and then when we check the condition again we've already decremented and so our final i is zero. However, we don't have access to it outside of the for loop and so we're unable to print it out unless it's saved in a variable in the outside scope.
Loops are great for performing a certain set of tasks repeatedly without having to write repetitive code. In the next video we'll look at libraries and how we can import different libraries to add more functionality to our programs.
- Downloading Java 9 and choosing an IDE
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Working with inheritance and interfaces
- Learning lambda