Is there a way to have diffrent statement for each loop in a while or for loop? Example: I have an int array, lenght n, and I want to add diffrent specific values (same type of course) at each index ...
so that when index is equal n-1 it stops. I know how to do it if their are a few statements but what if there are 100 specific value to be added.
More specific:
public static int[] ArrayPlay(int n)
{
int[] arr = new int[n];
while (arr[x] < n)
arr[0] = 3;
arr[1] = 12;
arr[2] = 176;
arr[3] = 4;
//so on...
return arr;
}
would look like somthing like this:
public static int[] ArrayPlay(int n)//4
{
int a = 0;
int[] arr = new int[n];
while ( a <= n) //lets say n is 4
arr[a] = 3; //arr[0]
a++;
arr[a] = 12; //arr[1]
a++;
arr[a] = 4; //arr[2]
a++;
arr[a] = 8; //arr[3]
a++; //(a=4 Stop! and return arr)
arr[a] = 7;
a++;
arr[a] = 12;
a++;
arr[a] = 22;
a++;
arr[a] = 18;
return arr;
}
it never passes to a++ because the first statement is Always true.
Is there a way to break, check condition and continue between each statement? and what property or method to check an arrays index nr (where I wrote arr[x]) x being the value to be compared with n.
public static int[] ArrayPlay2(int n)//4
{
int[] arr = new int[n];
for (int a = 0; a < arr.Length; a++)
{
arr[a] = 3; //arr[0]
arr[a] = 12; //arr[1]
arr[a] = 4; //arr[2]
arr[a] = 8; //arr[3] (a=4 Stop! and return arr)
arr[a] = 7;
arr[a] = 12;
arr[a] = 22;
arr[a] = 18;
}
return arr;
}
Why dont the code above work??