Learning Petit Lesson 2b: Loops

FOR loops
We've seen how we can make a basic infinite loop with GOTO. Many times, however, you'll need a loop that runs a certain number of times and then stop. What if we want to print the numbers from 0 to 5? This is certainly possible with GOTO, and you may have already figured it out on your own:

While this works, it is a bit clumsy. Now let's introduce the FOR loop:

This accomplishes exactly the same thing as the GOTO loop, but with 3 lines instead of 7. FOR lets us create a loop that runs a certain number of times. In this case, X is the loop counter - the variable that controls the loop (we can use any valid variable name for the loop counter, but X is a common choice in BASIC). We can set both the starting and ending values of the counter. If we wanted to print the values from 3 to 6, we would change the previous example to start

Each repetition of a loop is known as an iteration. This loop has 6 iterations (0,1,2,3,4,5).

STEP
You already understand the basic FOR loop and in many cases that's all you'll need. However, the basic FOR only lets the loop counter go up by 1 each iteration. What if we need to print even numbers from 0 to 6? We could do this:

That example is fairly straightforward but things could get messy if we used X repeatedly. Now we introduce the STEP counter:

This prints out the same values as the previous loop. The STEP counter controls how much the loop counter changes at each iteration. The value that the loop counter increases each iteration is called increment; so we can say a standard FOR loop has an increment of 1, but the last example has an increment of 2.

Lastly, we will cover looping in reverse:

This loop would print
 * 3
 * 2
 * 1
 * 0

If we need to go through values in reverse order, we can use a negative step value; this is called a decrement, so in this example we are decrementing by 1 each iteration.

Changing the loop counter
There is one last point to cover about FOR loops: nothing is stopping you from changing the value of the loop counter in the middle of the loop. You have to be very careful not to accidentally reuse that variable, or your code might do something very strange. Here's a simplified example of what can go wrong:

This loop will continue to print 0 forever because we cancel out the loop counter's increment by manually decrementing the counter each iteration. When the loop starts, X is 0. We print 0, then decrement X to -1. When the next iteration starts, X is incremented to 0 again. X never reaches 6, so the loop never stops.

Note we can take advantage of this to shortcut our way out of a loop without using a GOTO:


 * PRINT "The attack hits " NUMTIMESHIT " times!"
 * FOR X = 1 to NUMTIMESHIT
 * PRINT "You took " DAMAGE " damage
 * HEALTH = HEALTH - DAMAGE
 * GOSUB @TRYDODGE
 * IF DODGED THEN X = NUMTIMESHIT + 1
 * NEXT

In this example, the player has been struck by some attack that hits multiple times. We've decided to annoy the player by printing out how much damage they took for each hit. However, after each hit the player has a chance to dodge the remaining hits, which is calculated at the @TRYDODGE subroutine. If they did dodge successfully, we can break out of the loop early by setting the loop counter higher than its maximum value.