Interactive online courses HTML Academy
2026-03-09 10:55 Diff

Great! You figured the for loop out. It’s convenient to use when the number of repetitions is known in advance, even before the start of the loop. For example, when you know the number of pages in the document or the number of days.

But there are situations where the number of repetitions is not known beforehand. For example, biathlon: shoot at targets until you close them all.

In such cases, when the loop is repeated again and again until the condition is fulfilled, it is more convenient to use the loop while.

while (condition) { actions }

The actions specified in the loop will be executed again and again until the condition returns false.

Let’s practice using the loop while before the boss gives us yet another job to work on.

When on break from work, the boss likes to play darts. He is trying to set a record: to score the maximum number of points in a series of shots without a miss. As soon as he misses, the series ends (and judging by the holes in the wall, he misses quite often).

We need to write a simple program that records these record shots. We use the command muffin.getScore() to get the result of the next shot. If we hit the target, we get a number ranging from 1 to 10, if we miss, we get -1.