Counting down
A for loop does not have to count upwards. To count down, start high, decrease the counter with i-- and make the condition test the lower limit. All three parts of the header change together:
for (int i = 7; i > 2; i--) {
Console.WriteLine(i);
}
The output is:
7
6
5
4
3
int i = 7starts at the highest value.i > 2keeps the loop going whileiis above 2, so the last value printed is 3. Writingi >= 3gives the same result.i--subtracts 1 after each pass.
Note that the comparison flips. A loop that counts up asks "is i still below the limit?" with < or <=. A loop that counts down asks "is i still above the limit?" with > or >=.
The exercise: 5 down to -5
The task is to print every number from 5 to -5 in decreasing order, with both ends included. It has two steps.
- Write the header. Start at 5 and keep going while
i >= -5. The>=matters: with>the loop would stop as soon as the counter reached -5, and -5 would not be printed. Decrease withi--. - Print the counter with
Console.WriteLine(i).
for (int i = 5; i >= -5; i--) {
Console.WriteLine(i);
}
The output counts 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, eleven lines in total. The loop passes through 0 and into negative numbers without any special handling, because int values can be negative and -5 >= -5 is true. After the pass that prints -5, the counter becomes -6, the condition fails, and the loop ends.
Common mistakes
- Using
i++in a countdown. The counter moves away from the limit, and the loop never ends. The mistake is easy to make when you copy a loop that counts up. - Keeping the old comparison.
i <= -5is false right away for a start of 5, so the loop prints nothing. - Missing the endpoint.
i > -5ends at -4. Use>=when the last value should be printed. - Writing
i-ori =- 1. The decrement isi--, ori -= 1. The formi =- 1assigns -1 toiinstead of subtracting.
Counting down by more than one
The update can subtract any amount. This loop counts down from 10 to 0 in steps of 2:
for (int i = 10; i >= 0; i -= 2) {
Console.Write(i + " ");
}
// 10 8 6 4 2 0
Walking an array backwards
Counting down is how you visit an array from the end. The last position is Length - 1 and the first is 0:
int[] nums = {4, 8, 15};
for (int i = nums.Length - 1; i >= 0; i--) {
Console.Write(nums[i] + " ");
}
// 15 8 4
Be careful if the counter is an unsigned type such as uint. It cannot go below 0: after 0, i-- wraps to the largest value, so i >= 0 is true on every check and the loop never ends. With a plain int counter, this does not happen.
Checking a countdown before you run it
The same three questions as for any loop work here. The first value is 5, the last is -5, and the number of lines is the larger end minus the smaller end plus 1, which is 5 - (-5) + 1 = 11. If your output has ten lines and stops at -4, the condition uses > where >= is needed.
Zero passes and one pass
A countdown that starts below its limit, such as for (int i = 3; i >= 5; i--), fails the condition on the first check and prints nothing. A loop that starts exactly on the limit with >=, such as for (int i = 5; i >= 5; i--), runs exactly once. Both are quick ways to test that you have the comparison right.
Whichever direction a loop runs, the body sees each value exactly once, in the order the header produces them.
Key takeaways
- To count down, start high, test with
>or>=, and update withi--ori -= step. - A countdown flips the comparison, not just the update.
- Negative numbers need no special treatment in an
intloop.