Algorithm
Algorithm
Algorithm
OBJECTIVES
By the end of this topic learners should be able to:
1) Explain selection and repetition constructs
2) apply selection and repetition algorithm structures in problem solving
Algorithm
- An algorithm is a sequence of steps followed in order to perform a task /to solve a problem
Algorithm tools
Pseudocode
Pseudocode structure
1. Sequence construct
2. Selection construct
3. Iteration construct
Selection construct
- To solve a problem using a selection construct, there is need to control the flow of a code
instructions.
- This can be done using what are known as selection control structures.
- The key words used are:
1. If . . . Then. . .Endif
2. If . . . Then. . .Else . . . Endif
3. Case . . . of . . . Otherwise . . . End Case
- This control structure is used when there is only one action to be taken if the condition is
true.
- A condition normally can be an expression e.g. a>b
Syntax
If <condition> Then
Statement(s)
Endif
Example 2.1
Age=18
If Age>17 Then
Endif
- This control structure is used when there are two or more actions to be taken.
- The Else part of the construct is executed if the Boolean condition is false
- A Boolean situation consists of two states, either true or false.
Syntax
If <condition> Then
Statement(s)
Else
Statement(s)
Endif
Example 2.2
Age=18
Else
Display “Too young to register to vote”
Endif
- Case of is used when there are multiple options from which to choose.
Syntax
CASE<expression> OF
<expression>
<statement1>
<expression2
<statement>
OTHERWISE
Statement
END CASE
Example 2.3
Case grade of
0 to 39
Display “Fail”
40 to 44
Display “Grade E”
45 to 49
Display “Grade D”
50 to 59
Display “Grade C”
60 to 69
Display “Grade B”
70 to 100
Display “Grade A”
End Case
REPETITION structure
- The repetition structure is also known as loop or iteration structure.
- This structure is used when there is need to repeat performing the same task or instruction
over several times.
Repetition constructs
1. REPEAT ….UNTIL
2. WHILE . . . ENDWHILE
3. FOR . . . ENDFOR
REPEAT. . . UNTIL
- The sequence of instructions inside the loop is always performed at least once.
- This is because the test is performed after the sequence of instructions has been carried out.
- Each time the instructions inside the loop are performed, the condition is tested, if the
condition is false the loop repeats. If the condition is true the loop ends.
REPEAT
Statement(s)
UNTIL <condition>=true
Example 2.4
- A pseudocode that adds up a series of numbers until the total exceeds 120
Total = 0
REPEAT
Read Number
PRINT Total
- Remember to identify the variables and then make them column headings for the Trace
Table
- Our variables are : Total, Number
- Use the following test data 12,34,67,-1,25
-
- The instructions or statements inside the loop will be repeated if the condition is true.
- The loop may or may not execute those instruction since the condition is tested at the start
of the loop.
- This structure is used when one does not know how many times the steps inside a loop need
to be performed.
SYNTAX
WHILE <condition> DO
Statement(s)
ENDWHILE
Example 2.5
- Using the pseudo code in example 2.4 which adds up a series of numbers until the total
exceeds 120.
Total = 0
ENDWHILE
Print Total
- It is used when you want to perform the sequence of instructions in the loop a set number
of times.
- This type of a loop is sometimes called the counting loop because it counts how many times
the instructions inside the loop have been carried out.
SYNTAX
Statement(s)
ENDFOR
Example 2.6
Pseudocode
Sum = 0
FOR I. = 1 to 10
Read Number
ENDFOR
Print Sum
Example 2.7
- A routine is required where you input 60 numbers and the number of negative numbers are
counted and then the result is output.
Negnum=0
FOR a = 1 to 60
READ number
If number < 0 then
Negnum=negnum + 1
Endif
ENDFOR
Output Negnum
Activity
1) Write a pseudocode to prepare a meal of your choice.
2) What is the purpose of the following algorithm, which is part of a program to be used in
the shop’s computer system?
Input X
Input Y
If X< Y then
Temp=X
X=Y
Y=Temp
Endif
Output X
Output Y