#164 – for Loop Clauses Can Contain Lists of Statements
November 28, 2010 2 Comments
Both the initialization clause and the iteration clause of a for loop can consist of multiple statements, separated by commas.
For example, you could initialize two different variables in the initialization clause and then increment one variable and decrement the other in the iteration clause.
// Set reverse diagonal of matrix for (int i = 0, j = 9; i < 10; i++, j--) { nums[i, j] = 1; }
Do you know if there’s a way to handle different types or initializers? Like having an int and a string for instance.
You can only declare one loop variable. But you could declare and initialize other variables outside of the loop.