Start learning free

LESSON 116 OF 347 · WHILE LOOPS & FOR LOOPS

while Loop in C#: Stepping by 2 and Counting Backwards

Lesson Read the full explanation

Practice makes perfect! Let's check two more examples of printing numbers using the while loop.


Example 1:

Let's print all the even numbers from 4 through 14:

int i = 4;
while(i <= 14) {
	Console.WriteLine(i);
	i += 2;
}

The output of this program is:

4
6
8
10
12
14

Example 2:

We can also loop in reverse. Let's print all even numbers from 12 to 4 in decreasing order:

int i = 12;
while(i >= 4) {
	Console.WriteLine(i);
	i -= 2;
}

The output of this program is:

12
10
8
6
4

Assignment
Let's print all the odd numbers from 23 to 7 in decreasing order!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check

    C# · WHILE LOOPS & FOR LOOPS

    Printing Numbers 2 in C#, explained

    Changing the step

    The update statement doesn't have to add 1. Any change to the counter works, as long as it moves toward the point where the condition becomes false. This lesson shows two variations: a bigger step, and counting down.

    Even numbers from 4 through 14

    int i = 4;
    while (i <= 14) {
        Console.WriteLine(i);
        i += 2;
    }
    
    4
    6
    8
    10
    12
    14
    

    The counter starts at the first even number, and i += 2 jumps to the next one. Because the start is even and the step is 2, every value stays even. The loop prints 14 and then moves to 16, which makes i <= 14 false.

    Even numbers from 12 down to 4

    To count down, start at the larger number, subtract in the update, and flip the comparison so that it checks the lower limit:

    int i = 12;
    while (i >= 4) {
        Console.WriteLine(i);
        i -= 2;
    }
    
    12
    10
    8
    6
    4
    

    i -= 2 is shorthand for i = i - 2. In the same way, i += 2 is shorthand for i = i + 2.

    A recipe for any range

    Three questions give you the whole loop:

    1. Where does it start? This is the initial value of the counter.
    2. Which direction, and where does it end? Counting up uses <= with the last value. Counting down uses >= with the last value.
    3. How big is each step? This is the update, such as += 2 or -= 2.

    Using the wrong direction is a common mistake. If you count down but the condition says i <= 4 with a start of 12, the condition is false on the first check and nothing prints. If you count up with a condition that is always true, the loop never ends.

    When the step doesn't land on the limit

    The limit in the condition is a boundary, not a target. If the step skips over it, the loop simply stops at the last value that passes the check. Starting at 4 with j += 3 and the condition j <= 14 prints these numbers:

    4 7 10 13
    

    The next value would be 16, and 14 never appears. This is fine, and often what you want, but it's a reminder to work out the first and last values by hand before running the loop.

    The exercise, step by step

    The assignment is to print the odd numbers from 23 down to 7, in decreasing order.

    1. Start at 23. int counter = 23; is the largest number to print, and it's odd.
    2. Count down to 7. Use while (counter >= 7). The comparison is >= because the counter decreases and 7 must be included.
    3. Print the counter. Console.WriteLine(counter); prints each value on its own line.
    4. Subtract 2. counter -= 2; moves from one odd number to the previous one. Subtracting 2 from an odd number always gives an odd number.
    int counter = 23;
    while (counter >= 7) {
        Console.WriteLine(counter);
        counter -= 2;
    }
    

    The output is 23, 21, 19, 17, 15, 13, 11, 9, 7, which is nine lines. After 7 is printed, counter becomes 5 and the condition 5 >= 7 is false, so the loop ends.

    Common mistakes

    • Starting on an even number. Starting at 22 or 24 gives even values for the whole loop. The first value decides the parity when the step is 2.
    • Using += when counting down. The counter would grow away from the limit, so counter >= 7 is always true and the loop never ends.
    • Using the wrong comparison. counter <= 7 is false from the start, because 23 is not at most 7. Nothing is printed.
    • Stopping too early. counter > 7 leaves out 7.

    Key takeaways

    • The update can be any step: += 2 counts up by twos, and -= 2 counts down by twos.
    • Counting up uses < or <=. Counting down uses > or >=.
    • The starting value and the step decide whether you get odd or even numbers.
    • The update must always move the counter toward the exit condition.
    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