Tuesday, November 19, 2013

1.3.1 Three basic control stuctures

   1. Sequence

The sequence control structure is the straightforward execution of one processing step after another. In Pseudocode we represent this as a sequence of Pseudocode statements

Statement a
Statement b
Statement c


The sequence control structure can be used to represent the first four basic computer operations listed previously: to receive information, put out information, perform arithmetic and assign values. For example, a typical sequence of statements in an algorithm might read:

Add 1 to page-count
Print heading line
Set linecount to zero
Read customer record

These instructions illustrate the sequence control structure as a straightforward list of steps written one after the other, in a top-to-bottom fashion. Each instruction will be executed in the order in which it appears.

   2. Selections


The selection control structure is the presentation of a condition and the choice between two actions, the choice depending on whether the condition is true or false. This constructs represent the decision-making abilities of the computer and is used to illustrate the fifth basic computer operation, namely to compare two variables and select one of two alternate actions.

In Pseudocode, selection is represented by the keywords IF, THEN, ELSE and ENDIF

IF condition p is true THEN
     statements in true case
ELSE
     statements false case
ENDIF

If condition P is true then the statement or statements in the true case will be executed and the statements in the false case will be skipped. Otherwise (the Else statement) the statements in the true case will be skipped and statements in the false case will be executed. In either case, control then passes to the next processing step after the delimiter Endif. A typical Pseudocode example might read:
 

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


A variation of the selection control structure is the null Else structure, which is used when a task is performed only if a particular condition is true. The null Else construct is written in Pseudocode as:

IF condition is true THEN
     statement in true case
ENDIF



Note that the keyword Else is omitted. This construct tests the condition in the If clause and if that is found to be true, performs the statement or statements listed in the Then clause. However, if the initial condition is found to be false, no action will be taken and processing will proceed to the next statement after the ENDIF.

   3. Repetition


The repetition control structure can be defined as the presentation of a set of instructions to be performed repeatedly, as long as a condition is true. The basic idea of repetitive code is that a block of statements is executed again and again, until a terminating condition occurs. This construct represents the sixth basic computer operation, namely to repeat a group of actions. It is written in Pseudocode as:

DO WHILE condition p is true
     statement block
END DO



The DO WHILE loop is a leading decision loop; that is, the condition is tested before any statements are executed. If the condition in the DO WHILE statement is found to be true, the block of statements following the statement is executed once. The delimiter END DO then triggers a return of control to the retesting of the condition. If the condition is still true, the statements are repeated, and so the repetition process continues until the condition is found to be false. Control then passes to the statement, which follows the END DO statement. It is imperative that at least one statement within the statement block can alter the condition and eventually render it false, because otherwise the logic may result in an endless loop.
 

 
  

No comments:

Post a Comment