[MUSIC]
>> Greetings everyone, and welcome back to
Microsoft's Java for Beginner Series.
My name is Brian Benz and I'm a Cloud Advocate at Microsoft.
I'm joined by RJ, who's going to talk to us about loops.
RJ do you want to introduce yourself
and tell us a little bit about loops?
>> Sure. Thanks, Brian. Hey, everyone.
My name is Rajasa Savant,
I also go by RJ and I am a Software Engineer here at Microsoft.
Today we look at examples and syntaxes for the while loop,
the do-while loop, and the for loop.
We have to learn about
which loop you can use when, so stay tuned.
Loops in Java are used to
repeat certain code or certain blocks of code,
or set of instructions for a certain number of times.
A loop generally has certain components;
it's got the initialization expression,
the test expressions,
an update expression,
and the body or code block that needs to be executed.
A type of loop can have either
all of these or some of these components,
so we're going to talk about
two basic variations of loops in Java,
the while loops and the for loops.
We're going to go through a couple of
nuances and syntaxes for each of these.
Let's talk about the while loop.
The while loop is an entry controlled loop,
which means the entry into the loop is controlled by a condition.
Now, you will see an expression in
parenthesis that evaluates to either a true or a false value.
Now the loop are
the statements and code block within the parentheses here.
It's going to keep running as long
as this expression here evaluates to true.
Now, this loop will never run at all
if the condition is false in the beginning.
If the number of iterations is not fixed,
that is when it's recommended to use a while loop.
In this example below,
we are going to print the numbers below five,
so as you see, the variable number has been initialized to zero.
When you check this condition for
the first time before entering a loop,
you see that the value in
the variable number is less than five because it's zero.
That is the reason it's evaluated true,
and hence these set of
instructions or code is executed one by one.
After the control reaches here,
it's going to go back and recheck this condition.
Now, the value in the variable number is one,
which is still less than five.
This goes on till the value reaches to a level
that makes the statement to be
false so when number becomes greater than five or equal to five,
that's when this loop will not be executed.
Now, what is an empty loop?
Empty loop is typically used in adding
time delays or as time delay loops, so as you see here,
it's an empty loop because there are
no statements or code block to be executed,
so all this does is adds delay into your program.
Now, what's an infinite while loop?
As the name suggests,
it's an unending loop.
The reason it's unending is because
this expression here always evaluates to true, so as you see,
we do not have an update expression in this code,
because of which this value will always be true,
and this statement on this code block will
always be executed continuously.
We need to be very careful that we make sure that
the update expression to exit a while loop exists.
Now, let's move on to a do-while loop.
A do-while loop is an exit controlled loop and you'll see why.
A loop, that's a do-while loop,
will always be executed at least once,
even if the condition is false because the code block to be
executed is before the condition is tested,
so if there is a situation where the number of iterations is
not fixed and you must have to execute the loop at least once,
that is when it is recommended to use the do-while loop.
In this example here,
we are printing out the number
at least once before checking the condition,
so even if we would have initialized
this variable number with a five,
this block of code would have been executed
once before testing the condition and exiting out of the loop.
Let's move on to the for loop.
We're first going to talk about
the classic style or the C style for loop.
Now, if the number of iterations is fixed,
that is when it is recommended to use a for loop.
It's an entry control loop because the entry into the loop is
controlled or the entry into
the code block of interest is what is controlled by the condition.
Now first we'll talk about the classic or C style loop here.
We have some initialization,
some increment or decrement,
which is the update expression, and one condition.
The classic loop is good for working with arrays or
ArrayList variables that you can iterate
through the elements of the array using the index.
If you look at this example here,
we are initializing the number,
we are checking the condition,
and this is the increment of the update expression
which makes the loop exit after it is checked again.
In this particular example,
here we are using an array of strings to test out or
to print all the names
that are there as a part of the string array.
The nested for lop. The nested for
loop is a loop inside another loop.
Here what you can see is you can
nest one loop inside the other and each time
the inner loop will execute completely before moving
on to the next iteration of the outer loop.
Just for experimentation, you can try and
print or copy this code and execute it,
and see where you go.
Let's go into infinite loops now, infinite for loops.
In this case, the test expression never evaluates to false.
This is a test expression, and as you see,
the update condition does not exist,
we have a semicolon here,
but the update condition does not exist,
which means I will always be equal to one and always be equal to
less than five because of which this loop or
this set of instructions will keep on executing.
Now, Java also includes another version of for loop,
which is introduced in Java 5.
Enhanced for loop provides a simpler way to
iterate through the elements of a collection or a map.
It is inflexible and should be used
only when there is a need to iterate through
the elements in a sequential manner
without knowing the index of the currently processed element.
Also, note that the object
or variable is immutable when enhanced for loop is used.
What that means is, it ensures that
the values in the array cannot be modified,
so it can be said as a read-only loop,
but you can't update the values as
opposed to other loops where the values can be modified.
This is easier to use than the simple for loop
because we don't need to increment value
and use any subscript notation.
This works on the basis of elements and not the index,
so it's simple as this.
It returns elements one by one in the defined variable.
This is the defined variable here,
so what we see is this new linked list,
and we've added two values inside the link list,
and we're using a simple iterable that iterates through
these values and it is referenced by this variable name.
With that, we have covered the for loops.
>> Thank you very much, RJ. That was fantastic.
Please remember to check out the rest
of Microsoft's Java for Beginners series.
>> Thanks, everyone.
[MUSIC]