PF - 2
PF - 2
Programming Fundamentals
2
Algorithms: Pseudocode
3 Algorithms
If-then
General form:
if (condition is met) then
statement(s)
Example:
if temperature < 0 then
wear a jacket
14 Pseudo-Code: Decision Making
If-then
General form:
if (condition is met) then
statement(s)
Example:
if temperature < 0 then
wear a jacket
15 Pseudo-Code: Decision Making
If-then-else
General form:
if (condition is met) then
statement(s)
else
statements(s)
Example:
if (at work) then
Dress formally
else
Dress casually
16 Decision Making
while-do
General form:
while (condition is met)
statement(s)
Example:
while students ask questions
Answer questions
21 Algorithms and Pseudocode
Take and input of four marks and calculate average. If the
average is above 50 the student is declared pass.
22 Algorithms and Pseudocode
Algorithm:
Input a set of four marks
Calculate their average by summing and dividing the sum by 4
If average is above 50
Print : “PASS”
23 Algorithms and Pseudocode
Pseudocode:
1.0 Declare M1, M2, M3, M4, average
2.0 Input M1, M2, M3, M4
3.0 average = (M1+ M2+ M3+ M4) / 4
4.0 if (average > 50) then
4.1 Print “PASS”
5.0 endif
24 Algorithms and Pseudocode
Algorithm:
Input a set of four marks
Calculate their average by summing and dividing the sum by 4
If average is above 50
Print : “Pass”
else
Print “FAIL”
26 Algorithms and Pseudocode
Pseudocode:
Step 1: Input M1, M2, M3, M4
Step 2: Grade = (M1+ M2+ M3+ M4) / 4
Step 3: if (Grade > 50) then
Print “PASS”
else
Print “FAIL”
endif
27 Pseudo-Code: Decision Making
If-then-else
General form:
if (condition is met) then
statement(s)
else
statements(s)
Example:
if (at work) then
Dress formally
else
Dress casually
28 Algorithms and Pseudocode
Take and input of four marks and calculate average. If the average is
above 50 the student is declared pass otherwise failed.
29 Algorithms and Pseudocode
Algorithm:
Input a set of four marks
Calculate their average by summing and dividing the sum by 4
If average is above 50
Print : “PASS”
else
Print “FAIL”
30 Algorithms and Pseudocode
Pseudocode:
1.0 Declare M1, M2, M3, M4, average
2.0 Input M1, M2, M3, M4
3.0 average = (M1+ M2+ M3+ M4) / 4
4.0 if (Grade > 50) then
4.1 Print “PASS”
5.0 else
5.1 Print “FAIL”
6.0 endif
31
Use pseudo-code to specify the algorithm for a person who is ordering food
at a fast food restaurant
At the food counter, the person can either order or not order the following
items:
a burger
Fries
a drink
After placing her order the person then goes to the cashier and pays the
bill
34 Algorithm: Fast Food Example
Compound statement
Set of statements within a block
Write a pseudocode
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
Thank you