Professional Documents
Culture Documents
CONTROL
STRUCTURE
output.
INPUT ----------> PROCESS ----------> OUTPUT
(data) (data manipulation) (information)
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
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
Exercise:
(a)
(d)
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
Meaning
Greater than
Greater than or equal
Less than
Less than or equal
Equal to
Not equal
Meaning
AND
OR
NOT
If selection
will performs an action if a condition is TRUE or
condition
true
statement
false
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
Commission
10%
5%
3%
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 ?
false
true
5000<=Sales <10000 ?
false
true
Sales <5000 ?
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
condition
false
statement2
statement1
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
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
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
Input salary
Input age
true
age > 75?
false
new_salary = salary + salary * 0.05
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
number of 3 numbers.
Solution:
Problem Definition:
Input: 3 numbers
Output : minimum number
Algorithm:
start
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
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
Status = Fail
false
Display Status
Exercise
1.
2.
3.