0% found this document useful (0 votes)
657 views

Three Catagories of Algorithmic Operations: How To Represent Algorithms?

The document discusses three categories of algorithmic operations: sequential, conditional, and iterative. It then discusses three ways to represent algorithms: natural language, formal programming languages, and pseudo-code. Pseudo-code uses a formalized language that is easier to understand than programming languages. The document provides examples and explanations of common pseudo-code constructs used for sequences, conditionals like if/else, and iterations like while loops. It also covers nested constructs, invoking subprocedures, and a summary of pseudo-code language constructions.

Uploaded by

Learn Islam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
657 views

Three Catagories of Algorithmic Operations: How To Represent Algorithms?

The document discusses three categories of algorithmic operations: sequential, conditional, and iterative. It then discusses three ways to represent algorithms: natural language, formal programming languages, and pseudo-code. Pseudo-code uses a formalized language that is easier to understand than programming languages. The document provides examples and explanations of common pseudo-code constructs used for sequences, conditionals like if/else, and iterations like while loops. It also covers nested constructs, invoking subprocedures, and a summary of pseudo-code language constructions.

Uploaded by

Learn Islam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Three Catagories of Algorithmic Operations

Algorithmic operations are ordered in that there is a first instruction, a second


instruction etc. However, this is not enough. An algorithm must have the ability to
alter the order of its instructions. An instruction that alters the order of an
algorithm is called a control structure

Three Categories of Algorithmic Operations:

1. sequential operations - instructions are executed in order


2. conditional  ("question asking") operations - a control structure that asks a
true/false question and then selects the next instruction based on the answer
3. iterative operations (loops) - a control structure that repeats the execution of
a block of instructions

Unfortunately not every problem or task has a "good" algorithmic solution. There
are 

1. unsolvable problems - no algorithm can exist to solve the problem (Halting


Problem)
2. "hard" (intractable) problems - algorithm takes too long to solve the problem
(Traveling Salesman Problem)
3. problems with no known algorithmic solution

How to represent algorithms?

1. Use natural languages


o too verbose
o too "context-sensitive"- relies on experience of reader
2. Use formal programming languages
o too low level
o requires us to deal with complicated syntax of programming language
3. Pseudo-Code - natural language constructs modeled to look like statements
available in many programming languages
Pseudo-Code is simply a numbered list of instructions to perform some task. In
this course we will enforce three standards for good pseudo code

1. Number each instruction. This is to enforce the notion of an ordered


sequence of ... operations. Furthermore we introduce a dot notation (e.g. 3.1
come after 3 but before 4) to number subordinate operations for conditional
and iterative operations
2. Each instruction should be unambiguous (that is the computing agent, in this
case the reader, is capable of carrying out the instruction) and effectively
computable (do-able).
3. Completeness. Nothing is left out.

Pseudo-code is best understood by looking at examples. Each example below


demonstrates one of the control structures used in algorithms : sequential
operations,  conditional operations, and iterative operations. We also list all
variables used at the end of the pseudo-code.
Pseudo Code Construction Statements:

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

IF HoursWorked > NormalMax THEN


Display overtime message
ELSE
Display regular time message
ENDIF
WHILE

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

WHILE Population < Limit


Compute Population as Population + Births - Deaths
ENDWHILE
Example
WHILE employee.type NOT EQUAL manager AND personCount < numEmployees
INCREMENT personCount
CALL employeeList.getPerson with personCount RETURNING employee
ENDWHILE
CASE :A CASE construct indicates a multiway branch based on conditions that are
mutually exclusive. Four keywords, CASE, OF, OTHERS, and ENDCASE, and
conditions are used to indicate the various alternatives. The general form is:
CASE expression OF
condition 1 : sequence 1
condition 2 : sequence 2
...
condition n : sequence n
OTHERS:
default sequence
ENDCASE

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

FOR each month of the year (good)


FOR month = 1 to 12 (ok)

FOR each employee in the list (good)


FOR empno = 1 to listsize (ok)
NESTED CONSTRUCTS

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

