Languages and programs: a basic program

A BASIC program

Let's produce a program in BASIC which will carry out these flowchart steps. In this language, each line of program­ming must be numbered; the computer will then execute them in numbered sequence. It's conventional to number in steps of 10 (i.e. 10, 20, 30, etc.), as then additional lines can be inserted by typing intermediate numbers.

The first step in the program is to name the part of the memory in which the invoice total is to be held, and to set its initial value to 0. Let's name it simply TOTAL. TOTAL is then called a variable. (Note that some versions of BASIC require you to use single-character variable names, in which case we could have used 'T' as the name.) So the first

program line (line 10) is:

10 LET TOTAL= 0

Next, we tell the computer that it can expect the price and quantity of the first item on the invoice to be keyed in:

20 INPUT PRICE, QUANTITY

What we are in fact doing here is telling the computer to label a part of its memory 'QUANTITY' and a part 'PRICE' and put the first two values that it receives (via the key­ board) into those two parts of the memory.

Next, we want the computer to calculate the AMOUNT. This will be program line 30:

30 LET AMOUNT = PRICE * QUANTITY

This tells the computer to multiply the value held at address PRICE by the value of address QUANTITY and to store the result at another address in its memory which it is to label AMOUNT.

Next, we tell the computer to add the value stored at address AMOUNT to the value at address TOTAL, and then to put the result back into TOTAL. This new value in TOTAL replaces the previous value stored there. This is line 40:

40 LET TOTAL = TOTAL + AMOUNT

We must now tell the computer to repeat this process for successive values of PRICE and QUANTITY. The 'tra­ditional' way of doing this in BASIC is to use the GOTO command to tell it to return to line 20:

50GOT020

The computer will now repeat line 20 (using the next pair of values for PRICE and QUANTITY input at the keyboard) and all successive lines to line 50.

We also need to tell the computer when it has run out of items on the invoice, i.e. when there are no more values of PRICE and QUANTITY. If we don't it will stick at line 20 looking for another pair of values when there are in fact no more, and it will never get beyond line 50. One way of telling the computer that the data has come to an end is to provide it with impossible values of PRICE and QUAN­ TITY, such as negative values, or zero values, and to tell it to bypass line 50 when it receives these values.

Let's use zero values to tell it there are no more items on the invoice. This means that when we run the program we must key in 0 as the final value for PRICE. When the computer receives this it must bypass line 50 and go to line 60. To achieve this, we can insert after line 20 the following:

25 IF PRICE = 0 THEN GOTO 60

(Note that it does not matter that we are writing this line out of sequence, i.e. after line 50. The computer reads and executes each program line in its numerical sequence, not in the sequence in which it was keyed in.)

At line 60 we tell the computer to add 17.5% to TOTAL, and put the result back into TOTAL:

60 LET TOTAL= TOTAL* 17.5/100 +TOTAL

Then we want it to tell us what the answer is:

70 PRINT TOTAL

And the final instruction to the computer is to tell it that it has finished what we want it to do:

80END

clip_image001

A structured program

Even in the above very short program, you can see spaghetti programming beginning to creep in with the two GOTO statements causing branching to other places. A neater and clearer way of programming would be to use the structured programming commands REPEAT . . . UNTIL. In this case, we want to REPEAT lines 20, 30 and 40 UNTIL PRICE is zero. Here's the above program rewritten in this more structured form:

10 LETS= 0

15 REPEAT

20 INPUT PRICE, QUANTITY

30 LET AMOUNT= PRICE* QUANTITY

40LETTOTAL =TOTAL+ AMOUNT

50 UNTIL PRICE= 0

60 LET TOTAL= TOTAL* 17.5/100 +TOTAL

70 PRINT TOTAL

80END

Comments

Popular posts from this blog

WORKED EXAMPLES ON PROCESS SPECIFICATION.

Why do we need information systems, management structure, requirements of information at different levels of management.

The User Interface:Establishing User Interfaces