In the scolding lecture, we are going to use all the loops we went through in the previous video first
while then do while for loop, then continue and break.
And finally, we take an example for a for loop in a full loop.
We're going to use Bart Simpson's punishment as an example.
So let's do it first.
Let's create a new class for this.
Right.
Click on one of the files here.
You Java class loops up, copied the main method from one of the files you have been created.
Copy and paste it here, it entered the other curly brace.
OK, let's start with wild.
So as I said, we are going to need a variable outside of the loop.
Call it I and I equals one and a semicolon.
By the way, we use letters for such variables because they don't have any exact meaning.
They just helping to run our code five or fifty or a hundred times next to the wire.
And in the parentheses, I should be less than or equal to let's use five here instead of a hundred,
then curly braces enter system that order print double quotes.
Being right sucks, one very important thing here is to increase the value of I won, so I lost plus
and a semicolon, I think it would be useful to use I and added to the text just to see how many lines
we printed out.
So
I plus.
A dot and the space here also print out the value of eye after the loop.
So.
Double quotes, well, you have a colon plus I let's add a blank line to.
The latest now round the code.
All right, being socks printed out five times and the value of I is six, because that's one, Java
stopped executing the loop.
Great, it works as it should.
Now, let's do it with the wire loop.
Now we have to create a new variable for this.
Let's call it J and J equals one and a semicolon, starting with the curly braces computer code from
about.
Paste it here.
Change the variable name to Jay and here to now wild parentheses, Jay, less than or equal to five
and a semicolon here copied these lines to pasted it here.
Change the text and the variable name to run the code, brilliant.
Same result now change the five to zero and both loops.
So here.
And here now, we're on the road again.
Nice.
So as I said, while check the condition first, and if it's false, Java doesn't execute the loop in
the case of do while it runs the code first, then checks the condition.
You can leave it as it is or you can change it back.
It's totally up to you.
Now, let's move on to for loop, we don't need an external variable here, so for parentheses and I
equals one and a semicolon and we get an error message here.
Variable I is already defined in the scope, so I has already been defined outside of this.
So let's use another variable name.
OK, now, OK, should be less than or equal to five, then a semicolon and Kate plus plus then curly
braces hit, enter and copy.
This line.
To hear change the variable name Touquet, and that's it, now compare these lines to copy and paste
it here, change the text Touquet and variable name as well.
OK, so what's the problem here, it says cannot resolve symbolic, it's right because K is only accessible
inside the loop.
So delete that line and run the code.
Everything is fine, the Texas printed out five times, move on, continue and break and use our fresh
and crisp for loop at and if.
Here, parentheses and K equals free curly braces and continue keyword now run the code again.
So as you can see, the third step is missing Snice.
Now replace the continue with break.
And run the code again.
Now, as you can see, the execution stopped at the third step and two lines are printed out.
Last, let's do a bit weird thing, put a loop into another loop, copied a for loop.
And remove the F from it.
Now, copy it again.
And posted here instead of the system that our DOT printed on.
Now, as you can see, we should change the key here.
Because it's already used, so let's rename it to out here to.
And here change the printed out text to so double quotes, OK, because.
Then a plus an.
Space of equals plus L and the plus sign again.
And later, that changed the condition of Al from five to two.
Now let's run our code.
So the value of K and L is one than the inner loop is executed again and the value of L is increased
to two, then the value of K is increased by one.
And then the inner loop starts from the beginning.
And this goes on and on and on.
Finally, the text is printed out ten times.
We will definitely use loops later for more complex tasks, if you have any questions, feel free to
ask me.
Soon the next lecture.