So why do we need Loop's, because there are many cases when you have to do the same things again and
again and again, when I think of Loop's Bart Simpson comes to my mind when he is punished to write
a sentence on the blackboard a hundred times in code, it could be done by copy paste in the same line
a hundred times.
But I think most of us would miscounting after 30.
But it can be done much easier with loops.
What are loops?
Loops are repeating code until a specific condition is met.
In the case of Bart Simpson, the specific condition is reaching a hundred.
Now let's see what we are going to cover in this lecture.
There are a few loop types in Java.
First, we will look into why then do y loop after those for loop.
And finally, I will tell you what and how continue and Brachy words can be used.
The next lecture will be a coding lecture and I will give you examples to all of these.
First, let's look into while loop.
It starts with while then a condition in parentheses and the code that will be executed repeatedly is
in curly braces.
Java will keep continue executing the code until the condition is true.
I think an example make it more clear.
Right.
So we have to print out being right.
Sock's a hundred times.
So first we have to define a variable for the loop strictly outside of it.
We will call it AI and its value will be one then the wire loop with a condition that it should be less
than or equal to a hundred.
And the code in the curly braces.
After we print out the text, we increase the value of AI by one.
After the value of AI is increased, we get back to while and Java checks, whatever.
The condition is still true.
If it's true, it can continue executing the code inside the curly braces.
When the condition is false, it continue executing the code after the loop.
It's really important to keep in mind that the value of I must be modified in the loop.
If you don't modify it, the condition will remain true and the code will never stop and we get into
an infinite loop.
Later, we will use the loops for more complex code than just printing out something.
So now let's move on to do while it's form a bit different from while first do word, then the curly
braces with the code and then the wire keyword with the condition and last a semicolon.
Now, let's see an example we used in case of while transformed into a do.
While the biggest difference between a while and dubal is that while checking the condition before it
starts running it and if it's true, it will start executing the code.
If it's false, then it will skip the code in the curly braces do while runs the code first without
any checks and tests after the first code execution is finished.
So the code will be executed at least once after the conditioned check is finished.
The execution starts again from the beginning of the do while loop.
It's also important to keep an eye on the value of AI and don't let the condition be true all the time.
Moving on to for loop the form of the for loop is the following.
First for key keyword parentheses, then initialization, then semicolon.
Next is a condition, then a semicolon and an updated statement and lost the code in the curly braces.
Let's take our example we used before.
For Int equals one semicolon, I must be lesser or equal to a hundred than semicolon and I plus.
Plus and the code.
The big advantage here is that we don't have to create a variable outside of the loop and we don't need
to increase the value of AI in the loop.
So for Loop is much cleaner because we can do all the administration in one line and it is the best
choice if we know how many iterations we have to do after the code is finished, we get back to the
first line of the for loop.
First, it increases the value of AI and then checks the condition.
If it's true, it keeps running the loop.
If it's false, then it will stop the execution of the loop.
An extra thing here we can put a loop into another loop in case of our example code.
The text will be displayed a thousand times.
First, the inner loop will be executed and the text will be printed out ten times.
The value of I will be increased to and the inner loops code will be executed ten times again.
And it goes on and on and on.
You can also do this with while or do while.
Now the two key words before the coding part.
First one is to continue keyword.
It is actually for skipping steps and loops in our example.
If you use if I know I haven't talked about it, but I think you can imagine what it does.
So the loop goes on and when the value of AI is ten, we skip the tenth print statement, but the loops
execution goes along and at the end ninety nine lines will be printed instead of one hundred because
we skipped at the tenth.
The next one is break.
Break is for different purpose.
It stops the loop.
We use the same code, but I change the continue to break.
So after printing the text nine times the loop will be stopped.
Break and continue are not used very commonly, but there are cases where they are very handy.
That's it.
Let's continue with the coding part.