Flowchart
for
Control Structure
By Dr Goh Wan Inn
Control Structures
• Describe the flow of execution
• Basic types of control structure:
1. Sequential
2. Selection
3. Repetition
Sequential Structure
• A series of steps or statements that are executed in the
order they are written in an algorithm.
• Pseudo code - Mark the beginning & end of a block of
statements.
1. Start
2. Statement_1
3. Statement_2
4. Statement_3
n. Statement_n+1
N+1.End
Sequential Structure – flow chart
• Multiple statements considered as one
statement
Statement simply means
command or instruction
statement
statement
statement
Sequential Structure -
Start
trace
Read
Length,
Input:
Width Length <- 5
Width <- 3
Calculate Area Process:
Area=Length * Width Area = 5 * 3 = 15
Calculate Perimeter Process:
Perimeter= Perimeter =
2 * (Width+Length) 2* (5+3) = 16
Output
Area: 15
Print
Area, Perimeter: 16
Perimeter
Stop
Selection Structure
Selection allows you to choose between two or more
alternatives; that is it allows you to make decision.
Decisions made by a computer must be very simple
since everything in the computer ultimately reduces to
either true (1) or false (0).
If complex decisions are required, it is the
programmer’s job to reduce them to a series of simple
decisions that the computer can handle.
Selection Structure
– Problem Examples
Problem 1: Determine whether profit, return capital or loss.
Problem 2: Determine whether a number is even or odd.
Problem 3: Determine whether the marks is less than 60%. If it is less
than 60, then print “fail”, otherwise print “pass”.
Problem 4: Determine whether the speed limit exceeds 110 km per
hour. If the speed exceeds 110, then fine = 300, otherwise fine = 0.
Display fine.
Problem 5: Determine whether the age is above 12 years old. If the age
is above 12, then ticket = 20, otherwise ticket = 10. Display ticket.
Selection Structure(cont..)
• Pseudo code – requires the use of the
keywords if.
Algorithm: one choice selection
:
n. if condition
n.1 statement
n+1. end_if
:
Selection Structure
(cont..)
If
“do or don’t”
(one-choice)
TRUE
condition
condition
FALSE
statement
statement
°
If set condition is true, execute the
statement, else do nothing
Selection Structure (cont..)
• Pseudo code – requires the use of the
keywords if and else.
Algorithm: two choices selection
:
n. if condition
n.1 statement
:
n+1. else
n+1.1 statement
:
n+2. end_if
:
Selection Structure (cont..)
If-else
(two-choices) “do this or do that”
TRUE FALSE
condition
Statement 1 Statement 2 statement
If set condition is true, execute the first
statement, else execute second statement
Selection Structure (cont..)
• Pseudo code – nested if.
Algorithm: nested if
:
n. if condition
:
n.m if condition
n.m.1 statement
:
n+1. end_if
:
Selection Structure (cont..)
Nested if
(if within if)
FALSE
test1 FALSE
test1
TRUE
TRUE
FALSE
TRUE
test2 °
°
statement
Considered as °
one statement
°
it is an “one-choice” if
Selection Structure (cont..)
• Pseudo code – nested if using if-else if.
Algorithm: if-else if
:
n. if condition
n.m if condition
n.m.1 statement
:
n+1 else
n+1.m.1 statement
:
n+2. end_if
:
Selection Structure (cont..)
Complex if-else & if Statements
x
FALSE
condition
TRUE
statement
statement TRUE
condition
FALSE
statement
°
°
Considered as one statement
Selection Structure - trace
Start
Enter a Number >> 10
Category A
Input:
Read Num
Num <- 10
Num = 10
10 > 0 ? => YES
Num>0? No
Print
"Category B"
Yes
Print
Output:
"Category A"
“Category A” What is the Output of the
following flowchart when the input
Num= 10
Stop
Selection Structure – trace (cont..)
Start
Enter a Number >> 0
Category B
Read Num Input: Category A
Num <- 0
Num = 0 Output:
0 > 0 ? => NO “Category B”
Num>0? No
Print
"Category B"
Yes
Print
Output:
"Category A"
“Category A” What is the Output of the
following flowchart when the
input is Num= 0
Stop
Repetition Structure
• Specifies a block of one or more statements
that are repeatedly executed until a condition
is satisfied.
• Usually the loop has two important parts:
1. An expression that is tested for a
true/false,
2. A statement or block that is repeated as
long as the expression is true
Repetition Structure (cont..)
• Pseudo code – requires the use of the
keywords while for pre-test loop.
Algorithm: one choice selection
:
n. While condition
n.1 statement
:
n+1. end_while
:
Repetition Structure (cont..)
while Loop
(pre-test loop)
°
FALSE
condition
condition
TRUE
statement
body of loop
While a set condition is true, repeat statement (body of loop)
Repetition Structure (cont..)
• Pseudo code – requires the use of the
keywords repeat..until for post-
test loop.
Algorithm: one choice selection
:
n. Repeat
n.1 statement
:
n+1. until condition
:
Repetition Structure - Counters
• Counter: Can be used to control execution of the
loop (loop control variable)
• It will increment or decrement each time a loop
repeats
• Must be initialized before entering loop
Repetition Structure (cont..)
x
Start
initialization
cnt=0
°
°
FALSE
condition FALSE
cnt<5 End
TRUE
TRUE
body of loop
Print “dayang”
increment
cnt=cnt+1
y
Repetition Structure (cont..)
What is the Output of the following flowchart when the input is Num= 4
Start
Variables
Variables
Variables (in
(in
(in memory):
memory):
memory):
Num
Num
Num [[[ 444 ]]]
Input: Result
Result
Read Num
Num <- 4
Result [[[ 4
0710]
9 ]]] 049
7 +++ 431
2
Count
Count [[[ 4
Count 320
1 ]]] 431
2 --- 111
Initialize
Result=0
Count=Num
Enter a Number => 4
Count
Count
Count=
Count ===4
132
0
Print Count Count: 4
4
132>
>>>0
000?? ??=>
=>
=>YES
YES
YES
0 => YES
NO Count: 3
Count: 2
Result=Result + Count
Count>0? Yes Count: 1
Count=Count - 1
Count: 0
No
Result: 10
Print
Result
Stop
Repetition Structure –
Letting the User Control a Loop
❑ Program can be written so that user input
determines loop repetition
❑ Used when program processes a list of
items, and user knows the number of items
❑ User is prompted before loop. Their input is
used to control number of repetitions
Repetition Structure (cont..)
Start
cnt=0
Get limit
°
FALSE
cnt<limit End
TRUE
Print “dayang”
cnt=cnt+1
Repetition Structure - Sentinels
Algorithm 3.3: Loop control by sentinel
value
1. Start
2. Set repeat = 1
3. while (repeat = 1)
3.1 Read no1
3.2 Read no2
3.4 Print no1 + no2
3.5 Read repeat
4. end_while
5. End
THANK YOU