You have learned about foreach
loops. When iterating through a foreach
loop, we can use a local variable directly to access the data we need. In a for
loop, we also create a variable. However it is an integer variable for controlling the execution of the loop and accessing the data inside the collection by index.
There are three fundamental parts of the for
loop. It will look a bit scary to you at the beginning, but try not to run away.
The for
loop's syntax might look overcomplicated, but trust me, it isn't! Let's go through all of its elements one by one.
The for
loop begins with the for
keyword, followed by brackets. Inside the brackets we must have three fundamental elements separated by semicolons:
i
of the int
type and assigned it a value of 0
.i
variable is less than 10
.i++
in this case, simply adds a value of 1
to the mentioned variable every time the loop completes an execution.In simple words, to use a for
loop, we need an integer variable to control it. The initializer is the declaration and assignment of that variable. In most cases, you will never have to change it at all and it will always look like this:
int i = 0;
The next step is to describe under what conditions the loop will be running. It is the same type of condition that you might write in any if
statement. If i
is less than 10
, this statement is true. If this statement is true, our loop will execute whatever code is inside the code block:
i < 10;
The last part of the for
loop's syntax is simply the addition of 1
to the previous value stored in the i
variable. The i++
might look rather scary, but it's simply a more elegant version of this statement: i = i+1;
.
I have introduced lots of technical words here. Try not to panic. Remember that we want to take baby steps, as we need to make sure you fully understand for
loops. I will show it in an example. Let's now write some proper code using a for
loop.
Yet again, we are using the previous familyMember
example. Create a new C# script and write the following code:
We have analyzed most of this code before. Let's focus on lines 20 to 24. What we are trying to do in this example is iterate through all the elements inside the familyMembers
list and print their values to the Unity Console. You will probably ask, "Why do I even have to learn this? I could have used a foreach
loop." Correct! A foreach
loop is a definitely good approach to this task. However, there will be cases when you need to know at what index position in the array a certain element is stored. That is when you will use a for
loop instead of foreach
. Trust me, it's worth it!
Line 20 constructs our for
loop. As I promised before, in most cases there is no need to edit the initializer at all.
Take a look at this condition:
i < familyMembers.Count;
Previously, we used a value of 10
directly in the condition. In the current example, we want to iterate through all the elements in the array. We don't always know the size of our array, so the safest option is to access its size directly. If you are still confused, please go back to the previous chapter where we spoke about checking out the size of an array and List<T>.
Before you test the code in Unity, try to imagine what actually happens when you click on the Play button.
Unity executes the Start()
function. Lines 14 to 17 add four string values to the familyMembers
list.
Unity starts executing the loop from line 20. It creates the i
variable with a value of 0
. Then it checks the condition. As 0
is smaller than the size of the list, the code block is executed with a value of 0
for i
.
Line 23 accesses the value in the first place in the familyMembers
list as the value of i
is 0
. Unity prints the "Greg"
string.
Unity hits the end of the loop block and goes back to line 20 to check whether the loop should run through again.
But first, i++
increments the value of i
by 1
. So 0 + 1 = 1
. The i
variable is now equal to 1
. Unity checks the condition again and runs through the loop block again.
These steps will keep occurring until the i < familyMembers.Count;
condition becomes false
. This condition will be false
simply when i
becomes equal to 4
. Then, Unity won't execute the code block anymore but will proceed with the normal execution order outside the loop.
Go ahead now. Press Play in Unity. You should see four names being printed to the console one by one.