Start learning free

LESSON 120 OF 347 · WHILE LOOPS & FOR LOOPS

Printing Numbers in C# with For Loops: Changing the Iteration

Lesson Read the full explanation

Let's see an example where we change the iteration statement.

We can print all even numbers from 2 through 10:

for (int i = 2; i <= 10; i += 2) {
	Console.WriteLine(i);
}

The output of this code is:

2
4
6
8
10

Let's break down this code:

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

  • The condition statement is i <= 10 so we end at number 10. We could've written i < 11 and it would still be correct.

  • The iteration statement is i += 2, so we increase our number by 2 every time.


Assignment

Let's print all odd numbers from 7 through 23 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    C# · WHILE LOOPS & FOR LOOPS

    Printing Numbers 4 in C#, explained

    The third part of the header

    In for (initialization; condition; iteration), the iteration part runs after every pass and decides how far the counter moves. So far that was i++, a step of 1. Replacing it with i += 2 makes the counter skip every other number.

    Even numbers from 2 to 10

    for (int i = 2; i <= 10; i += 2) {
        Console.WriteLine(i);
    }
    

    The output is:

    2
    4
    6
    8
    10
    
    • The loop starts at 2, an even number.
    • i <= 10 includes 10. Writing i < 11 would give the same result.
    • i += 2 adds 2 after each pass, so the counter stays even.

    The key idea is that adding 2 never changes whether a number is even or odd. The first value therefore decides which kind of number the loop prints.

    The exercise: odd numbers from 7 to 23

    The task is to print the odd numbers 7, 9, 11 and so on up to 23. It has two steps.

    1. Write the header. Start at 7, the first odd number in the range, so the loop prints odd numbers only. Stop with i <= 23, because 23 is odd and should be printed. Step with i += 2.
    2. Print the counter with Console.WriteLine(i).
    for (int i = 7; i <= 23; i += 2) {
        Console.WriteLine(i);
    }
    

    The counter takes the values 7, 9, 11, 13, 15, 17, 19, 21 and 23. After 23 it becomes 25, which fails i <= 23, and the loop ends.

    Other step sizes

    The step can be any number. i += 5 counts in fives, and i += 10 counts in tens. A step larger than 1 means the counter may never equal the upper bound exactly, as when starting at 7 and stepping by 3 towards 23. That is why loops use <= or < in the condition rather than !=: a range check works whether or not the counter lands on the limit.

    Common mistakes

    • Starting on the wrong parity. Starting at 6 with i += 2 prints even numbers, however the condition is written.
    • Using i + 2 instead of i += 2. The expression i + 2 computes a value and throws it away, so the counter never changes and the loop never ends. Use i += 2 or i = i + 2.
    • Using != as the stop condition. i != 24 with odd numbers never matches, and the loop keeps going past 23.
    • Stopping one step early. i < 23 leaves out 23 itself.

    A trace of the exercise loop

    Reading the loop pass by pass shows where the iteration part acts:

    PassValue of iCondition i <= 23Printed
    17true7
    29true9
    923true23
    end25falsenothing

    Another example is for (int i = 0; i <= 12; i += 3), which prints 0, 3, 6, 9 and 12.

    Stepping versus testing

    There is a second way to print odd numbers: loop over every number and test each one.

    for (int i = 7; i <= 23; i++) {
        if (i % 2 != 0) {
            Console.WriteLine(i);
        }
    }
    

    This prints the same output but makes 17 passes instead of 9. Stepping by 2 is shorter and does less work. The test is still useful when the start value is not known in advance. It is written as i % 2 != 0 rather than i % 2 == 1 because the remainder of a negative odd number is negative in C#: -3 % 2 is -1.

    Why the starting value matters

    With a step of 2, the loop only ever sees numbers of the same parity as its start. That is why the exercise starts at 7 and not at 8: the loop cannot move from an even number to an odd one by adding 2. If a task says "odd numbers from 8 to 23", the first odd number is 9, so that is where the loop should start.

    Key takeaways

    • The iteration part sets the step size: i += 2 skips every other number.
    • The starting value decides whether the loop prints odd or even numbers.
    • Use a range condition such as <= rather than != when the step is more than 1.
    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