Start learning free

LESSON 115 OF 347 · WHILE LOOPS & FOR LOOPS

Printing Numbers with a while Loop in C#: Initialization and Condition

Lesson Read the full explanation

We can achieve many different outcomes with while loops.

For example, we can print print all the numbers from 0 through 4:

int i = 0;
while(i < 5) {
	Console.WriteLine(i);
	i++;
}

The output of this program is:

0
1
2
3
4

And this is what the computer does behind the scenes during this loop:

0. Creates and initializes a variable i = 0

1. First iteration:
	a. Is i < 5 true? <=> Is 0 < 5 true? Yes. 
	b. Console.WriteLine(i); => Output: 0
	c. i++; => i = 1
	
1. Second iteration:
	a. Is i < 5 true? <=> Is 1 < 5 true? Yes. 
	b. Console.WriteLine(i); => Output: 1
	c. i++; => i = 2
	
2. Third iteration:
	a. Is i < 5 true? <=> Is 2 < 5 true? Yes. 
	b. Console.WriteLine(i); => Output: 2
	c. i++; => i = 3
	
3. Forth iteration:
	a. Is i < 5 true? <=> Is 3 < 5 true? Yes. 
	b. Console.WriteLine(i); => Output: 3
	c. i++; => i = 4
	
4. Fifth iteration:
	a. Is i < 5 true? <=> Is 4 < 5 true? Yes. 
	b. Console.WriteLine(i); => Output: 4
	c. i++; => i = 5

5. Sixth iteration:
	a. Is i < 5 true? <=> Is 5 < 5 true? No.
	b. Exit the loop.

Assignment
Let's print all numbers from 3 to 10 using a while loop.


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 in C#, explained

    Counting with a while loop

    A while loop can walk through a range of numbers. Three choices define the range: the starting value (initialization), the condition that decides whether another pass happens, and the step that changes the counter on each pass.

    This example prints the numbers 0 through 4:

    int i = 0;
    while (i < 5) {
        Console.WriteLine(i);
        i++;
    }
    
    0
    1
    2
    3
    4
    

    The variable starts at 0, and each pass prints it and adds 1. When i reaches 5, the condition i < 5 is false, so the loop stops before printing 5. Console.WriteLine accepts an int directly. You don't have to convert it to a string first.

    Choosing the start and the condition

    The starting value is the first number printed, and the condition decides the last. The difference between < and <= matters:

    • i < 5 stops before 5, so the last number printed is 4.
    • i <= 5 stops after 5, so the last number printed is 5.

    To include a number in the output, use <= with that number, or use < with the next one. Mixing these up is the most common off-by-one error with loops.

    Walking through the first example

    CheckiIs i < 5?Output
    10yes0
    21yes1
    32yes2
    43yes3
    54yes4
    65noloop ends

    The condition is evaluated six times, but the body runs five times. The last check is the one that fails.

    Using the counter for more than printing

    The counter is an ordinary variable, so the body can use it for calculations. This loop adds up the numbers from 1 to 5:

    int i = 1;
    int sum = 0;
    while (i <= 5) {
        sum += i;
        i++;
    }
    Console.WriteLine(sum);   // 15
    

    The variable sum is declared before the loop so that it survives from one pass to the next, and i supplies the values 1 to 5. The same start, condition and step pattern works for any task that has to visit a range of numbers.

    The exercise, step by step

    The assignment is to print every number from 3 to 10, each on its own line.

    1. Start at 3. Declare int counter = 3;. The first number printed is the starting value.
    2. Keep going through 10. Use while (counter <= 10). With <=, 10 is printed too. With < the output would stop at 9.
    3. Print the counter. Console.WriteLine(counter); prints the value and ends the line, so each number sits on its own line.
    4. Add 1 each pass. counter++; moves to the next number. Without it, the loop would print 3 forever.
    int counter = 3;
    while (counter <= 10) {
        Console.WriteLine(counter);
        counter++;
    }
    

    The output is the eight numbers 3, 4, 5, 6, 7, 8, 9 and 10, one per line. The number of passes is the last value minus the first, plus one: 10 - 3 + 1 = 8.

    Common mistakes

    • Stopping one number early. counter < 10 leaves out 10.
    • Printing before or after the update by accident. If you put counter++ above the WriteLine, the first number printed is 4, not 3, and the last is 11.
    • Forgetting the update. The loop prints 3 forever.
    • Declaring the counter inside the loop. A variable declared in the body is not visible to the loop condition, so the code doesn't compile. Declare the counter before the loop.

    The pass count can also help you check your own work. Before running a loop, work out by hand how many lines it should print. If the output has a different number of lines, one of the three choices (start, condition or step) is off by one.

    Key takeaways

    • The starting value is the first number printed.
    • The condition decides the last number: <= includes the limit, < excludes it.
    • The update inside the body moves the counter forward and eventually makes the condition false.
    • The number of passes for a step of 1 is the last value minus the first, plus one.
    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