Custom Search
 
  

 
CONTROL STATEMENTS .

Up to this point, the BASIC statements we have used have been executed sequentially. That is, after one statement has been executed, the statement immediately following it (the one with the next higher line number) will be executed until the program ends. Now we will discuss the statements which may be used to alter this sequence of execution and some of the reasons why this may be desirable.

This change in the normal sequence of execution is called transfer of control branching. There are two categories of transfer of control statements. They are conditional and control is symbols are used in evaluating the condition.

For example:

If the condition (X>2) proves true, the program branches to line 50; if the condition proves false, processing continues sequentially through the program. Unconditional transfer of control is not dependent on any condition. It is achieved by the control statement, GOTO, which specifies the line number to which control is to be transferred.

For example:

When this statement is executed control will be unconditionally transferred to line 99.

GOTO Statement (Unconditional)

Using the unconditional GOTO statement is one method of interrupting the sequential execution of program statements. You can use it to branch (transfer control) to any part of a program during execution.

In the first part of the chapter, we discussed how to use the READ, DATA and INPUT statements to introduce data into a program. If we wanted to run several sets of data through a program, it would be necessary to rerun the program for each new set of data. By putting a GOTO statement in a program, we can transfer control back to the beginning of the program to process additional data. This creates a loop. A loop is any sequence of statements that is to be repeated some specified number of times, or until a particular condition is met.

Example:

Loop using GOTO, READ and DATA

The GOTO statement in line 70 creates the loop. That is, it tells the program to branch back to the READ statement in line 40, which assigns the next data element to variable F, performs the calculations, prints the results, and repeats this process until all the data elements have been used. The first time through the loop, F will equal 96, the second time F equals 93 and so on. When all the data elements have been read, you get an "out of data" error message. Later, we will discuss how to prevent this, using other control statements.

Study the loop and become familiar with how a loop works. Note that the PRINT statement in line 30 which prints the heading is outside of the loop. One major point to remember in creating a loop is that you only want to include functions that are to be performed repetitively inside the loop. Do not include statements that are to be performed only once during program execution. If we had included line 30 inside the loop, a heading would have been printed each time the loop was executed.

Now lets examine how a loop with an INPUT statement can be used to introduce data into a program.

Example:

Loop using GOTO and INPUT

Using the GOTO statement in this program will cause the computer to query the user for more input after each entry has been processed. Note that lines 10 and 20 are outside of the loop. Line 10 is a prompt used to specify what to input, and line 20 is the heading which we wouldnt want printed each time. This program will stay in this loop and continue to query the user because we did not provide an exit from the loop. The statements we can use to control (exit) a loop will be discussed next.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business