You are on page 1of 39

TOPIC 5

CONTROL
STRUCTURE

Sequential Control Structure


Normally each activity involve 3 phases:- input, process,

output.
INPUT ----------> PROCESS ----------> OUTPUT
(data) (data manipulation) (information)

But we also have activities that does not require input


Sequential structure has a series of instructions that will

executed one by one follows its sequence


The most important in sequential structure is
arrangement / sequence of instructions that form the
algorithm

In calculating the total of 3 numbers, statements involve

are input statement, output statement and assignment


statement. To solve this problem, we arrange the
instructions as follows:
start
display Enter three numbers :
input no1, no2, no3
total no1 + no2 + no3
output The total of three numbers is , total
end

It is also can be written using number as follows:

1. start
2. display Enter three numbers :
3. input no1, no2, no3
4. total no1 + no2 + no3
5. output The total of three numbers is , total
6. end
In example of calculating an area of a circle, there are two

assignment statements:

(i) PI 3.142
(ii) area PI * radius * radius
At (ii), content of variable area depend on the result of

arithmetic operation (content of variables PI and radius)


Means, statement (i) must written before statement (ii)

So, the algorithm for this problem:

1. start
2. output Enter a radius of a circle
3. input radius
4. PI 3.142
5. area PI * radius * radius
6. output The area of a circle is , area
7. end
Example : Calculate area of two circles
1. start
2. output Enter radius for two circles
3. input radius1, radius2
4. PI 3.142
5. area1 PI * radius1 * radius1
6. area2 PI * radius2 * radius2
7. output The area of first circle is , area1
8. output The area of second circle is , area2
9. end

Look at the statement line 5 and 6. The calculation of area1

and area2 are undependable on each other. So, NOT an error if


we change the position of statement 5 and 6 as follows:
1. start
2. output Enter radius for two circles
3. input radius1, radius2
4. PI 3.142
5. area2 PI * radius2 * radius2
6. area1 PI * radius1 * radius1
7. output The area of first circle is , area1
8. output The area of second circle is , area2
9. end
But statement 4 must written before calculating the area1 and

area2 because these values depend on variable PI

Exercise:
(a)

Calculate the circumference and area of a circle.

(b) You will be given a number. Identify the result and


remainder when that number divided with 3
(c) Identify the length between 2 coordinate point X
(x1,y1) and Y (x2, y2). The length can be calculated
using the following formula:

(d)

Calculate the total cumulative investment at the end of


the first year when an investor invest some money at
early of a year. The interest rate is 14.5% per year.
Total cumulative is total investment added with
interest received by the investor.

Selection Control Structure


solutions that we can choose based on the true

condition
also known as a branch
A condition is when control flow reaches a point
where it can proceed to one of two (or more)
alternatives
The condition is what determines which step to
proceed to

When your algorithm involve conditional, we

must familiar first with the operator


two operators involved which are relational
operator and logical operator
Relational Operator - is the relationship which
values can have with one another.
Operator
>
>=
<
<=
==
!=

Meaning
Greater than
Greater than or equal
Less than
Less than or equal
Equal to
Not equal

Logical Operator - is the way that the


relationships can be connected.
Operator
&&
||
!

Meaning
AND
OR
NOT

Statements are executed based on the


conditions. There are three types of selection:
1. if selection
2. if/else selection
3. multiway selection (nested if)

If selection
will performs an action if a condition is TRUE or

skip if the condition is FALSE.


Syntax:
if <condition>
statement

condition

true
statement

false

Example 1: Display the statement MALE if the

gender is male.

Solution:

Problem Definition:
Input: gender
Output: Display MALE if gender is male
Algorithm:
start
Input gender
if (gender = = male) then
display MALE
end

Input gender
true
gender ==male?

false

Display MALE

Example 2: A direct selling company gives the

commission to their employees regarding on


sales have been done.
Sales
>= 10000
>= 5000 and < 10000
< 5000

Commission
10%
5%
3%

Write an algorithm to calculate the commission for


the one employee.

Solution:

Problem Definition:
Input: Sales
Output: commission
Formula: Commission = Sales * 0.1 if Sales > = 10000
Commission = Sales * 0.05 if 5000 <= Sales <10000
Commission = Sales * 0.03 if Sales < 5000

Algorithm:
start
output Enter sales : RM
input Sales
If Sales > = 10000 then
Commission Sales * 0.1
If 5000 <= Sales <10000
Commission Sales * 0.05
If Sales < 5000
Commission Sales * 0.03
output The commission is RM, Commission
end

Input Sales
true
Sales >=10000 ?

Commission = Sales * 0.1

false
true
5000<=Sales <10000 ?

Commission = Sales * 0.05

false
true
Sales <5000 ?

Commission = Sales * 0.03

false
Output The commission is RM , Commission

If/else Selection
will perform an action from two conditions
Statement1 will be executed if the condition is

TRUE or Statement2 will be executed if the


condition is FALSE.
Syntax:
true
if <condition>
statement1
else
statement2

condition
false
statement2

statement1

