Three Catagories of Algorithmic Operations: How To Represent Algorithms?
Three Catagories of Algorithmic Operations: How To Represent Algorithms?
Unfortunately not every problem or task has a "good" algorithmic solution. There
are
SEQUENCE is a linear progression where one task is performed sequentially after another.
WHILE is a loop (repetition) with a simple conditional test at its beginning.
IF-THEN-ELSE is a decision (selection) in which a choice is made between two alternative
courses of action.
Although these constructs are sufficient, it is often useful to include three more constructs:
REPEAT-UNTIL is a loop with a simple conditional test at the bottom.
CASE is a multiway branch (decision) based on the value of an expression. CASE is a
generalization of IF-THEN-ELSE.
FOR is a "counting" loop.
SEQUENCE
Sequential control is indicated by writing one action after another, each action on a
line by itself, and all actions aligned with the same indent. The actions are
performed in the sequence (top to bottom) that they are written.
Example (non-computer)
Brush teeth
Wash face
Comb hair
Smile in mirror
Example
READ height of rectangle
READ width of rectangle
COMPUTE area as height times width
Common Action Keywords
Several keywords are often used to indicate common input, output, and processing
operations.
Input: READ, OBTAIN, GET
Output: PRINT, DISPLAY, SHOW
Compute: COMPUTE, CALCULATE, DETERMINE
Initialize: SET, INIT
Add one: INCREMENT, BUMP
IF-THEN-ELSE
Binary choice on a given Boolean condition is indicated by the use of four keywords:
IF, THEN, ELSE, and ENDIF. The general form is:
IF condition THEN
sequence 1
ELSE
sequence 2
ENDIF
The ELSE keyword and "sequence 2" are optional. If the condition is true, sequence
1 is performed, otherwise sequence 2 is performed.
Example
The WHILE construct is used to specify a loop with a test at the top. The beginning
and ending of the loop are indicated by two keywords WHILE and ENDWHILE. The
general form is:
WHILE condition
sequence
ENDWHILE
The loop is entered only if the condition is true. The "sequence" is performed for
each iteration. At the conclusion of each iteration, the condition is evaluated and
the loop continues as long as the condition is true.
Example
The OTHERS clause with its default sequence is optional. Conditions are normally
numbers or characters
indicating the value of "expression", but they can be English statements or some
other notation that specifies the condition under which the given sequence is to be
performed. A certain sequence may be associated with more than one condition.
Example
CASE Title OF
Mr : Print "Mister"
Mrs : Print "Missus"
Miss : Print "Miss"
Ms : Print "Mizz"
Dr : Print "Doctor"
ENDCASE
Example
CASE grade OF
A : points = 4
B : points = 3
C : points = 2
D : points = 1
F : points = 0
ENDCASE
REPEAT-UNTIL
This loop is similar to the WHILE loop except that the test is performed at the
bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are
used. The general form is:
REPEAT
sequence
UNTIL condition
The "sequence" in this type of loop is always performed at least once, because the
test is peformed after the sequence is executed. At the conclusion of each iteration,
the condition is evaluated, and the loop repeats if the condition is false. The loop
terminates when the condition becomes true.
FOR
This loop is a specialized construct for iterating a specific number of times, often
called a "counting" loop. Two keywords, FOR and ENDFOR are used. The general
form is:
FOR iteration bounds
sequence
ENDFOR
In cases where the loop constraints can be obviously inferred it is best to describe
the loop using problem domain vocabulary.
Example
The constructs can be embedded within each other, and this is made clear by use of
indenting. Nested constructs should be clearly indented from their surrounding
constructs.
Example
1. Computation/Assignment
2. Input/Output
if "condition"
6.1 (subordinate) statement 1
6.2 etc ...
else
7.1 (subordinate) statement 2
7.2 etc ...
while "condition"
9.1 (subordinate) statement 1
9.2 etc ...
Symbol Symbol Symbol Description
Name
(alias)
Flow Line Flow line connectors show the direction that the
(Arrow, process flows.
Connector)