Chapter 3- Algorithms Control Structures FINAL
Chapter 3- Algorithms Control Structures FINAL
3.1. Introduction
Algorithm control structures also known as program control structures or program constructs are
blocks of statements that determine how statement are to be executed. There are three control
structures namely:
Sequence
Selection
Iteration( loops)
3.2. Sequence
Instruction 1
Begin
Instruction1 Instruction 2
Instruction 2
Instruction 3
Instruction 3
Instruction n
Instruction n
Stop
Example of sequence: the pseudo code below add two numbers ( number1 and 2) and display
their sum.
Begin
INPUT number1
INPUT number2
Sum = number1+number2
PRINT sum
STOP
Page | 1
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
3.2.1. Incrementation
3.3. Selection/decision
This control structure is used to branch whether a condition return a value True or False
There are four types of selection control structures used in programs namely:
IF ……..THEN…….ENDIF
IF……...THEN…….ELSE……..ENDIF
Nested IF
Case Selection
3.4. IF…..THEN……ENDIF
This control structure is used if Only one Option is available. Others options are ignored .
Page | 2
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
IF ( Condition) THEN
FALSE
Statements
Condition
ENDIF
TRUE
Statements
IF ( Condition) THEN
Statement 1 FALSE
ELSE
Condition
Statement 2
Statement 2
ENDIF TRUE
Statement 1
Page | 3
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Example: In Football match a player is given a Red Card if he does a serious fault/mistake ,
otherwise he is given a Yellow Card.
ENDIF
3.4.2. Nested IF
This selection control structure is used where two or more options have to be considered to make
a decision. The general format of Nested If is as follow
Pseudocode version Flowchart version
IF ( Condition1) THEN
Statement 1
ELSE IF
Statement 2
ELSE IF ( condition2)
THEN
Statement 3
ELSE ( condition n)
THEN
Statement n
ENDIF
ENDIF
ENDIF
Page | 4
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Note: Each IF statement and ELSE IF statement must have an ENDIF statement to terminate.
Example 16: Write pseudo code that will take marks in an exam as input and will tell the
grade A for 80 to 100, B for 70 to 79, C for 60 to 69 and F grade for 0 to 59.
Solution
WRITE “Enter Marks”
READ marks
IF marks >= 80 AND marks <= 100
THEN
WRITE “The Grade is A”
ELSE IF marks >= 70 AND marks < 80
THEN
WRITE “The Grade is B”
ELSE IF marks >= 60 AND marks < 70
THEN
WRITE “The Grade is C”
ELSE IF marks >= 0 AND marks < 60
THEN
WRITE “The Grade is F”
ELSE
WRITE “Invalid Marks”
ENDIF
ENDIF
ENDIF
ENDIF
Problem statement:
We want to Print a rectangle of stars, on 5 lines and each line having 5 stars. The pseudo code A
can be used to achieve this.
Another way to achieve this is by using a FOR loop which requires fewer instructions (Pseudo
code B) the statement In The FOR loop “ PRINT “ ***** ” is repeated 5 times.
Page | 5
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Limitation of Pseudo code A: If we want to Display a pattern of 50 lines of stars, the Pseudo
Code A is not appropriate since we need to type in 50 instructions that will occupies a lot of
space and the program will be too long. Therefore the Program B is suitable for this task.
Iteration also known as repetion or loop. This control structure is used when a statement or a
block of statements is to executed many times based on given condition. Three categories of
loop exist.
FOR……….TO………..ENDFOR
WHILE………DO……..ENDWHILE
REPEAT………..UNTIL
3.5.1. FOR…….TO……ENDFOR.
This repetition statement is used when we know how many times an instruction or set of
instructions is to be repeated.
Count = Lower Value
Incrementation of Count
Page | 6
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Example: 4: Write pseudo code that will take 10 numbers as input and print their average by
using FOR…TO…NEXT loop. Dry run for num = 10, 20, 30, 25, 5,15,2,8,45, 40.
3.5.2. WHILE…….DO……ENDWHILE
This repetition statement is used when we don’t know how many times an instruction or set of
instructions is to be repeated.
Page | 7
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
WHILE ( Condition)
DO FALSE
Statements Condition
ENDWHILE.
TRUE
Repeated Statements
The Statements in the loop are repeated as long as the condition is true and halted when
the condition is false.
Condition is tested at the beginning of the loop.
The statements/instruction between DO and ENDWHILE keywords are repeated.
Example 7: Write pseudocode or draw a flowchart that will take a Password as Input and
compared it with the one stored in the computer that is “ call@237” if the password matched
then the access is granted otherwise the access is denied.
Solution
BEGIN
INPUT “ Type in your Password”
READ pswd
WHILE ( Pswd Not “ call@237”)
DO
PRINT “ password incorrect, ACCESS DENIED”
Statements to be
INPUT “ Enter a Correct Password”
repeated
READ Pswd
ENDWHILE
PRINT “ ACCESS GRANTED”
END
Page | 8
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
It is a repetition statement that is used when we don’t know how many times an instruction or
set of instructions is to be repeated. It is different from WHILE… DO … ENDWHILE because
this loop will be repeated so long as a condition is false and stop when the condition is True.
The general format
REPEAT
Statements
Statements
UNTIL ( Condition)
FALSE
Condition
TRUE
Note : The condition is tested at the end of the loop and even if a condition is true the
loop will execute at least once.
Example 13: Write the pseudo code that will take numbers as input, add and gives the
total as output. The loop will continue until “0” is given as input.
Solution
BEGIN
Total = 0
REPEAT
WRITE “Enter a number to add”
READ num
Total = Total + num
UNTIL num is “0”
Write Total
END
Page | 9
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Part 1 : Selections
Exercise 1: the pseudo code compute the Profit and loss of items sold.
START
1. WRITE “Enter the cost price"
2. READ CP
3. WRITE "Enter the selling price"
4. READ SP
5. IF SP>CP THEN
6. PROFIT = SP-CP
7. WRITE "The profit is", PROFIT
8. ELSE
9. LOSS = CP-SP
10. WRITE "The loss is" , LOSS
11. ENDIF
END
1. Dry run the pseudo code of Exercise1 with the values below of inputs; and then write the
output for each case(case a and b)
a. CP = 5000, SP = 7000
b. CP = 8000 SP = 7500
2. List the variables used in this pseudo code.
3. Convert the pseudo code into a flowchart
4. What is the name of the program construct depicted from line 5 to 11.
Exercise 2: Write pseudocode that will take a number as input and tells whether a number is
positive, negative or zero.
Exercise 3: Write down pseudocode that will take three numbers as input and tells
(a) The Largest Number
(b) The Smallest Number
Exercise 4: A store charges $12 per item if you buy less than 10 items. If you buy between 10
and 99 items, the cost is $10 per item. If you buy 100 or more items, the cost is $7 per item.
Write a program that asks the user how many items they are buying and prints the total cost.
Page | 10
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
BEGIN
PRINT “ Enter the number of items bought”
READ num
IF num < 10 THEN
TotalCcost = num*12
ELSEIF num >=10 AND num <=99 THEN
TotalCost = num*10
ELSE
TotalCcost = num*7
ENDIF
ENDIF
PRINT TotalCcost
END
1. What is the value of the output when the user enters the numbers below as inputs:
i) Num = 70 ii) Num = 250 iii) 5
2. Convert the pseudo code of exercise 4 into a flowchart.
Part 2 : Sequence
Exercise 5. Consider the pseudo code below. Write down the values of Cost, Price and
SellingPrice,
SET Cost to 10
SET Price to COST times 2
Tax = price * 0.12
SellingPrice Price + Tax
Exercise 6: Write down a program in pseudo code that gets radius and central angle as user
inputs and computes the area of a sector. Given that the area of a sector is:
Area of a Sector = (π * radius * radius * central angle)/360
Exercise 7: Write a program that asks the user for a weight in kilograms and converts it to
gram. There are 1000 grams in a kilogram.
Part 2 : LOOPS
Exercise 8: Write down a pseudo code to display on the screen a rectangle of star as shown
below: Allow the user to specify the height of the rectangle.
*******************
*******************
*******************
*******************
Page | 11
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Exercise 9: Write a program that asks the user for their name and how many times to print it.
The program should print out the user’s name the specified number of times.
Exercise 10: A geography class decided to measure daily temperature per day over a week
period (7 days). Write pseudo code that inputs the temperature for all 7 days and give
output as average temperature for the week.
BEGIN
SET Total_temp to 0
FOR Count = 1 to 7
WRITE “Please enter temperature”
READ temp
Total_temp =Total_temp + temp
ENDFOR
Avgtemp = Total_temp/7
WRITE “Average temperature for whole year is:”, Avgtemp
END.
1. Use the table below to dry run the code.
Count Temp Total_temp Avgtemp Output
1 20 0
21
21
23
22
25
24
Exercise 11: The pseudo code below Prints the Multiples of 5 from zero to a limit entered by
a user.
BEGIN
INPUT m.
Sum = 0
Count = 0
WHILE (Count <= m )
PRINT count
Count = count +5
ENDWHILE
END
2. Write down the Output for the following values of m:
a. 25
b. 50
3. Convert the pseudo code into a flowchart
Page | 12
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
Solution of exercise 2:
WRITE “Enter a number”
READ num
IF num> 0
THEN
WRITE “The number is positive”
ELSE IF num = 0
THEN
WRITE “The number is zero”
ELSE
WRITE “The number is negative”
ENDIF
ENDIF
Solution of exercise 3:
WRITE “Enter three numbers”
READ num1
READ num2
READ num3
IF num1 > num2 AND num1 > num3
THEN
WRITE “The largest Number is”, num1
ELSE IF num2 > num1 AND num2 > num3
THEN
WRITE “The largest Number is”, num2
ELSE
WRITE “The largest Number is”, num3
ENDIF
ENDIF
Page | 13
TOPIC: ALGORITHMS [ ALGORITHM CONTROL STRUCTURES ]
IF ( Condition1) THEN
Statement 1 TRUE
Condition 1 Statement 1
ELSE IF
Statement 2
FALSE
ELSE IF ( condition2)
THEN TRUE
Statement 3 Statement 2
Condition 2
ELSE ( condition n)
FALSE
THEN
Statement n
Statement 3
ENDIF Condition 3
TRUE
ENDIF
ENDIF
FALSE
Statement n
Condition n
TRUE
FALSE
Page | 14