Example 1: Display the statement MALE if the

gender is male, statement FEMALE if the


gender is female.
Solution:
Problem Definition:
Input: gender
Output: Display MALE if gender is male
or FEMALE if gender is female

Algorithm:

start
display Enter gender M or F
display Enter gender M or F
input gender
if (gender = = M) then
Input gender
display MALE
true
else
gender ==M?
display MALE
display FEMALE
false
endIf
display FEMALE
end

Example 2: Write an algorithm in which the user

enters their salary. This algorithm is to calculate


and display the amount of tax payable on this
salary. Use the simple tax rule: Salaries under
RM4000 pay 2% tax, salaries of RM4000 or
more pay 6.5% tax.
Solution:
Problem Definition:
Input: salary
Output: tax
Formula:
tax = salary * 0.02 if salary <4000
tax = salary * 0.65 if salary >=4000

Algorithm:
start
display Enter salary : RM
Display Enter salary : RM
input salary
If salary < 4000
tax salary * 0.02 Input salary
true
else
salary < 4000?
tax salary * 0.065
false
endIf
display Tax is : , tax tax=salary * 0.065
end
Display Tax is : , tax

tax=salary * 0.02

Example 4: Write an algorithm in which the user

enters their salary and their age. If the person is


over 75, increase their salary by 10% of their
salary. Otherwise, increase their salary by 5% of
their salary. Display the new salary in either case.
Solution:
Problem Definition:
Input: salary, age
Output: new salary
Formula : new salary = salary + (salary * 0.10) if age > 75
new salary = salary + (salary* 0.05) if age < = 75

Algorithm:
start
output Enter salary and age
input salary, age
If age > 75
new_salary salary + ( salary * 0.10)
else
new_salary salary + (salary * 0.05)
endIf
output New salary is RM , new_salary
end

Display Enter salary and age

Input salary

Input age
true
age > 75?

new_salary = salary + salary * 0.1

false
new_salary = salary + salary * 0.05

display New salary is RM , new_salary

Multiway selection (Nested if)


Nested if is the if statement inside other if

statements or else statements, and so on.


Example 1: Display the status of a student based
on their mark below:
Mark
>= 75
50 <= mark < 75
< 50
Fail

Status
Excellent
Moderate

Solution:
Algorithm:
start
Input mark
If mark < 50
Status fail
Else
If mark < 75
Status Moderate
Else
Status Excellent
endIf
Output Status
end

Input mark
true
mark < 50
false

Status = Fail

true
mark < 75

false

Status = Excellent

display Status

Status = Moderate

Example 2: Find and display the minimum

number of 3 numbers.
Solution:
Problem Definition:
Input: 3 numbers
Output : minimum number

Algorithm:
start

Input no1, no2, no3


If (no1 < no2) and (no1 < no3)
output no1 block 1
else
if (no2<no1) and (no2 < no3)
output no2
else
block2
output no3
endif
end
Note: We can see the ifelse statement in the second block.

Example 3: Find the grade of a student. The

range of input is 0 100.


Mark
0 49
50 64
65 79
80 100

Solution:

Problem Definition:
Input : mark
Output: grade

Grade
F
C
B
A

Algorithm:
start

Input mark
If mark < 50
Grade F
Else
If mark < 65
Grade C
Else
If mark < 80
Grade B
Else
Grade A
endIf
endIf
endIf
Output Grade
end

Input mark
true
mark < 50
false

Grade = F

true
Grade = C

mark < 65
false

true
mark < 80

false
Grade = A

display Status

Grade = B

Case structure
used as other alternative of Nested IF
Syntax:

case variable/expression
(data1/ data range1) : statement1
(data2/ data range2) :statement2
(data3/ data range3) :statement3
.
End Case

expression
true
statement1

data1
false

true
data2

false

statement2
true

data3

statement3

false
true
dataN

statementN

false
default

defaultStatement

Example: Find the status of a student. The range of status

is in the table below:


Grade
A+, A, AB+, B, BC+, C
C-, D+, D, E, F

Status
Excellent
Good
Pass
Fail

Solution:

Problem Definition:
Input: grade
Output: status

Algorithm:
start
Input grade
case grade
A+,A,A- : Status Excellent
B+,B,B- : Status Good
C+,C
: Status Pass
C-,D+,D,E,F : Status Fail
endCase
Output Status
end

Input grade

grade
true
A+,A,Afalse

Status= Excellent
true

B+, B, Bfalse

Status = Good

true
C+, C

Status = Pass

C-, D+, D, E,F

Status = Fail

false

Display Status

Exercise
1.

2.

3.

ABC Company has decided to increase the staff salary


and attendance allowance by 20% and 10%
respectively if the salary is less than RM1000. Write an
algorithm to solve that problem.
AreCS magazines cost RM5 each if at least 5 units of
magazines are purchased, and RM7 each otherwise.
Write an algorithm to calculate the price a customer
has to pay after he/she enters in the quantity of the
magazines purchased.
Write an algorithm which will input three(3) numbers
and display the sum, average, the largest and lowest
value.

You might also like