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.
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.
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:
i
to 3.i <= 10
.i
.i
by 1.i
exceeds 10.Here is the step-by-step breakdown of the algorithm:
i
to 3.i <= 10
. If true, proceed to the next step; otherwise, exit the loop.i
.i
by 1.i <= 10
is false.#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;
}
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.
Potential edge cases include:
In this problem, the range is fixed from 3 to 10, so these edge cases are not applicable.
To test the solution comprehensively, consider the following test cases:
When approaching such problems, consider the following tips:
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.
For further reading and practice, consider the following resources: