Counting with a while loop
A while loop can produce a sequence of numbers if a variable changes by the same amount on every pass. The variable's starting value decides where the sequence begins, the condition decides where it ends, and the update decides the step. These are the loop's initialization, condition and update.
This program prints the numbers from 0 through 4:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
0
1
2
3
4
The sequence ends at 4 and not at 5, because the condition i < 5 is false once i reaches 5, and the body does not run for that value.
What the computer does
Here is the loop one step at a time:
iis created with the value 0.- Is
0 < 5? Yes. Print 0.i++makesiequal to 1. - Is
1 < 5? Yes. Print 1. Nowiis 2. - Is
2 < 5? Yes. Print 2. Nowiis 3. - Is
3 < 5? Yes. Print 3. Nowiis 4. - Is
4 < 5? Yes. Print 4. Nowiis 5. - Is
5 < 5? No. Exit the loop.
The order inside the body matters. The number is printed first and incremented second, so the value shown is the one that passed the check. If you swapped the two lines, the loop would print 1 to 5 instead.
Choosing the start and the end
Two decisions control which numbers appear:
- The start is the initial value. A loop that begins with
let counter: number = 3;prints 3 first. - The end depends on the comparison operator.
counter < 10stops before 10.counter <= 10includes it.
To include the last number, use <= with that number, or < with the number after it. Both work, and which one reads better depends on the task. When the task says "from 3 to 10", with 10 included, <= 10 matches the wording most directly.
The exercise
The assignment is to print every number from 3 to 10 with a while loop, one number per line. Four steps build it:
- Declare the counter with the start value:
let counter: number = 3;. It isletbecause the loop changes it. - Write the loop with the condition
counter <= 10. It uses<=so that 10 is printed too. - Print the counter in the body with
console.log(counter). Each call ends the line, so every number lands on its own line. - Increase the counter with
counter++. Without it the value stays at 3 and the condition is true forever.
let counter: number = 3;
while (counter <= 10) {
console.log(counter);
counter++;
}
The output is the eight lines 3, 4, 5, 6, 7, 8, 9 and 10. When the loop ends, counter is 11, because the update runs after the last print and the following check fails. If you used the variable after the loop, you would find 11 in it.
Common mistakes
- Stopping one number early. With
counter < 10the output ends at 9. Always check the last value against the task. - Starting at the wrong number. Starting the counter at 0 or 1 prints extra lines before 3. Check the first value too.
- Forgetting the update. The program prints 3 again and again and never finishes.
- Declaring the counter inside the loop. A
letinside the body is created again on every pass, so it would restart at 3 each time. Declare it before the loop. - Printing a string by accident.
console.log("counter")with quotes prints the word, not the number. Without quotes it prints the value.
Key takeaways
- A counting loop has an initialization, a condition and an update.
- The initial value is the first number the loop sees, and the condition decides where it stops.
<=includes the end value and<excludes it.- When the loop ends, the counter holds the first value that failed the condition.