An algorithm inductivity speaking is a finite step-by-step list of
well-defined instructions for solving a particular problem. The First part is a
paragraph, which tells the purpose of the algorithm identifies the variable,
which occurs in algorithm and lists the input data. The second part of the
algorithm consists of list of steps that is to be executed.
Identifying number:
Each algorithm is assigned and identifying number
Steps:
The steps of algorithm are executed one after the other, beginning with "Step 1",
unless indicated otherwise.
For Example:
Step 01: [1st instruction]
Step 02: [2nd instruction]
:
:
Step n: [End of Algorithm.]
Control:
Control may be transferred to step n of the algorithm by the statement "Go to step-n."
For example:
Step 01: [1st instruction]
Step 02: [2nd instruction]
:
Step 06: Go to step 9
Step 07: Go to step 6
:
Step 09: [9th instruction]
:
:
Step n: Exit [End of Algorithm.]
Exit:
The Algorithm is completed when Algorithm is terminated by the statement name " EXIT ."
For Example:
Step 05: Exit [End of Algorithm.]
COMMENTS :
Each step may content a comment in [brackets] which indicate the main purpose
of the step. The comment will usually appear at the beginning or the end of the
step. Some times comment will use in new line. Comment do not use in separate
step because it is use with the statement.
For example:
Step 01: Start [1st instruction]
Step 02: Set A:= 2 [Initialize the variable]
:
Step 06: Go to step 9 [Jump the control from step 6 to step 9]
Step 07: Go to step 6
:
Step 09: [9th instruction]
:
:
Step n: Exit [End of Algorithm.]
Variable Names:
Variable name will be use capital letters, as like: A, B, C, DATA, COUNTER.
For Example:
Step 01: Set A := 0 [Initialise the variable]
Step 02: Set D [COUNTER] := DATA [A]
Assignments:
Our assignment statements will use the colon with equal notation " := " that is also use in Pascal.
For example:
Step 01: Start [1st instruction]
Step 02: Set A:= A+1 [Increment Counter]
Step 03: Set B:=DATA[A]
:
Step n: Exit [End of Algorithm.]
Input :
Data may be input and assigned to variables by means of "Read: " statement with the following form:
Read: Variables name.
For example:
Step 01: Start
Step 02: Read: A
Step 03: Read: B, C, E.
Step 04: Read: DATA [2]
Step 05: Read: DATA [K], A, K [A]
:
Step n: Exit [End of Algorithm.]
Write:
Data in variables may be output by means of a "WRITE:" or "PRINT:" statement with the following form.
Write: Messages and / or Variables name.
For example:
Step 01: Start
Step 02: Write: "MY NAME IS ABDUL HAFEEZ"
Step 03: Write: "Your Name:" , VN.
Step 04: Write: "Your Roll Number", VR, "Your Class", VC
Step 05: Write: DATA [K], A, K [A]
:
Step n: Exit [End of Algorithm.]
Procedures:
The term "Procedure" will be used for and independent Algorithmic module, which solves a particular problem.
The use of word "Procedure" or "Module" rather than "Algorithm" for a given problem is simply a matter a taste.
Generally speaking, the word "Algorithm" will be reserve for the solution of general problem.
The term "Procedure" will also be use to describe a certain type of sub algorithm.
For example:
Step 01: Start
Step 02: Module A
Step 03: Set DATA := Module B
Step 04: Set K := Module C
:
Step n: Exit [End of Algorithm.]
Module A
Step 01: Start
:
Step n: Return.
Module B
Step 01: Start
:
Step n: Return DATA
Module C
Step 01: Start
:
Step n: Return 23
Control Structures:
1- Sequence logic, or Sequential flow
2- Selection logic, or conditional flow
3- Iteration logic, or repetitive flow
Sequence logic (Sequential flow):
The steps of algorithm are executed one after the other, beginning with "Step 1", unless indicated otherwise.
The sequence may be presented explicitly, by means of numbered steps, or implicitly, by the order in which the module are written.
For Example:
Step 01: Set A := 3
Step 02: Module A
Step 03: Set A := Module B
:
Step n: Exit [End of Algorithm.]
Selection logic (Conditional flow):
Selection logic employs a number of conditions, which lead to a selection of one out of several alternative modules.
The structures, which implemented this logic are called conditional structures or If structures.
For clarity, we will frequently indicate the end of such a structure by the statement like "[End of If structure]" or some equivalent.
These conditional structures fall into three types, which are discussed separately.
i) Single Alternative:
This structure has the form
If condition, then:
[Statements]
[End of If structure]
ii) Double alternative:
This structure has the form
If condition, then:
[Statements]
Else:
[Statements]
[End of If structure]
iii) Multiple alternative:
This structure has the form
If condition, then:
[Statements]
Else if condition, then:
[Module 1]
Else if condition, then:
[Module 2]
:
:
Else if condition, then:
[Module n]
Else:
[Statements]
[End of If structure]
For Example:
Step01: Start
Step02: Set DATA := Module B
Step03: Set K := Module C
Step04: If K = 3, then:
K := 0
[End of If structure]
Step05: If K = 2, then:
K := 0
Else:
K := 3
[End of If structure]
Step06: If K = 3, then:
K := 0
Else if K < 3, then:
K := 5
Else:
K := 3
[End of If structure]
:
:
Step n: Exit [End of Algorithm.]
Iterative Logic (Repetitive Flow):
This kind of logic refers to either of two types of structures involving loops.
Each types begins with a Repeat statement and is followed by a module, called the body of the loop.
For clarity, we will indicate the end of the structure by the statement " [End of loop]" or some equivalent.
Each type of loop structure is discussed separately.
The repeat for loop uses an index variable, such as K, to control the loop.
The loop will usually have the form:
Repeat Stepsfor VAR = INITIALIZE to COND by INC:
[Module A]
[End of loop]
Here INITIALIZE is called the initial value, COND the end of value or test
value or condition check value and INC the increment value.
Similarly VAR is use for Variable.
Another Type of loop, the repeat-while loop uses a condition to control the
loop. The loop will usually have the form
Repeat Stepswhile condition:
[Module A]
[End of loop]
For Example:
Step 01: Start
Step 02: Set DATA := Module B
Step 03: Set K := Module C
Step 04: Repeat for A = 0 to 10 by 1:
Write: A
[End of loop]
Step 05: Repeat for A = 10 to 0 by -1: [Decrement loop]
Write: A
[End of Step 5 loop]
Step 06: Repeat Step 7 to 10 while K<5:
Step 07: Write: A
Step 08: Module B
Step 09: Write: C
Step 10: Write: D
[End of Step 6 loop]
Step 11: Repeat Step 12 to 15 for A = 0 to 10 by 1:
Step 12: Write: A
Step 13: Module B
Step 14: Write: C
Step 15: Write: D
[End of Step 11 loop]
:
:
Step n: Exit [End of Algorithm.]