# Thread: Multivariable For Loop

1. ## Multivariable For Loop

Greetings!
I'm trying to iterate through two arrays, using one for loop, but whenever I try to print both array elements, I get an IndexOutOfBounds error. I've also tried using two different variables to keep track of the two array's indices, but that failed as well. Both of my methods are posted below.
Code:
```for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++){
Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (j+1), high[j]);
}```
And this
Code:
```for (int i = 0; i < average.Length; i++)
Console.Write("  Week {0} -- {1}\t  Week {0} -- {2}\n", (i + 1), average[i], high[i]);```
Also, every time I post code here, I get multiple extra blank lines. How can I get rid of those? Thanks.

2. The second method makes i increased until it is equal to average.Length-1 .This may surpasses borders of array high.

3. Both arrays are of the same size, but array average is of type double and array high is of type int. Would that cause an issue?

4. double or int does not affect in this case.You are interested in the number of cells,not the type of them right now.Make sure that your arrays are allocated properly and no 'garbage' are held in it.

5. Simple, one of the arrays is not as long as the other one.

6. That loop assumes both arrays are the same length yet there is no guarantee from the code that they will be. This is a crash waiting to happen. Incidentally since i and j must be within the same exact range you can ditch j and just use i. If that does not completely iterate over both arrays then you are using the wrong construct to analyze their contents. At the very least you should check to ensure both arrays are the same length and throw an exception if they are not.

7. > for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++)
So is the comma operator the same as boolean && in C and C++?

Perhaps
for(int i = 0, j = 0; i < average.Length && j < high.Length; i++, j++)

8. Originally Posted by DanFraser
Simple, one of the arrays is not as long as the other one.
Originally Posted by VirtualAce
That loop assumes both arrays are the same length yet there is no guarantee from the code that they will be.
Notice that Mcdom34 says the arrays are of same size.

9. > Notice that Mcdom34 says the arrays are of same size.
Saying it and proving it are two different things.

If they were the same, where would IndexOutOfBounds come from?

10. Originally Posted by Salem
> Notice that Mcdom34 says the arrays are of same size.
Saying it and proving it are two different things.
Correct

If they were the same, where would IndexOutOfBounds come from?[/QUOTE] If i knew,i would have answered already

11. Your compiler is telling you they are not the same length. Also before you go correcting me make sure you understand what I am saying. Although the code will work if the arrays are the same size there is no check to ensure they are. Quite simply this is bad code and has the potential to cause a crash.

12. Assuming that average.Length and high.Length are equal (the code will fail if they are not), I fail to see how
Code:
```for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++)
{
Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (j+1), high[j]);
}```
differs from
Code:
```for(int i = 0; i < average.Length; i++)
{
Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (i+1), high[i]);
}```

13. Originally Posted by Mcdom34
Greetings!
I'm trying to iterate through two arrays, using one for loop, but whenever I try to print both array elements, I get an IndexOutOfBounds error. I've also tried using two different variables to keep track of the two array's indices, but that failed as well. Both of my methods are posted below.
Code:
```for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++){
Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (j+1), high[j]);
}```
And this
Code:
```for (int i = 0; i < average.Length; i++)
Console.Write("  Week {0} -- {1}\t  Week {0} -- {2}\n", (i + 1), average[i], high[i]);```
Also, every time I post code here, I get multiple extra blank lines. How can I get rid of those? Thanks.
The first is not even a legal C# expression. The standard states the conditional part of a for loop must be a boolean expression - while the first and third part are comma-delimited expression lists. I don't know what you're using to run this program but visual C# won't even compile that first piece.

Anyhow, you have the exception - just catch it and check the values of i, j, and the .Length values, and you should see quickly where you're stepping beyond the boundary.

Popular pages Recent additions