LOOPS, COUNTERS, & ACCUMULATORS


One of the best features of computers is their ability to repeat certain steps over and over without getting tired. When certain steps are repeated over and over, this process is called a LOOP in programming languages. In order to create a loop in a program, one must tell the computer where the loop starts, with the command DO and where the loop ends, with the command LOOP. All the statements inside the DO - LOOP commands are repeated over and over until a certain condition is found to terminate and exit the loop.

To exit a loop, we must ask the computer to make a decision to see if we are done processing the information. All decisions in QBASIC use the command IF followed by a variable and the condition for leaving the loop. To repeat a loop 20 times we would use a command such as the one below inside the DO--LOOP structure:

IF COUNTER = 20 THEN EXIT DO

If we know how many times we want to repeat the loop, we can set up a counter to keep track of the number of times we repeated the loop, each time increasing the counter by one. The QBASIC command to accomplish this task is:

COUNTER = COUNTER + 1

The above command tells the computer to take the old value of COUNTER from its memory location and add one to it. Then store the new answer back into the same location called COUNTER. This process produces a counter. The first time: 0+1 and stores a one into COUNTER. Then 1+1, then 2+1, 3+1, and so on until we get to the end.

An ACCUMULATOR is a memory location used to store the sum or the total of different numbers. The accumulator works the same as the counter, the only difference is that every time we add a different number to the old total. To set up an accumulator we would use a command such as:

SUM = SUM + VALUE

SUM is the name of the accumulator and VALUE is the number to be added each time we go through the loop. The above command tells the computer to take the old total in SUM from memory and add the new number stored in VALUE to it. Then store the new total back into the same location called SUM.

E X A M P L E P R O G R A M


The following example uses loops, counters, accumulators, and the READ/DATA
statements. Problem: Read several numbers from data statements. Print all the numbers. At the end print, the total and the average of all the numbers with proper labels.

REM ***** PROGRAM #5.1 *****
REM ***** BY YOUR NAME *****
REM ***** TODAY'S DATE *****

REM ** This program will read and print several numbers.
REM ** At the end, it will calculate and prints the total
REM ** and the average of all the numbers.

REM ** MEMORY LOCATION USED **
REM **
REM ** Num% = Holds the Value/number **
REM ** Sum% = Total of the values **
REM ** Count% = Counts the number of items **
REM ** Avg! = Hold the average of the items **

Sum% = 0 :REM set the total at zero
PRINT "NUMBERS" :REM Prints the heading/Title

DO :REM Starts a loop structure

READ Num% :REM Reads the next value
IF Num% < 0 THEN EXIT DO :REM Leaves the loop when negative
PRINT Num%; :REM Writes the value
Sum% = Sum% + Num% :REM Adds value to the total
Count% = Count% +1 :REM Adds one to the counter

LOOP :REM Ends of the loop structure

PRINT:PRINT:REM Prints two blank lines
Avg! = Sum%/Count% :REM Calculates the Average
PRINT "Total Items: ";Count% :REM Print the # of items
PRINT "THE SUM IS: "; Sum %:REM Prints the Total/Sum
PRINT "THE AVERAGE IS: "; Avg! :REM Prints the Average

DATA 64,45,67,78,84,45,89,68
DATA 77,55,59,83,93,82,75,99,-1

Activity 10: QBASIC - The DO ... LOOP


10.01 Write a Structured BASIC program to Read several numbers from data statements. Print each number. Find the total and the average of the numbers. Print the total and the average of the numbers with appropriate labels.


10.02 Write a Structured BASIC program to Read several employee records containing a name, gross pay and total deductions. Print all the information. Also calculate and display the net pay for each employee. At the end calculate and print the average of the gross pay.


10.03 Write a Structured BASIC program to Read and print the following report on the screen. Calculate the average for each student.

RECORD OF MARKS

Name

English

Math

Computers

Average

Abel, R

56

57

60

 

Bibs, J

74

90

91

 

Cates, R

44

50

52

 

Gala, J.

55

59

69

 

Holt, M

83

77

85

 

Hoy, J

61

88

76

 

May, L

64

45

76

 

Prose, V

35

70

79

 

Rosen, R

85

85

85

 

Simon, T

56

40

83

 

 

 

 

 

Averages

 

 

 

 



10.04 Same as in Program 10.03. Also your program must calculate the average for each student and the class average for each subject. Include these averages at the bottom of the report.


MAIN