Arrays and loops are like siblings previously defined an array that stores four kingdoms and you learn
how to access each element using its index.
Now, let's imagine the array had 20 elements, are you going to put 20 print statements and access
each index one by one?
Absolutely not.
That would be terrible.
Instead, we can use a loop that runs through every element in the array.
In this video, you're going to learn how to loop through an array of values.
The first thing you need to do is create a new class by yourself inside Section six.
Wow, that's a tongue twister inside your Section six folder, create a new file, need to raise an
Leapster Java and make sure the class has a main method.
So how can we loop through an array, a loop can run through every element in an array, but how does
that happen?
Let's have a look.
Inside arrays and loops make another kingdom's variable, it's going to be the exact same variable as
the last one, string kingdom's is equal to an array of string values.
W6.
Northumbria and East Anglia.
Yay for short.
The array has four elements, so you need a for loop that runs exactly four times, so I'm going to
say four enties is equal to zero.
The eye counter is going to start at zero because the index of a race first element starts at zero as
well.
And so it makes sense because the last index of an array race three.
The loop is going to keep running while the counter eye is less than the array's length for E-Plus plus.
And then I'm going to print the counter I during each run.
All right, this loop keeps running as long as it is less than four, I starts at zero and goes up until
three, basically designed this loop so that each counter value matches the index of an element so I
can use each counter value to index an element inside the kingdom array.
Some print the element at Inducts I.
And here will index the element from the kingdom's array.
And very nice, the for loop runs through every value in the array.
And here is a visual that I made for you.
I start at zero indexing the first element.
Increases up to one.
Indexing the second element.
And the loop keeps running until I equals three, which allows us to index the last elements.
Arrays of a length property that calculates the number of elements.
You can use the length to calculate the number of elements for kingdoms, right?
So here I'm going to print.
The number of elements is.
Kingdoms that length.
And indeed, the number of elements is four, because the array has four elements, so length equals
four.
And so inside the for loop, instead of writing is smaller than four, I'm going to write I is smaller
than kingdoms that length.
And nice, this works very well with that being said, you never need to count the elements yourself,
always use the length property to run through an array.
Now, here's something to consider when you look through an array.
Pay attention to the condition, because what if you replaced your condition with eyes smaller than
or equal to kingdoms that length?
In this case, the code crashes because the next four is out of range.
Because we start counting at zero, the last index number in an array is always going to be one smaller
than the array's length.
Into this loop runs as long as I is smaller than or equal to four.
In the fifth run, the counter is going to equal the array's length, for there is no element at index
four, so the code crashes.
So your loop should only run as long as it is smaller than the array length.
And now everything works, the loop keeps running right up until the last inducts and then the loop
stops.
In this lesson, we used a for loop to access each element in an array, your first for a loop ran through
the length of the kingdom's array and also because we start counting at zero, the last index number
is always one smaller than the array length.
So your condition for running through an array should always be smaller than the length of the array.
If I count up to an index that's equal to the length, your code is going to crash.