For Loops, While Loops, and Arrays

I am not that new to Unity but i do not know when i need to use arrays and the different loops. i can use if statements thoes are very simple but if anyone can tell me the proper time to use an array that will be very helpful. Thanks.

Well, the proper time to use an array is when you have a collection of data of the same type. For example, you want to generate 20 numbers and store them… you wouldn’t want to have 20 individual variables so you use an array (size 20).

C#
`

//[] = array. so int[] means int array.
int[] numbers = new int[20];

`

You then access ‘elements’ within the area using the key value, starting at 0. So a size 20 array will have 0-19 as the references.

So to flesh out the example of random generation (using a for loop):

C#
`

int[] Generate(int count, int min, int max)
{
    int[] numbers = new int[count];

    //Start at 0, and make sure the iterator (i) does not go beyond the array limit 
    for (int i = 0; i < count; i++)
    {
        numbers *= Random.Range(min,max);*

}
return numbers;
}
`
For loops should be used when you know how many times the loop will need to process. It is appropriate here because we know that ‘count’ will be the amount of times it needs to run.
While loops should be used when you have no idea how many times it would need to run. Like when you are making something procedural and you are using a boolean to check to see if it is done or not.