Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
121
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

132 Part II: Learning Programming with Liberty BASIC

I = 1

WHILE I <= 10

PRINT “This is I = “; I

IF I = 5 THEN EXIT WHILE

I = I + 1

WEND

END

Normally this WHILE-WEND loop would repeat ten (10) times but the IF I = 5 THEN EXIT WHILE command tells the program to stop after the program has looped only five (5) times.

The EXIT WHILE command can be useful to make sure a WHILE-WEND loop eventually stops running, but make sure that you don’t exit out of a loop sooner than you really want to.

Endless loops #1: Failing to modify the

Boolean expression inside the loop

One of the trickiest parts of using a loop is avoiding an endless loop. An endless loop occurs whenever the Boolean expression of a loop never changes to tell the computer to stop running the instructions inside the loop as in the following program:

I = 1

WHILE I < 5

PRINT “This loop never ends.”

WEND

END

This loop never stops because the value of I never changes. Thus the Boolean expression I < 5 always remains true, so the WHILE-WEND loop keeps running the instructions between the WHILE and WEND commands. In this case, the program just keeps printing, This loop never ends.

To fix this problem, you need to insert a command inside the WHILE-WEND loop that changes the variable in the Boolean expression such as inserting the command I = I + 1, as shown in the following example:

I = 1

WHILE I < 5

PRINT “This loop eventually ends.”

I = I + 1

WEND

END

Chapter 10: Repeating Yourself with Loops 133

Endless loops are a common bug that keeps a program from working correctly. You can often recognize an endless loop if a program never responds to anything you try to do and you can’t do anything except shut your computer off and start all over again.

Endless loops #2: Failing to initialize a Boolean expression outside the loop

Another variation of the endless loop occurs if you forget to define the value of a variable outside the WHILE-WEND loop, as in the following program:

WHILE I < 5

I = 1

PRINT “This loop never ends.”

I = I + 1

WEND

END

In this example, the value of variable I is always set to one (1) at the beginning of the WHILE-WEND loop and then gets incremented to two (2) at the end of the WHILE-WEND loop. But each time the WHILE-WEND loop runs, the value of I is reset to one (1) so the Boolean expression I < 5 always remains true, and thus the loop never ends.

The correct way to use the preceding WHILE-WEND loop is to place the statement I = 1 right in front of the WHILE-WEND loop, as in the following example:

I = 1

WHILE I < 5

PRINT “This loop will eventually end.”

I = I + 1

WEND

END

In this example, the value of the I variable is set to one (1) before the computer runs the WHILE-WEND loop. Once inside the WHILE-WEND loop, the value of I steadily increases until the value of I is equal to 5, which makes the Boolean expression I < 5 false; therefore, the WHILE-WEND loop ends.

To avoid endless loops in your program, always remember the following two points:

Initialize all variables that make up a Boolean expression right before a WHILE-WEND loop. This makes sure that your variable starts out with a value that you intended.

Make sure that the variable of the Boolean expression changes inside the WHILE-WEND loop.This makes sure that your loop eventually ends.