Suite101

Flow Control in BASIC Programming

Changing the execution sequence in a BASIC program

© Guy Lecky-Thompson

An introduction to flow control in programming, including the use of GOTO and GOSUB, with some good reasons why, and why not, to use these to change execution sequence.

Introduction

One of the concepts common to programming in procedural languages is that the evaluation of the code block is, by default, from top to bottom. However, it is often convenient to be able to change the way that the flow of control is passed around in a program.

In the Functions and Procedures articles, we saw how it is possible to call a named block of code, but there are also other mechanisms provided by most BASIC style languages to change the control path in other ways. Flow control is the term given to the manipulation of the path that the computer takes through your program.

The Programming Concepts and Principles article covers some useful points that will help in understanding the use of flow control in BBC BASIC programming.

The Stack

It is worth pointing out at this stage that most manipulation of flow control (be it through Loops, Functions or Procedures, as well as these techniques) causes the computer to stop what it is doing, and do something else. In doing so, it needs to be sure that it maintains:

  • The last point of execution;
  • The 'machine state' at that time.

If it does not know at what point it left the last instruction, it will not be able to get back again. Similarly, if the state is not stored, it will be unable to pick up where it left off. These values are stored on something called the stack.

Since the stack is dynamically allocated storage, it both limits the amount of memory available for other things (like variables), and is also self limiting. In other words, we cannot jump around in a program indefinitely. At some point, we will get a stack error. Usually when the computer runs out of memory.

The stack influences the use of:

  • Function calls;
  • Procedure calls;
  • Loops, and nested loops.

There are some flow control statements that do not affect the stack, such as GOTO. Most, however, affect the stack for exactly the same reason that the above three categories do.

GOTO, GOSUB & RETURN

It is not considered good programming style to use the GOTO statement, since it changes the flow of a program by referencing a label or line number. A label can be moved, but is not supported by BBC BASIC. Visual Basic, C, and other languages support the use of labels with a GOTO statement, but BBC BASIC restricts its' use to line numbers.

Care must be taken in using GOTO, for the reason that renumbering the program will change the line numbers that each GOTO statement refers to. Hence, the program will no longer run as predicted. A clever editor will change the line numbers referred to automatically, but the best solution is to avoid using GOTOs as far as possible.

Consider, for example, the following program:

10 PRINT "HELLO"
20 GOTO 10
30 REM this line never executed

It is a very simple example of what can happen in a program where GOTO use present. In a more complex piece of code, the chances of an infinite loop being entered into by accident increases dramatically with the inclusion of GOTO statements.

The same caveats apply to the GODUB statement, which, in conjunction with a line number, continues execution at the specified line number. The key difference is that the stack is preserved, as the program is fully expected to resume execution at some point, once the RETURN statement is encountered.

 100 ON ERROR GOSUB 1000
 110 ...
1000 REM Error handling subroutine
1010 REM Handle the error here
1020 RETURN

In the above snippet, any information that the program needs in handling the errors that it encounters must be held in global variables. This makes it desirable to try and avoid this kind of error handling subroutine.

There are some good reasons to use GOTO and/or GOSUB; the ability to supply a calculated line number among them. However, it is more usual to try and replace their use with well-named PROCedures instead. It certainly makes the programs easier to read.

Jumping Out

There are also some stack-related rules to follow when changing the flow around a program. For example, never use GOTO to jump out of a function, procedure, loop, etc. because the stack will become corrupted. This kind of error could become reasonably commonplace in programs using GOTO and GOSUB, especially where the programmer is inexperienced.

Links


The copyright of the article Flow Control in BASIC Programming in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Flow Control in BASIC Programming in print or online 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