How to loop in Unity C#

For Loop

The for loop is used when you know exactly how many times you want to iterate through a piece of code. It has the following syntax:

for (int i = 0; i < numberOfIterations; i++)
{
    // Code to be executed in each iteration
}
C#

Example:

for (int i = 0; i < 10; i++)
{
    Debug.Log("Iteration " + i);
}
C#

While Loop

The while loop is used when you want to iterate through a piece of code until a certain condition is met. It has the following syntax:

while (condition)
{
    // Code to be executed in each iteration
}
C#

Example:

int i = 0;
while (i < 10)
{
    Debug.Log("Iteration " + i);
    i++;
}
C#

Do-While Loop

Do-while is similar to the while loop, but it executes the code at least once before checking the condition. It has the following syntax:

do
{
    // Code to be executed in each iteration
}
while (condition);
C#

Example:

int i = 0;
do
{
    Debug.Log("Iteration " + i);
    i++;
}
while (i < 10);
C#

ForEach Loop

The foreach loop is used to iterate through the elements of a collection, such as an array or a list. It has the following syntax:

foreach (var element in collection)
{
    // Code to be executed for each element
}
C#

Example:

string[] fruits = { "apple", "banana", "orange" };
foreach (string fruit in fruits)
{
    Debug.Log(fruit);
}
C#

Nested Loops

Nested loops are used when you need to iterate through multiple collections or when you need to perform a repetitive action for each element in a collection. It has the following syntax:

for (int i = 0; i < numberOfIterations; i++)
{
    for (int j = 0; j < numberOfIterations2; j++)
    {
        // Code to be executed for each element
    }
}
C#

Example:

int[,] matrix = { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Debug.Log("Element at [" + i + ", " + j + "] = " + matrix[i, j]);
    }
}
C#

Lambda Loops

Here’s an example of how to use a lambda expression with the ForEach method to iterate through a list of objects and perform an action on each object:

// Create a list of integers
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Use a lambda expression with the ForEach method to print each number
numbers.ForEach(number => Debug.Log(number));
C#

In this example, we use the ForEach method provided by the List<T> class to iterate through the list of integers and print each number using a lambda expression. The lambda expression number => Debug.Log(number) is used to define the action to be performed on each number. The variable number represents the current number being processed, and the Debug.Log method is used to print it to the console. Note that the ForEach method is a convenience method provided by the List<T> class that allows you to perform an action on each element of the list without the need for a separate loop. It takes a lambda expression as a parameter that defines the action to be performed on each element.

Leave a Reply