(upbeat music)
- [Narrator] 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 print it 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 the 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
that 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 the 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 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 two 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 a new variable called i,
will 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 key word for saying, it's a for loop.
And then we create this into i, it's our internal variable.
And it's gonna start off as three.
And while i is greater than zero, we're gonna 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're 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, check 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.
(upbeat music)