While Loop: Printing Numbers in Python (Time Complexity: O(n))


We can achieve many different outcomes with while loops.

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

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.


Understanding the Problem

The core challenge here is to use a while loop to print a sequence of numbers from 3 to 10. This problem is fundamental in understanding how loops work in Python, which is crucial for controlling the flow of a program.

Common applications of while loops include iterating over data structures, performing repetitive tasks, and implementing algorithms that require repeated execution until a condition is met.

Potential pitfalls include creating infinite loops if the loop condition is never met or not updating the loop variable correctly.

Approach

To solve this problem, we need to initialize a variable to 3 and use a while loop to print the variable's value until it reaches 10. We will increment the variable by 1 in each iteration.

Let's start with a naive solution:

i = 3
while i <= 10:
    print(i)
    i += 1

This solution is straightforward and works efficiently for this problem. The loop runs from 3 to 10, inclusive, printing each number.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize a variable i to 3.
  2. Start a while loop that continues as long as i is less than or equal to 10.
  3. Inside the loop, print the value of i.
  4. Increment i by 1.
  5. Once i exceeds 10, exit the loop.

Code Implementation

i = 3
while i <= 10:
    # Print the current value of i
    print(i)
    # Increment i by 1
    i += 1

In this code:

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of iterations. In this case, n is 8 (from 3 to 10 inclusive).

The space complexity is O(1) since we are using a constant amount of extra space.

Edge Cases

Potential edge cases include:

For example, if we start at 10 and end at 3, the loop will not run:

i = 10
while i <= 3:
    print(i)
    i += 1

This will produce no output.

Testing

To test the solution comprehensively, we can use a variety of test cases:

We can use Python's built-in unittest framework to automate these tests.

Thinking and Problem-Solving Tips

When approaching such problems:

Practice by solving similar problems and studying different algorithms to improve your problem-solving skills.

Conclusion

In this blog post, we discussed how to use a while loop to print numbers from 3 to 10 in Python. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for mastering control flow in programming.

Keep practicing and exploring further to enhance your coding skills!

Additional Resources