This article introduces the concept of counted (for) loops in BBC BASIC programming, with several examples, and some possible pitfalls.
Counted loops are a way of repeating a set of instructions a given number of times. For instance, we might want to ask the user for a set of numbers to sum, average, or otherwise process.
For those needing more information about loops in programming, then the main article Programming Loops in Computer Code might be of particular interest. It covers most kind of loops, including a look at counted loops. Once the basic premise is understood, the rest of this article might be easier to digest.
BBC BASIC handles counted loops in a very similar manner to Visual Basic, but slightly differently to C. The differences should become clear as the article unfolds.
When we design a counted loop, we need three things:
The variable can be though of as a box into which we can put a value, and then look it up, or change it. In counted loops, the value is usually a number. For more on variables, the Using Variables in Computer Programming article might be of interest.
The general skeleton for a FOR loop is as follows:
In the above snippet, the code will be executed 10 times, until the variable Counter% is equal to the ending value, EndValue%. Rather than using variables, we could have used constants for the starting and ending values. For obvious reasons one cannot use a constant for the counter.
It is also possible to leave out the variable name in the NEXT statement, however it is advisable to leave it intact. In the event that BBC BASIC is unable to find a matching counter variable, the Can't Match For error will be generated.
If we wish to leave out certain values, we can use the STEP keyword to identify that we wish to add more than one unit to the counter. For example, to print out every other value, we could use code such as:
It is important to think of the effect that this will have on the number of cycles that the FOR loop will execute. If the STEP value is higher than half the difference between the starting and ending values, for example, then the loop will only execute once.
This is important when considering using a variable in place of a constant for the STEP value. Perfectly legal, but sometimes, in the pursuit of efficient execution, it might be better to test for a value that does not make sense, before wasting cycles on the loop mechanism.
We can also write reverse loops, which count down. It is important to note that the STEP parameter becomes mandatory in such cases, with -1 being used to count down in single steps:
Of course, we can also count down in larger steps by specifying a higher negative number in the STEP.
Unlike other languages, BBC BASIC does not offer an easy way to break out of a FOR loop. However, there is an accepted way to do it - set the counter variable to a value outside (beyond) the ending value, and then jump to the NEXT statement. This can be achieved thus:
The language will not usually allow the programmer to exit a FOR loop with a GOTO statement. This is both bad programming practice, and opens up the possibility to corrupt the stack.