2. Never tire of learning new things. Stretch your
mind. Broaden your experience.
3. Introduction
A computer is an electronic device capable of
performing commands given by human.
Program – Are sequences of instructions and
decisions that the computer carries out to
achieve a task
Programmer – person responsible in writing a
program
4. Defining Control Structures
All computer programs are written using one
or more of three basic structures:
Sequence
Repetition
Selection
These are called control structures or logic
structures because they control the flow of a
program’s logic.
5. The sequence structure
You use it each time you follow a set of
directions, in order, from beginning to end.
Example – a cookie recipe – you need to
follow each recipe instruction in order,
beginning with the first instruction and ending
the last
6. Example – a robot named Didi. Didi can understand
only a specific number of instructions / commands :
walk, turn, sit
Walk – takes one complete step forward
Turn – turns 180 degree
Sit – sits down
Assume that Didi is facing a chair that is two steps
away from him.
Write the instructions using only commands that Didi
understands, that direct Didi to sit in the chair.
7. 1. Walk
2. Walk
3. Turn
4. Sit 2 steps
algorithm
A set of step-by-step
instructions that
accomplish a task
8. The repetition structure
Example – shampoo bottles typically include
repetition structure in the directions for
washing your hair. Those direction usually tell
you to repeat the “apply shampoo to hair”,
“lather”, and “rinse” steps until your hair is
clean.
Repetition structure referred to as loop,
directs the computer to repeat one or more
instructions until some condition is met, at
which time the computer should stop
repeating the instructions.
9. Example : Didi is facing a chair that is 50
steps away from him. Write the algorithm that
directs Didi to sit in the chair.
Extra command : repeat x times
Solution:
Write “walk” instruction 50 times
Use the “repeat 50 times.”
1. repeat 50 times:
walk
2. turn
3. sit
10. The selection structure
Also called decision structure
Makes a decision, and then takes appropriate
action based on that decision
Example: you drive your car and approach an
intersection, whether you need to stop or
proceed.
11. Example
Example: Didi is holding either a red or yellow
balloon and that he is facing two boxes. One of the
box is colored yellow and the other is colored red.
The two boxes are located 20 steps away from Didi.
Your task is have Didi to drop the balloon into the
appropriate box. The yellow balloon belongs to the
yellow box and the red balloon belongs to the red
box. After Didi drops the balloon you should return
him to the original position.
You may add additional instructions to Didi’s
instruction set
12. The algorithm
1. repeat 20 times:
walk
2. if the balloon is red, do this:
drop the balloon in the red box
otherwise
drop the balloon in the yellow box
3. turn
4. repeat 20 times
walk
5. turn
13. Calculate the bonus by multiplying the salary 1%
Calculate the bonus by multiplying the salary 2%
If the years employed are greater than or equal to 5,
do this:
If the years employed are less than 5, do this:
Otherwise, do this:
Print the bonus
Read the salary and years employed
Repeat for each employee:
14. Exercise
Assume a company pays an annual bonus to
its employees. The bonus is based on the
number of years the employee has been with
the company. Employees working at the
company for less than 5 years receive a 1%
bonus, all others receive a 2% bonus. Write
an algorithm that prints each employee’s
bonus. Use only the instructions given:
18. Problem
Write a program obtain average of 3 numbers
given by a user. Display the 3 numbers and
the average of the numbers.
19. Problems Analysis
Input : 3 numbers
Process : 1. Add 3 numbers
2. Divide sum of the numbers by 3
Output : Print 3 numbers & average
20. Algorithm
Algorithm
Any computing problem can be done by executing
a series of actions in a specific order.
A procedure for solving a problem in terms of the
actions to be executed and the order in which
these actions are to be executed is called an
algorithm.
Is a logical solution that is inline with our daily
language or mother tongue language.
21. Algorithm
Start
1. Set sum = 0 and average = 0
2. Read 3 numbers: nom1, nom2, nom3
3. Add 3 numbers
4. Calculate the average, average = (sum)/3
5. Print 3 numbers(nom1, nom2, nom3) and
the average
End
22. Pseudo code
Pseudo code
Is an artificial and informal language
Usually it is use English language
Quite similar to programming language
The purpose of pseudo code is to make
humans who do not understand computer
programming can easily understand the flow
of the problem solving.
23. Pseudo code
START
SET sum = 0, average = 0
INPUT nom1, nom2, nom3
sum = nom1 + nom2 + nom3
average = sum / 3
PRINT nom1, nom2, nom3
PRINT average
END
24. Flow Chart
Flow Chart
It is represents by using geometry shapes with
connected line
Use a standard symbol
25. Flow Chart
TERMINAL
Indicates the beginning or end of an algorithm
PROCESS
Indicates an input computational or data
manipulation.
INPUT / OUTPUT
Indicates an input or output operation
26. Flow Chart
DECISION
Indicates a decision point in the algorithm
CONNECTOR
Indicates an entry to or exit from another
part of the flowchart
FLOW LINES
Used to connect the flowchart symbols and
indicate the logic flow
LOOP
Indicates the initial, final and increment
values of a loop
27. Example of a flow chart
START
END
sum = 0, average = 0
Input nom1,nom2, nom3
sum = nom1 + nom2 + nom3
average = sum / 3
print nom1,nom2, nom3
print average
28. Exercise
Write an algorithm and a flow chart to
calculate and display a volume of a sphere.
Volume = 4/3 x pi x radius x radius , where pi = 3.41
29. Solution
Analyze the problem
Input :
Process :
Output :
radius
Calculate volume of a sphere
Volume = 4/3 x pi x radius x radius
print volume of a sphere
30. Solution (algorithm)
Start
1. Input radius
2. Calculate volume of a sphere
Volume = 4/3 x pi x radius x radius
3. print volume of a sphere
End
32. Control Structures Flow Chart
3 types of control structures:
Sequence structure
Selection structure
If
If… else
Repetition structure
For
While
Do… while
33. Flow Chart for Selection
Structure 1
Pseudo code
if condition
statement 1
Condition / Boolean
operator
Statement 1 (inside if
structure)
True
False
Example Pseudo code
if student’s grade is greater than or equal to 60
Print “Passed”
34. Flow Chart for Selection
Structure 2
Condition / Boolean
operator
true
false
Statement 2 Statement 2
Pseudo code
if condition
statement 1
Else
statement 2
Example Pseudo code
if student’s grade is greater than or equal to 60
Print “Passed”
Else
Print “Failed”
35. Flow Chart for Repetition
Structure
Condition / Boolean
operator
Statement 1 (inside if
structure)
True
False
Example Pseudo code
While there are more items on the shopping list
Purchase next item and cross it off my list
36. Module Structure
Used for a big and complex problem
Divide the problem into smaller problem –
sub module
To simplify a problem solving
Module structure Pseudo code
Main module
Call sub module
End of main module
Sub module name
Statements
End of sub module
Main module
Sub module
1
Sub module
2