The for loop

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

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 ...

Get Unity 5: Learning C# by Developing Games now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.