This article looks at an alternative to counted loops, using the REPEAT, UNTIL, WHILE and ENDWHILE constructs.
This article introduces repetitive loops; a concept that enables programmers to repeat a block of code a number of times, until a condition is met. It is useful in cases where we do not know in advance how many times the code block should execute, but where we can test a specific condition in order to exit the loop.
In those cases where we know the number of times a loop should execute, even if it is a variable number of times, we can use a counted loop. The article Counted Loops in BBC BASIC Programming introduces this mechanism. For a general overview, the Programming Loops in Computer Code gives a language neutral introduction to the various concepts.
In order to construct a loop, it is necessary to know several pieces of information in order to design the correct behavior. These are:
We can either construct a loop which executes the code block first, before testing the completion condition, or one which tests the condition before each cycle. The former is known as a REPEAT .. UNTIL loop, and the latter a WHILE .. ENDWHILE loop.
This is a very simple mechanism - we simply execute a code block until a tested condition is met. The general skeleton for this loop is as follows:
The condition to test can be any BBC BASIC condition, similar to that that can be used in an IF statement. Any combination of constants, variables, operators, logical operators, and return values from functions can be used.
Some common uses for this type of loop include a single line, waiting for a key-press, network input, or even a Windows event.
One aspect of the REPEAT UNTIL loop is the possibility to create an endless loop. The only way to break out of this loop is to stop the program; needless to say this is not generally advisable, but it can be useful in video game programming, for example.
The skeleton code for such a loop is as follows:
The FALSE statement, slightly paradoxically, is always true. The least useful form for this, is the single line:
This will compile, and run, but it will not actually do anything else at all, and the program will seem to have stopped responding.
The alternative to the REPEAT .. UNTIL loop, is the WHILE .. ENDWHILE. In this form of loop, the condition is tested at the top of the code block - before even the first cycle is executed. The skeleton for this is as follows:
Again, the condition can be any mixture of valid operators, return values, and pieces of data that could be tested in order to determine whether the next cycle should be executed. One of the key differences between the REPEAT .. UNTIL and WHILE .. ENDWHILE is that a REPEAT .. UNTIL loop will execute the code block at least once.
There is no real way to exit these loops, unlike the FOR loop. In fact, the only way to exit is to make sure that the tested condition evaluates to TRUE. One of the reasons for this is that the GOTO statement should not be used to jump outside the code block, because it will likely lead to stack issues.
Without entering into the details of compiler design, when a loop is about to be entered, the address of the entry point is pushed onto the stack. When the end of the loop is encountered, this value is pulled back off the stack, and the memory address jumped to.
If the mechanism that saves and restores this address is not used, then the value could be left on the stack, with undefined consequences.