Start learning free

LESSON 119 OF 347 · WHILE LOOPS & FOR LOOPS

Printing Numbers in C# with For Loops: Initialization and Condition

Lesson Read the full explanation

We can achieve many different outcomes with for loops.

It's all about tuning the initialization, condition and iteration statements.

For example, we can print all numbers from 2 through 6:

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

The output of this code is:

2
3
4
5
6

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 < 7. We could've written i <= 6 and it would still be correct.

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


Assignment

Let's print all numbers from 3 through 10 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 3 in C#, explained

    Tuning the header

    A for loop is controlled by three parts: initialization, condition and iteration. Changing one of them changes which numbers the loop visits. This lesson changes the first two, to print a range that does not start at 0.

    A range that starts at 2

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

    The output is:

    2
    3
    4
    5
    6
    
    • int i = 2 makes the first value printed 2.
    • i < 7 keeps the loop going while i is 6 or less, so 6 is the last value printed. 7 fails the test and is never printed.
    • i++ adds 1 after each pass.

    Two ways to write the same upper bound

    For whole numbers, i < 7 and i <= 6 are equivalent: both stop after 6. Pick the one that matches how you think about the task:

    • Use <= when the last value is named in the task ("through 10"), so the number in the code is the number in the task.
    • Use < when you count items from 0, such as i < n for the first n positions.

    A quick way to know how many passes a loop makes: for a step of 1, it is the last value minus the first value plus 1. From 2 through 6, that is 6 - 2 + 1 = 5 passes.

    The exercise: 3 through 10

    The task is to print every number from 3 to 10, including both ends. It takes two steps.

    1. Write the header. Declare int i = 3 so that the first value is 3. Use i <= 10 so that 10 is included: with i < 10 the loop would stop at 9. Finish with i++.
    2. Print the counter. Call Console.WriteLine(i) in the body. WriteLine adds a line break after the value, so each number gets its own line.
    for (int i = 3; i <= 10; i++) {
        Console.WriteLine(i);
    }
    

    This prints 3, 4, 5, 6, 7, 8, 9 and 10, eight lines in total, which matches 10 - 3 + 1.

    Common mistakes

    • Dropping the endpoint. Writing i < 10 when the task says "through 10" misses the last number.
    • Printing a string instead of the variable. Console.WriteLine("i") prints the letter i every time. Without quotes, it prints the value.
    • Using Console.Write by accident. It prints without a line break, so the numbers run together on one line as 345678910.
    • Starting from the wrong value. Initialization sets the first number printed. Check it against the task before anything else.

    Other starting points

    The initialization part can hold any int, including negative numbers and values from variables:

    for (int i = -2; i <= 2; i++) {
        Console.Write(i + " ");
    }
    // -2 -1 0 1 2
    
    int start = 3, end = 10;
    for (int i = start; i <= end; i++) {
        Console.Write(i + " ");
    }
    // 3 4 5 6 7 8 9 10
    

    Console.Write keeps the output on one line, which is handy for checking a range quickly. If start were larger than end, the condition would be false on the first check and the body would never run.

    Checking a loop before you run it

    Three quick questions catch most range mistakes:

    1. What is the first value? It is whatever the initialization sets.
    2. What is the last value? It is the largest number that still passes the condition.
    3. How many lines should appear? For a step of 1, it is last minus first plus 1.

    For the exercise, the answers are 3, 10 and 8. If your output has seven lines or ends at 9, the condition is off by one.

    When the loop never runs

    If the condition is false on the very first check, the body is skipped completely and the program moves on without an error. For example, for (int i = 10; i <= 3; i++) prints nothing. When a loop produces no output at all, compare the starting value with the limit first.

    Key takeaways

    • Initialization sets the first value; the condition decides where the loop stops.
    • For integers, i < 7 and i <= 6 stop at the same place.
    • Choose <= when a task says "through" a number.
    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