Q-BASIC provides facilities for controlling the order of execution of statements. These are known as the control statements. The basic control statements in BASIC are:
1. GO TO Statement
2. ON….GO TO Statement
3. IF….THEN Statement
JUMPING-THE GO TO STATEMENT
This is the simplest of all the control statement and causes the computer to jump from one point to another in the program.
n is the line number of a statement in the program. During the running of a program when a statement like
50 GO TO 70
is met the flow of control will jump to the statement labeled 70. This happens unconditionally and therefore it is called an unconditional GO TO statement. Another example could be:
50 GO TO 30
That is, the statement number n may be below or above the GO TO statement. If the statement n is below, some statement will be skipped and the jump is known as ‘forward jump’. If the statement n is above, some statement will be repeated and the jump is known as ‘backward jump’.
Illustration of a forward jump.
10 READ A, B
20 READ X, Y
30 GO TO 60
40 LET X=X*X
50 LEY Y=Y*Y
55 PRINT X, Y
60 LET X=A*X
70 LET Y=B*Y
75 PRINT X, Y
80 DATA 5, 4, 3, 2
In Example after reading the values of A, B, X and Y the computer jumps to statement 60 thereby by-passing the statements 40, 50 and 55.
MULTIPLE BRANCHING – THE ON….GO TO STATEMENT
The ON…GO TO statement is a conditional transfer statement. While a simple GO TO statement allows only one transfer point and IF….THEN allows two transfer points, the ON…GO TO can have more than two transfer points thus providing multiple branching facility. The form of this statement is
ON [ numeric variable or expression] GO TO n1,n2,n3,n4…..
The expression is a valid BASIC expression and n1, n2, n3, n4.. are the statement numbers in the program.
When the statement is encountered, the expression is evaluated and rounded off to an integer value. Then control is transferred to a statement as shown below:
Integer value Action taken
1 – Program jumps to n1
2 – Program jumps to n2
3 – Program jumps to n3
And so on.
10 ON CODE GOTO 100,200,300,400
The value of CODE controls the line to which the control is to be transferred.
For example, in statement no. 10 above, if CODE=1 then program control will be transferred to line number 100, if CODE=2 it will go to 200 and so on.
If the integer value is less than one or greater than the total number of line numbers present in the list, then an ‘out of range’ error message will occur. The value of the expression determines the path of execution.
What is Infinite Loop?
Infinite loop means a never-ending loop. The computer goes round and round indefinitely and never reaches the END statement.
Breaking the Infinite Loop
There are several ways of getting out of a loop situation. They vary considerably between computers. Common methods are:
1) A program which is looping can be stopped simply by striking a key on the terminal.
2) Pressing the CONTROL key and typing the letter C can halt the computer.
3) Typing the BREAK key can break the loop.
4) If a program control contains an INPUT statement in the loop, then typing in a STOP in place of the requested input will halt the computer.
5) If the control contains a READ statement in the loop, then the computer will stop with an error message when the data is exhausted.
A well-written program will have some built-in provision to get the computer out of the loop after the required tasks are completed. The GO TO is a widely used control statement. It is often used at the end of a program to direct the computer to go to READ & INPUT statement at the start of program. When a program contains multiple branches, it is used at the end of the branches to direct them to meet at a common point. Counting loops frequently employ the GO TO statement.