
Learning C# by Developing Games with Unity
By :

We've accessed individual collection elements through the subscript operator, along with collection type methods, but what do we do when we need to go through the entire collection element by element? In programming, this is called iteration, and C# provides several statement types that let us loop through (or iterate over, if you want to be technical) collection elements. Iteration statements are like methods, in that they store a block of code to be executed; however, unlike methods, they can repeatedly execute their code blocks as long as their conditions are met.
The for
loop is most commonly used when a block of code needs to be executed a certain number of times before the program continues. The statement itself takes in three expressions, each with a specific function to perform before the loop executes. Since for
loops keep track of the current iteration, they are best suited to arrays and lists.
Take a look at the following looping statement...