We can use for loops to run code many times.
So far, you learn to store that information using variables, control how your code runs, using a
false and switch and organize your code into functions, every step that you take is a step closer to
becoming a professional Java developer.
And your next step to Java greatness is to control how many times a piece of code needs to run, essentially
by running your code in a loop.
So in this lesson, I'm going to teach you how to use a for loop.
First thing you need to do is create a new Java project named Section five and wow, time really does
fly, doesn't it?
We're already at Section five.
And inside the project, create a new file named for Leubsdorf, Java, and as always, make sure the
class has a main method.
Loops can control how many times a block of code needs to run, and there are two types of loops.
There's the for loop and then there's the wire loop.
As we dive deeper into the section, I want you to keep this in mind.
Use a for loop when you know in advance how many times you need to run a piece of code.
I'm going to repeat this one more time, use a for loop when you know in advance how many times you
need to run a piece of code.
This is really important information that I'm going to put in your cheat sheet, OK, and use a while
loop when it's not clear how many times you need to run a piece of code, because unlike the for loop,
the wire loop doesn't have any fancy counters to keep track of how many times a chunk of code needs
to run.
It's just going to keep running the code as long as some condition remains true.
So we'll start with the for loop.
The Furler can run code a specific number of times, it loops through code for an exact number of times
and then the loop breaks.
Now, the for Loop has three parts.
There is the start, this loop starts running with I equalling one, and then there's this top condition.
This loop is going to keep running as long as I is less than or equal to three.
And then the loop stops.
Then there's the step size I is going to increase by a step size of one every time the loop runs.
Now, this might seem like a lot to digest, but it's going to become easier once we start using for
loops in our code.
So we're going to add this for a loop in Maine, the for loop is going to start running with equaling
one and I equals one.
And it's going to keep running as long as I is less than or equal to three, and every time it runs,
we're going to increase the value by one.
OK, so my stop condition is I is less than or equal to three because I want the code to run three times.
You're going to see exactly how this plays out really soon, but every time this loop runs, we're going
to print the counter I print line I.
I'm going to run the code.
And as I predicted, the code runs three times, the code keeps running as long as it is smaller than
or equal to three and then the loop breaks.
So the counter starts at 1:00, the condition is true, the code runs and after the code runs, AA plus
plus increases the counter by one.
Now we're at the second run and this is going to keep happening as long as I is less than or equal to
three.
Once I isn't smaller than or equal to three anymore, the condition is going to be false and the loop
is going to break.
Now, here's how it looks from a visual perspective, the for loop starts with a counter.
I have one.
So the condition is true and the code runs in after the code runs, I plus plus increases the counter
value by one.
The counter is now two.
And this is just going to keep happening until I is no longer less than or equal to three.
And once that happens, the condition is going to be false and the brakes were essentially designed
iroko to only run three times.
In this lesson, you coded a for loop, and it controls how many times a block of code needs to run.
Philip is useful when you know in advance, when you know exactly how many times you want to run your
code.
We wrote a for loop and we designed it to run exactly three times the for loop starts with equaling
one and the loop keeps running as long as I smaller than or equal to three.
And then the loop breaks.
And now that you know how to use for loops, the world is your oyster.
There are so many apps that you can now build, so strap in and get ready for the workbook bonanza.
It's coming up.