Wednesday, November 13, 2013

1.2.2 Six basic computer operations with Pseudocode

  1. A computer can receive information


When a computer is required to receive information or input from a particular source, whether it be a terminal, a disk or any other device, the verbs Read, Input and Get are used in Pseudocode. Read is usually used when the algorithm is to receive input from a record on a file, while Get, Input are used when the algorithm is used to receive input from the keyboard. For example, typical Pseudocode
instructions to receive information are :


Read student name
Get system date
Read number-1, number-2
Get tax-code
Input marks

(Highlighted words are keywords).

Each example uses a single verb, Read or Get followed by one or more nouns to indicate what data is to be obtained. At no stage is necessary to specify the source of the data, as this information is not required until run time.


      2. A computer can put out information


When a computer is required to supply information or output to a device, the verbs Print, Write, Put, Output or Display are used in Pseudocode. Print is usually used when the output is to be sent to the printer, while write is used when the output is to be written to a file. If the output is written to the screen, the words put, output or display are used in Pseudocode. Typical Pseudocode examples are -

Print 'Program completed'
Write customer record to master file
Put out name, address and postcode
Output total-tax
Display 'End of data'

In each example, the data to be output is described concisely using mostly lower-case letters.


     3.  A computer can perform arithmetic 

Most programs require the computer to perform some sort of mathematical calculation or formula and for these a programmer may use either actual mathematical symbols or the words for those symbols. For instance, the same Pseudocode instruction can be expressed as either of the following:

Add number to total

Total = total + number

Both expressions clearly instructs the computer to add one value to another so either is acceptable in Pseudocode. The equal symbol ‘=’ has been used to indicate assignment of a value as a result of some processing.

To be consistent with high-level programming languages, the following symbols can be written in Pseudocode :

                +             Add
    -                 Subtract
    *            Multiply
    /             Divide
   ( )           Parentheses
 
The verbs Compute and Calculate are also available. Some Pseudocode examples to perform a calculation are:

Divide total – marks by student-count
Sales – tax = cost – price * 0.10
Compute C = (F - 32) * 5/9


When writing mathematical calculations for the computer, the ‘order of operations’ common to all programming languages, should be considered. The first operation carried out will be any calculation contained within parentheses. Next, any multiplication or division as it occurs from left to right, will be performed. Then any addition or subtraction, as it occurs from left to right, will be performed.



      4. A computer can assign a value to a variable or memory location 

  
There are three cases where you may write Pseudocode to assign a value to a variable or memory location:

  1. To give data an initial value in Pseudocode, the verbs Initialize or Set are used.
  2. To assign a value as a result of some processing, the symbol ‘=’ is written
  3. To keep a piece of information for later use, the verbs Save or Store is used 
Some typical Pseudocode examples are:

Initialize total accumulators to zero
Set student - count to 0
Total - price = cost - price + soles - tax
Store customer - num in lost - customer - num

    5. A computer can compare two variables and select one of two alternative actions



An important computer operation available to programmer is the ability to compare two variables and then, as a result of the comparison, select one of two alternative actions. To represent this operation in Pseudocode, special keywords are used: IF, THEN and ELSE. The comparison of data is established in the If clause, and the choice of alternatives is determined by the Then or Else options. Only one of these alternatives will be performed. A typical Pseudocode example to illustrate this operation is :

IF student is part-time THEN
    add 1 to part-time count
ELSE
    add 1 to full-time count
ENDIF



In this example the attendance status of the student is investigated, with the result that either the part time count or the full time count accumulator is incremented. Note the use of indentation to emphasize the Then and Else options, and the use of the delimiter ENDIF to close the operation.


   6. A computer can repeat a group of actions



When there is a sequence of processing tips, which need to be repeated, two special keywords, DO WHILE and END DO are used in Pseudocode. The condition for the repletion of a group of actions is established in the Do While clause, and the actions to be repeated are listed beneath it. For example:

DO WHILE student-total < 50
    Read Student record
    Print student name, address to report
    Add 1 to student-total
END DO


In this example it is easy to see the statements which are to be repeated, as they immediately follow the DO WHILE statement and are intended for added emphasis. The condition which controls and eventually terminates the repetition is established in the DO WHILE clause, and the keyword END DO acts as delimiter. As soon as the condition for repetition is found to be false, control passes to the next statement after the END DO.
 

 
 



 

2 comments: