What this lesson covers
A while loop repeats its block as long as its condition is true. Counting is the classic use: keep a number in a variable, print it, and change it by a fixed amount on every pass. This lesson shows two variations on that pattern. You can step by 2 instead of 1, and you can count down instead of up.
The exercise: print all the odd numbers from 23 down to 7, in decreasing order.
The counting pattern
Every counting loop has three ingredients, and they are easy to see in this example that prints the even numbers from 4 to 14:
let i = 4; // 1. a starting value
while (i <= 14) { // 2. a condition that says when to keep going
console.log(i);
i += 2; // 3. an update that moves toward the end
}
It prints 4, 6, 8, 10, 12 and 14, each on its own line. The loop checks the condition before every pass, including the first. After the pass that prints 14, i becomes 16, the condition 16 <= 14 is false, and the loop stops without printing 16.
The step i += 2 is shorthand for i = i + 2. Because the start, 4, is even and the step is 2, every value of i is even.
Counting down
To go in reverse, start at the large end, subtract in the update, and flip the comparison so that the loop continues while the counter is still at or above the last value you want:
let i = 12;
while (i >= 4) {
console.log(i);
i -= 2;
}
This prints 12, 10, 8, 6 and 4. The comparison changed from <= to >= because the counter now approaches the limit from above. If you kept <= with a start of 12 and a limit of 4, the condition would be false at the very first check and the loop would never run.
The exercise, step by step
The task is to print 23, 21, 19, and so on, down to 7. Three questions decide the code: where to start, when to stop, and how far to move each time.
- Declare the counter. It starts at 23, the first number to print, in a
numbervariable namedcounter. In TypeScript the annotation: numberis optional here, because the compiler works out the type from the starting value. It is written out in the solution to make the type visible. - Write the loop and its condition. The last number to print is 7, and the counter is moving down, so the loop must run while
counteris greater than or equal to 7. Using>=instead of>is what lets 7 itself be printed. - Print the counter with
console.log(counter)inside the loop. - Decrease the counter by 2. Subtracting 2 from an odd number gives the previous odd number, so the values stay odd: 23, 21, 19, and so on.
The solution in TypeScript
let counter: number = 23;
while (counter >= 7) {
console.log(counter);
counter -= 2;
}
It prints 23, 21, 19, 17, 15, 13, 11, 9 and 7. After 7 is printed, counter becomes 5, the condition 5 >= 7 is false, and the loop ends.
Things to watch for
- Forgetting the update. Without
counter -= 2the counter never changes, the condition stays true, and the program prints 23 forever. A later lesson looks at infinite loops in detail. - Moving the wrong way. Writing
counter += 2in a loop that is meant to count down makes the counter grow, socounter >= 7stays true forever. - An off-by-one limit. With
counter > 7the loop stops one step early, and 7 is never printed. Whenever the last value should be included, use<=or>=. - The wrong parity. Starting at 22 would print the even numbers instead. The start decides whether you get odd or even values, because the step of 2 never changes the parity.
- Assignment instead of comparison. The condition needs a comparison operator such as
>=. A single=assigns a value, and a condition likecounter = 7is not what you want.
The compiler does not stop you from writing any of these loops. Types only check that the values are numbers. Whether the loop ends is a question about your logic, and the compiler will not answer it.
Key takeaways
- A counting
whileloop needs a start value, a condition and an update that moves toward the end. - To step by a different amount, change the update:
+= 2,-= 2, and so on. - To count down, start high, use
>or>=in the condition, and subtract in the update. - Choose
<=or>=when the last value must be included. - The parity of the start value, together with an even step, decides whether the loop visits odd or even numbers.