I have a for loop in C# as below,
int[] dim1, dim2, dim3;
//Initialize dim1, dim2 and dim3
for(int i = 0; i < dim1.Length; i++)
{
DoSomething1(i);
for(int j = 0; j < dim2.Length; j++)
{
DoSomething2(i, j);
for(int k = 0; k < dim3.Length; k++)
{
DoSomething3(i, j, k);
}
}
}
My problem is that sometimes the dimension of array dim2 would be 0, then DoSomething2() and DoSomething3() will not be executed. Is there any way to bypass the second for loop if dim2.Length = 0? That is, when dim2.Length = 0, I want the whole for loops to behave like below,
int[] dim1, dim2, dim3;
//Initialize dim1, dim2 and dim3
for(int i = 0; i < dim1.Length; i++)
{
DoSomething1(i);
for(int k = 0; k < dim3.Length; k++)
{
DoSomething3(i, k);
}
}
Moreover, the for loops might be many dimensions (I will have dim1, dim2, dim3, dim4, ..., dimn), and except for the length of dim1, the length of any other array might be 0. Is there any good way to do this? Thanks!
DoSomething3
n number of times. Where n =dim1.Length * dim2.Length * dim3.Length
.