ALGORITHMS
ALGORITHMS
- A set of instructions describing the steps followed in performing a specific task, for example,
calculating change.
- They are a sequence of instructions for solving a problem.
- Algorithms can be illustrated using the following:
Descriptions, Flowcharts, Pseudocodes, Structure diagrams
a. Descriptions:
- These are general statements that are followed in order to complete a specific task.
- They are not governed by any programming language. An example is as follows:
Enter temperature in oC
Store the value in box C
Calculate the equivalent temperature in oF
Store the value in box F
Print the value of box C and F
End the program.
b. Pseudocodes:
- These are English-like statements, closer to programming language that indicates steps
followed in performing a specific task.
- They are means of expressing algorithms without worrying about the syntax of the
programming language.
- There are no strict rules on how pseudocode statements should be written.
- Indentations are very important in writing pseudocodes since they clearly indicate the extent
of loops and conditional statements.
- They are however independent of any programming language.
- An example is as follows:
Enter centigrade temperature, C
If C = 0, then stop.
Set F to 32 + (9C/5)
Print C and F
End
A B C
The above 3 Pseudocodes produces the same result.
Cascaded/Nested If Statements
This is whereby if statements are found inside other if statements (nested Ifs) as shown below:
Start
Enter “First Number”, A
Enter “Second Number”, B
Enter “Third Number”, C
If A>B Then
If B>C Then
Print “A is the biggest Number”
End If
End If
End.
CASE Statement: This is an alternative to the IF...THEN...ELSE statement and is shorter. For
example:
Enter first Number, A
Enter second number, B
Enter operand (+, -, * /)
CASE operand of:
“+”: C = A + B
“-”: C = A-B
“*”: C = A*B
“/”: C = A/B
ENDCASE
Print C
END
iii. Repetition/Iteration/looping:
A control structure that repeatedly executes part of a program or the whole program until a
certain condition is satisfied.
Iteration is in the following forms: FOR...NEXT LOOP, REPEAT... UNTIL Loop and the
WHILE...ENDWHILE Loop.
a. For...Next Loop: A looping structure that repeatedly executes the loop body for a specified
number of times. The syntax of the For...Next loop is as follows:
A group of statements between the looping structures is called the loop body and is the one
that is repeatedly executed.
The For...Next loop is appropriate when the number of repetitions is known well in advance,
e.g. five times. An example of a program that uses the For...Next loop is as follows:
Sum, Average = 0
FOR I = 1 to 5 DO
Enter Number
Sum = Sum + number
NEXT I
Average = Sum/5
Display Sum, Average
End
b. Repeat...Until Structure: This is a looping structure that repeatedly executes the loop body
when the condition set is FALSE until it becomes TRUE. The number of repetitions may not
be known in advance and the loop body is executed at least once. The syntax is as follows:
Repeat
Statement 1
Statement 2 loop body
................
Until {Condition}
For example
Sum, Average, Count = 0
Repeat
Enter Number (999 to end)
Sum = Sum + Number
Count = count + 1
Until Number = 999
Average = Sum / count
Print Sum, count, Average
End
In the above program:
- Count records the number of times the loop body executes.
- 999 is used to stop further data entry through the keyboard and thereby ending the loop.
Such a value that stops further data entry through the keyboard thereby terminating a
loop is called a Rogue value or sentinel.
- The condition here is {Number = 999}. The loop exits when the number 999 is entered.
If 999 is part of the number to be entered in this program, then the user has to split it
into two numbers, that is 999 = 990 + 9, therefore can be entered separately as 990 and
9.
- A flag is also used to control the loop. In this case 999 is also a flag.
NB. As for the Repeat...Until loop, the condition is tested after the loop body has been run at
least once, even when the condition is true from start. This is rather misleading.
WHILE {condition}
Statement 1
Statement 2 loop body
................
ENDWHILE