BWD Chapter 08: Feeling loopy

2004-11-08 18:57 - Beginners Web Development

Chapter 08
Feeling loopy

In chapter seven we learned about the WHILE loop, and we used it to expand the game that we started in chapter six. Here we will learn a bit more about loops.

Loops are one way to perform repetitive tasks. Our game used a loop to repeat the same tasks for a turn over again, rather than typing thw whole thing out many times. This is of course the purpose for loops. But there are many different types of tasks that might need repeating, so there are many types of loops, each of which operates slightly differently.

We learned about the WHILE...WEND loop last chapter. At the beginning of the loop it checks its condition, and if it is true it executes the loop, if not skips to the next line after the loop. At the bottom of course it loops to the top. WHILE...WEND has a cousin called DO...LOOP.

DO...LOOP does everything exactly the same as WHILE...WEND with one vital difference. It checks its condition at the bottom of the loop, not the top. This means that DO...LOOPs always execute at least once. This is a subtle difference but can be useful in the right situation. Well, actually, a DO...LOOP can be written both ways, the condition at the top or the bottom. This is just an important possibility to remember. It also has another keyword that makes it even more powerful. Just before the condition, you must use the keyword WHILE or UNTIL to control how the conditional effects the loop. Clearly WHILE loops while the condition is true, and UNTIL loops until the condition is true.

When it is what you want, a WHILE...WEND loop is a nice compact set of statements to fit into your code. Do not forget, though, the power of the DO...LOOP which is also available. For DO...LOOP also has yet another trick in its bag, the EXIT statement. EXIT can do many things, but an EXIT DO statement will at any time stop the current DO loop. You probably noticed that I set turn to 99 in my program at the end of the last chapter. I used a WHILE loop, and EXIT will not work for WHILE loops. So now that we know, we should make that program use a DO WHILE...LOOP and an EXIT statement when the player wins. Can you do that?

There is one more loop, and you will hate me for introducing it last. It is called a FOR loop, and it specializes in looping for a particular number of times. It looks like this:

FOR i = 1 TO 10
  PRINT i
NEXT

A FOR loop takes care of a few steps of work we did back in our guessing game all at once. We had to set our turn variable to one, check it in our WHILE condition, and be sure to add one to turn at the end of the loop. The FOR does this all at once, takes care of the setting and checking and adding, with just the first line. We can also use EXIT FOR if necessary. From now on remember that FOR loops are good for looping a paricular number of times, and the other loops are best for looping continually until a particular thing happens (or stops happening).

Comments:

No comments!

Post a comment:

Username
Password
  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.
Subject:
Comment:

You may use Markdown syntax in the comment, but no HTML. Hints:

  • An empty line between text will create a paragraph boundary.
  • Use angle braces around a plain URL to auto-link it: <http://www.example.com/>.
  • Use this format to create a link with different text showing: [An Example](http://www.example.com/).
  • Use backticks (``), not leading spaces to enclose a code block.

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)