Counted Loops in BASIC Programming

Describing the use of the FOR .. TO .. STEP .. NEXT construct

© Guy Lecky-Thompson

This article introduces the concept of counted (for) loops in BBC BASIC programming, with several examples, and some possible pitfalls.

Introduction

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.

The FOR .. TO .. NEXT Construct

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:

10 StartValue% = 0: EndValue% = 10
20 FOR Counter% = StartValue% TO EndValue%
30 REM Do something useful
40 NEXT Counter%

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.

The FOR .. TO .. STEP .. NEXT Construct

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:

10 StartValue% = 0: EndValue% = 10
20 FOR Counter% = StartValue% TO EndValue% STEP 2
30 REM Do something useful
40 NEXT Counter%

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.

Reverse Loops & Jumping Out

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:

10 StartValue% = 10: EndValue% = 0
20 FOR Counter% = StartValue% TO EndValue% STEP -1
30 REM Do something useful
40 NEXT Counter%

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:

10 StartValue% = 0: EndValue% = 10
20 FOR Counter% = StartValue% TO EndValue%
30 IF Counter% * 3 EndValue% THEN GOTO 50
40 REM Do some processing here
50 NEXT Counter%

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.

Links


The copyright of the article Counted Loops in BASIC Programming in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Counted Loops in BASIC Programming must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo