Loops

Show Index

One thing you may have noticed about "Our first game" is that in order to play it again, you must 'Run' it again.  But wouldn't it be nice if you wanted to play it several times, if it would just start over by itself?  This is where you can make use of Loops.

Several kinds of loops exist.  But all of them have this in common:  They loop until a certain condition is met.

The only exception to this is the Repeat-Forever combo of commands which, as their names imply, loop indefinitely.

Since we mentioned it, we will start with the Repeat command.  Usually it is followed by the Until command.   Included after the Until command is an expression or comparison that must equate to True in order for the loop to end.

An example usage could be:

counter = 0
Print "Before the loop."
Repeat
  counter = counter + 1
  Print "Count = " + counter
Until counter = 5
Print "Loop has ended."

The code above loops 5 times between the Repeat and Until commands.  Each time it loops, it adds 1 to the value stored in the variable 'counter'.  Once the value stored in the variable 'counter' equals 5, the loop ends, and program execution continues at the Print statement following the Until command.

The output of the above code would be:

Before the loop.
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Loop has ended.

 

Another loop command is the While-Wend combo of commands.  They basically perform the same function as the Repeat-Until commands, except that the check for 'the expression or comparison to equal True' is done BEFORE the loop, instead of after the loop.

Here is an example:

counter = 0
Print "Before the loop."
While counter < 6
  counter = counter + 1
  Print "Count = " + counter
Wend
Print "Loop has ended."

If you ran the above code you would see the following output:

Before the loop.
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Count = 6
Loop has ended.

If you're keen, you will have noticed that the code prints "Count = 6" even though the check at the While line is checking that counter is less than ( < ) 6.  That's because the counter IS less than 6 at the time when it equals 5.   It just gets incremented one more time after the While line and then is  Printed to the screen.  This doesn't mean that only While loops have this little quirk about them.  You will have to keep in mind that any time you are using a loop, the count may be one more or less than you may expect.  It just depends on how you write the code.

 

Another kind of loop is the For-Next loop.  It's usage could be like the following:

Print "Before the loop."
For counter = 1 to 5
  Print "Count = " + counter
Next
Print "Loop has ended."

If you ran the above code you would see the following output:

Before the loop.
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Loop has ended.

You can see that when using a For-Next loop, it's not necessary to increment the counter (counter = counter + 1).  The '1 to 5' part of the For-Next loop sets the counter to the first value (in this case, 1) the first time through the loop, and then automatically increments the counter by 1 every time until it equals the last value (in this case, 5).

So, why do we have all of these different kinds of loops?  They are there to make programming easier.  Each one can probably do what the others can do.  They each just do it a little bit differently.  We can pick and choose which one works best for us.
Repeat-Until loops are usually used when we want the loop to execute at least one time.
While-Wend loops are usually used when we don't want the loop to execute unless the While is true.  This means they may not execute at all.
For-Next loops are usually used to cause a counter to increase/decrease incrementally a certain amount of times.

 

So, now that we know how loops work, how could we apply them to our first game to make it start over by itself?

I'll show you how in Our first game - Part 2
Goto next lesson

 

If you've reached this page and there's no index on the left, Click here to show the Index Page