Start learning free

LESSON 121 OF 415 · WHILE LOOPS & FOR LOOPS

Printing Numbers with a While Loop in Swift

Lesson Read the full explanation

We can achieve many different outcomes with while loops.

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

var i = 0
while i < 5 {
	print(i)
	i += 1
}

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. print(i) => Output: 0
	c. i += 1 => i = 1

1. Second iteration:
	a. Is i < 5 true? <=> Is 1 < 5 true? Yes.
	b. print(i) => Output: 1
	c. i += 1 => i = 2

2. Third iteration:
	a. Is i < 5 true? <=> Is 2 < 5 true? Yes.
	b. print(i) => Output: 2
	c. i += 1 => i = 3

3. Fourth iteration:
	a. Is i < 5 true? <=> Is 3 < 5 true? Yes.
	b. print(i) => Output: 3
	c. i += 1 => i = 4

4. Fifth iteration:
	a. Is i < 5 true? <=> Is 4 < 5 true? Yes.
	b. print(i) => Output: 4
	c. i += 1 => 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

    SWIFT · WHILE LOOPS & FOR LOOPS

    Printing Numbers in Swift, explained

    Counting with a while loop

    A while loop can print a run of numbers. The loop variable holds the current number, the body prints it, and an update moves to the next one. Here are the numbers 0 through 4:

    var i = 0
    while i < 5 {
        print(i)
        i += 1
    }
    
    0
    1
    2
    3
    4
    

    What the computer does

    The variable i starts at 0. Before each pass the condition i < 5 is checked.

    • With i equal to 0, 1, 2, 3 and 4 the condition is true. The body prints the value and adds 1.
    • After the fifth pass i is 5. The condition 5 < 5 is false, so the loop stops without printing 5.

    Two choices shape the output: the starting value and the condition. With < the last number is excluded. With <= it is included. Choosing between them is the main thing to get right.

    The exercise

    The assignment is to print all the numbers from 3 to 10, both included.

    1. Start at the first number. Declare var counter = 3. It must be a var, because the loop changes it.
    2. Choose the condition. Ten must be printed, so use counter <= 10. With < the output would stop at 9. Swift takes no parentheses around the condition, but the braces are required.
    3. Print the counter in the body with print(counter). Each call ends with a newline, so every number is on its own line.
    4. Update the counter with counter += 1. Without it, counter stays 3, the condition is always true and the loop never ends.
    var counter = 3
    while counter <= 10 {
        print(counter)
        counter += 1
    }
    

    The output is the eight numbers from 3 to 10, one per line. After the loop, counter is 11, the first value that failed the condition.

    Reading the loop as a table

    When a loop misbehaves, write down the variable's value at the start of each pass. For the loop above, counter is 3 on the first pass and 10 on the last, so there are eight passes, one per number from 3 to 10. After the update on the eighth pass it is 11, and the condition 11 <= 10 is false. The number of passes is the last value minus the first plus one, which is why a range that includes both ends has one more element than their difference.

    Swift details

    • No ++. Swift removed the increment operator. Use counter += 1.
    • The type is inferred. var counter = 3 makes counter an Int, so you don't write the type.
    • A different tool. For a fixed range, for counter in 3...10 does the same without a manual update. The while form is useful when you need to control the update yourself.

    Common mistakes

    • Off by one at the end. counter < 10 misses the 10. Count the numbers you expect: 3 to 10 is eight lines.
    • Wrong start. Starting at 0 or 1 prints extra numbers.
    • Update before print. If counter += 1 comes first, the first line is 4 and the last is 11.
    • Using let. A constant can't be increased.

    Key takeaways

    • The initial value sets the first number printed.
    • The condition sets the last. Use <= to include it.
    • The update must move the variable toward ending the loop.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free