PF - 3
PF - 3
Programming Fundamentals
3
Algorithms and Problem-Solving
2 Techniques
3 Flow Charts
4 The Flowchart
Dictionary Definition: A schematic representation of a sequence of
operation, as in a manufacturing process or computer program
Technical Definition: A graphical representation of the sequence of
operations in an information system or program
Information system flowcharts: show how data flows from source documents
through the computer to final distribution to the users
Program flowcharts: show the sequence of instructions in a single program or
subroutine
Different symbols are used to draw each type of flowchart
5 The Flowchart
A flowchart
Shows logic of an algorithm
Emphasizes individual steps and their
interconnections
Start
Processing
1. Start
declare num1,
2. declare num1, num2, result num2, result
Input
3. input num1
Input n1
4. input num2 Input
5. results num1 + num2 Input n2
Processing
6. Print results result n1 + n2
7. End
Print result Output
End
10 Class Task
Selection structure
Choose among alternative courses of action
Pseudocode example:
If student’s grade is greater than or equal to 50
Print “Passed”
If the condition is true
Print statement is executed and program continues to next statement
If the condition is false
Print statement is ignored and program continues
Indenting makes programs easier to read
12 If selection structure
Generic Format
true
condition Statement (s)
false
13 if Selection Structure
Translation into Algorithm
If student’s grade is greater than or equal to 50
Print “Passed”
if ( grade >= 50 )
print "Passed";
if structure
Single-entry/single-exit
14 if Selection Structure
General Structure
true false
if condition is met condition
then
statement_1 (s) Statement_1 Statement_2
(s) (s)
else
statement_2 (s)
endif
17
if/else Selection Structure
if
Performs action if condition true
if/else
Different actions if conditions true or false
Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
if ( grade >= 60 )
Print "Passed";
else
Print "Failed";
18 if/else Selection Structure
false true
grade >= 60
print print
“Failed” “Passed”
Example 3: Flow chart
19
Start
Pseudocode:
1.0 Start declare M1, M2,
2.0 Declare M1, M2, M3, M4, average M3, M4, average
6.0 else
true average false
6.1 Print “FAIL” >= 60
7.0 endif
8.0 End Print
Print “PASS” “FAIL”
End
if/else Selection Structure
20
Nested if/else structures
One inside another, test for multiple cases
Once condition met, other statements skipped
if Logical_Expression :
Indented Code Block
22
23
if Logical_Expression :
Indented Code Block 1
else :
Indented Code Block 2
24
25
if Logical_Expression_1 :
Indented Code Block 1
elif Logical_Expression_2 :
Indented Code Block 2
elif Logical_Expression_3 :
Indented Code Block 3
...
else :
Indented Code Block N
26
x = 10
27 y = 20
z = 30
print("Start")
if x == 10:
print(" Nested If")
if y == 20:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
elif y == 20:
print(" Elif block ")
else:
print(" Nested If")
if z == 30:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
print("Stop")
x = 10
28 y = 20
z = 30
print("Start")
if x == 10:
print(" Nested If")
if y == 20:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
elif y == 20:
print(" Elif block ")
else:
print(" Nested If")
if z == 30:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
print("Stop")
a = 10
b = 20
29 c = 30
avg = (a + b + c) / 3
print("avg =", avg)
if grade >= 60
Print "Passed"
else
Print "Failed";
Print "You must take this course again"
endif
33
34
35
36
Sequence Flowchart
37
Start
Input
Read one
number n1
Read second
number n2
Processing
Sum n1 + n2
Output
Display
Sum
End
38 Example 1
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
print “Fail”
else
print “Pass”
end if
39 Example 2
Pseudocode
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
end if
Step 3: Print “The largest value is”, MAX
43
Example 6
Write a pseudocode and draw a flowchart
to read an employee name (NAME), overtime hours worked (OVERTIME), hours
absent (ABSENT)
determine the bonus payment (PAYMENT)
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
Pseudocode
1.0 Declare VALUE1, VALUE2, MAX
2.0 Input VALUE1, VALUE2
3.0 if (VALUE1 > VALUE2) then
3.1 MAX VALUE1
4.0 else
4.1 MAX VALUE2
5.0 end if
6.0 Print “The largest value is”, MAX
46 The Repetition Structure
In flowcharting one of the more confusing things is to separate
selection from looping
This is because both structures use the diamond as their control
symbol
In pseudocode we avoid this by using specific keywords to
designate looping
WHILE/ENDWHILE
REPEAT/UNTIL
The main difference is that while loops are designed to run while a
condition is satisfied and then terminate once that condition returns
false. On the other hand, until loops are designed to run while the
condition returns false and only terminate when the condition returns
true.
47
while Repetition Structure
Repetition structure
Action repeated while some condition remains true
Psuedocode
while there are more items on my shopping list
Purchase next item and cross it off my list
while loop repeated until condition becomes false
Example
Declare product = 2;
while ( product <= 1000 )
product = 2 * product;
48 Looping flowchart
49 while Repetition Structure
Flowchart for while loop
true
product <= 1000 product = 2 * product
false
WHILE / ENDWHILE
50 count = 0
Start
WHILE count < 10
ADD 1 to count
count = 0 WRITE count
ENDWHILE
WRITE “The End”
Write Process
“The End” ADD 1 to count
WRITE count
52 Exercise Program 1
Write pseudocode and draw a flowchart that will
Print first 10 odd numbers starting from the number provided by the user.
If it is even number then start from the next odd number if it is odd number
then start with the current number
E.g. the user input 10 then the out put will be
11 13 15 17 19 21 23 25 27 29
Declare
count, num count = 0
Input num
count False
num < 10
False
mod 2
True
=0
True Print num
num = num +1
num = num + 2
count = count +1
1
END
55 Exercise Program 2
Write a pseudocode and draw a flowchart to
Read an employee number (EMPNO), employee name (NAME), overtime
hours worked (OVERTIME), hours absent (ABSENT) and
Determine the bonus payment (PAYMENT) for 10 employees one by one
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
Thank you