0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>In the previous step, you wrote the main loop. The most important thing is that this loop can stop!</p>
1
<p>In the previous step, you wrote the main loop. The most important thing is that this loop can stop!</p>
2
<p>Remember the condition:</p>
2
<p>Remember the condition:</p>
3
while (<b>score > -1</b>) { }<p>In order for the loop to stop, the condition must at some point become false. For this to happen, the variable score must change within the loop. That’s what we made sure would happen when we started to record the result of the next shot at each iteration.</p>
3
while (<b>score > -1</b>) { }<p>In order for the loop to stop, the condition must at some point become false. For this to happen, the variable score must change within the loop. That’s what we made sure would happen when we started to record the result of the next shot at each iteration.</p>
4
<p>If the loop stop condition does not work, the loop cannot stop. This is an endless loop, one of the favorite errors of programmers. By the way, the loop for can also be made infinite.</p>
4
<p>If the loop stop condition does not work, the loop cannot stop. This is an endless loop, one of the favorite errors of programmers. By the way, the loop for can also be made infinite.</p>
5
<p>Part of the logic of the program is ready: we get the results of all the shots and stop the game when a miss occurs. All we have left to do is calculate the sum of points.</p>
5
<p>Part of the logic of the program is ready: we get the results of all the shots and stop the game when a miss occurs. All we have left to do is calculate the sum of points.</p>
6
<p>To do this, before the loop, declare variable total, and in each iteration, we will add to it the points received for the shot.</p>
6
<p>To do this, before the loop, declare variable total, and in each iteration, we will add to it the points received for the shot.</p>
7
<p>And, at last, we’ll log the result of the game in the console.</p>
7
<p>And, at last, we’ll log the result of the game in the console.</p>