# Thread: single for loop --- two conditions??

1. ## single for loop --- two conditions??

How do you write the following as two nested loops?

Code:
`for (int low = 0, high = n - 1; low < high; low++, high--)` 2. It can't be written as nested loops without totally changing what it does.
It is by nature a single-dimensional loop.
Why do you think you want a double loop? 3. General rule: for is only a clever way to write while loops. With your example:
Code:
```for (int i = 0, j = n - 1; i < j; i++, j--)
doSomething();```
Is the same as:
Code:
```/* block, because i and j are local to the loop */
{
int i = 0, j = n - 1;

while ( i < j )
{
doSomething();

i++, j--;
}
}``` 4. I don't think you want that as two loops, because that would change the behavior in ways you don't expect. However, you could factor the "high" variable out as a function of the "low" variable:

Code:
```
for (int low = 0; low < n / 2; ++low) {
int high = n - 1 - low;

// etc.
}``` Popular pages Recent additions high, high--, high;, int, low 