Start learning free

LESSON 121 OF 347 · WHILE LOOPS & FOR LOOPS

Looping in Reverse in C#: Counting Down with For

Lesson Read the full explanation

A for loop can also count backwards, as long as we define the right conditions. For example:

for (int i = 7; i > 2; i--) {
	Console.WriteLine(i);
}

The output of this code is:

7
6
5
4
3

Let's break down this code:

  • The initialization statement is int i = 7, so we start iterating from number 7.

  • We loop as long as i > 2, so we'll end at number 3. We could've written i >= 3 and it would still be correct.

  • The iteration statement is i--, so we decrease our number by 1 every time.


Assignment

Let's print all numbers from 5 through -5 in decreasing order.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    C# · WHILE LOOPS & FOR LOOPS

    Looping In Reverse in C#, explained

    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 = 7 starts at the highest value.
    • i > 2 keeps the loop going while i is above 2, so the last value printed is 3. Writing i >= 3 gives 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.

    1. 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 with i--.
    2. 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 <= -5 is false right away for a start of 5, so the loop prints nothing.
    • Missing the endpoint. i > -5 ends at -4. Use >= when the last value should be printed.
    • Writing i- or i =- 1. The decrement is i--, or i -= 1. The form i =- 1 assigns -1 to i instead 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 with i-- or i -= step.
    • A countdown flips the comparison, not just the update.
    • Negative numbers need no special treatment in an int loop.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free