SET total to zero


REPEAT
READ Temperature
IF Temperature > Freezing THEN
    INCREMENT total
END IF
UNTIL Temperature < zero
Print total
In the above example, the IF construct is nested within the REPEAT construct, and
therefore is indented.
 INVOKING SUBPROCEDURES

Use the CALL keyword. For example:

CALL AvgAge with StudentAges


CALL Swap with CurrentItem and TargetItem
CALL Account.debit with CheckAmount
CALL getBalance RETURNING aBalance
CALL SquareRoot with orbitHeight RETURNING nominalOrbit

Pseudo-code Language Constructions : A Summary

1. Computation/Assignment 

set the value of "variable" to :"arithmetic expression" or


        "variable" equals "expression" or
        "variable" = "expression"

2. Input/Output

get "variable", "variable", ...


        display "variable", "variable", ...

3. Conditional (dot notation used for numbering subordinate statements)

if  "condition"
                    6.1    (subordinate) statement 1 
                    6.2     etc ...
            else
                    7.1    (subordinate) statement 2
                    7.2    etc ...

4. Iterative (dot notation used for numbering subordinate statements)

while "condition" 
                    9.1    (subordinate) statement 1
                    9.2    etc ...
Symbol Symbol Symbol Description
Name
(alias)

Process / Operation Symbols

Process Show a Process or action step. This is the most


common symbol in both process flowcharts and
business process maps.

Predefined A Predefined Process symbol is a marker for another


Process process step or series of process flow steps that are
(Subroutine) formally defined elsewhere. This shape commonly
depicts sub-processes (or subroutines in
programming flowcharts). If the sub-process is
considered "known" but not actually defined in a
process procedure, work instruction, or some other
process flowchart or documentation, then it is best
not to use this symbol since it implies a formally
defined process.

Alternate As the shape name suggests, this flowchart symbol


Process is used when the process flow step is an alternate to
the normal process step. Flow lines into an alternate
process flow step are typically dashed.

Delay The Delay flowchart symbol depicts any waiting


period that is part of a process. Delay shapes are
common in process mapping.

Branching and Control of Flow Symbols

Flow Line Flow line connectors show the direction that the
(Arrow, process flows.
Connector)

Terminator Terminators show the start and stop points in a


(Terminal process. When used as a Start symbol, terminators
Point, Oval) depict a trigger action that sets the process flow into
motion.

Decision Indicates a question or branch in the process flow.


Typically, a Decision  flowchart shape is used when
there are 2 options (Yes/No, No/No-Go, etc.)
Connector Flowchart: In flowcharts, this symbol is typically
(Inspection) small and is used as a Connector to show a jump
from one point in the process flow to another.
Connectors are usually labeled with capital letters (A,
B, AA) to show matching jump points. They are
handy for avoiding flow lines that cross other shapes
and flow lines. They are also handy for jumping to
and from a sub-processes defined in a separate area
than the main flowchart.
Process Mapping: In process maps, this symbol is
full sized and shows an Inspection point in the
process flow.

Off-Page Off-Page Connector shows continuation of a process


Connector flowchart onto another page. When using them in
conjunction with Connectors, it's best to differentiate
the labels, e.g. use numbers for Off-Page Connectors
and capital letters for Connectors. In actual practice,
most flowcharts just use the Connect shape for both
on-page and off-page references.

Input and Output Symbols

Data The Data flowchart shape indicates inputs to and


(I/O) outputs from a process. As such, the shape is more
often referred to as an I/O shape than a Data shape.

Document Pretty self explanatory - the Document flowchart


symbol is for a process step that produces a
document.

Multi- Same as Document, except, well, multiple


Document documents. This shape is not as commonly used as
the Document flowchart shape, even when multiple
documents are implied.

Display Indicates a process step where information is


displayed to a person (e.g., PC user, machine
operator).

Manual Input Manual Input flowchart shapes show process steps


where the operator/ user is prompted for information
that must be manually input into a system.

You might also like