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


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) {
	cout << i << endl;
	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. cout << i << endl; => Output: 0
	c. i++; => i = 1
	
1. Second iteration:
	a. Is i < 5 true? <=> Is 1 < 5 true? Yes. 
	b. cout << i << endl; => Output: 1
	c. i++; => i = 2
	
2. Third iteration:
	a. Is i < 5 true? <=> Is 2 < 5 true? Yes. 
	b. cout << i << endl; => Output: 2
	c. i++; => i = 3
	
3. Forth iteration:
	a. Is i < 5 true? <=> Is 3 < 5 true? Yes. 
	b. cout << i << endl; => Output: 3
	c. i++; => i = 4
	
4. Fifth iteration:
	a. Is i < 5 true? <=> Is 4 < 5 true? Yes. 
	b. cout << i << endl; => 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.


Understanding the Problem

The core challenge of this problem is to use a while loop to print numbers from 3 to 10. This is a fundamental exercise in understanding how loops work in C++ and how to control the flow of a program using conditions.

While loops are commonly used in scenarios where the number of iterations is not known beforehand and the loop should continue until a certain condition is met.

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. The loop should increment the variable in each iteration.

Here is a step-by-step approach:

  1. Initialize a variable i to 3.
  2. Set up a while loop with the condition i <= 10.
  3. Inside the loop, print the value of i.
  4. Increment i by 1.
  5. The loop will terminate when i exceeds 10.

Algorithm

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

  1. Initialize i to 3.
  2. Check if i <= 10. If true, proceed to the next step; otherwise, exit the loop.
  3. Print the value of i.
  4. Increment i by 1.
  5. Repeat steps 2-4 until the condition i <= 10 is false.

Code Implementation

#include <iostream>
using namespace std;

int main() {
    // Initialize the variable i to 3
    int i = 3;
    
    // While loop to print numbers from 3 to 10
    while (i <= 10) {
        // Print the current value of i
        cout << i << endl;
        
        // Increment i by 1
        i++;
    }
    
    return 0;
}

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) as we are using a constant amount of extra space.

Edge Cases

Potential edge cases include:

In this problem, the range is fixed from 3 to 10, so these edge cases are not applicable.

Testing

To test the solution comprehensively, consider the following test cases:

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to use a while loop in C++ to print numbers from 3 to 10. 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.

Practice is key to improving problem-solving skills. Try solving similar problems and explore different types of loops and conditions.

Additional Resources

For further reading and practice, consider the following resources: