Taught by: Hisham Othman
Slides are based on: Prof. Dr. Slim Abdennadher’s slides
German University Cairo, Department of Media Engineering and Technology
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Algorithms: operations
Algorithms can be constructed by the following
operations:
Sequential Operations
Conditional Operations
Iterative Operations
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Objectives
By the end of this lecture, you should be able to:
Design algorithms using conditional operations
Nested-if
if-elif
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
with more than two choices
Nested if-statement
if first_condition:
if second_condition:
# <do something>
else:
# <do something else>
else:
# <do something different>
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
with more than two choices
Nested if-statement
if first_condition:
# <do first thing>
else:
if second_condition:
# <do second thing>
else:
# <do something else>
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Example :
Algorithm to find the largest of three different
numbers.
solution:
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Example 1:
Algorithm to find the largest of three numbers.
solution:
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Example :
Algorithm to find the largest of three numbers.
solution:
A, B, C = eval(input()), eval(input()), eval(input())
if A > B:
if A > C:
print(A)
else:
print(C)
else:
if B > C:
print(B)
else:
print(C)
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Example:
Write an algorithm that reads each student’s marks,
print either a grade or an error message. Students
marks in a class are graded on the following policy:
A: 85-100
B: 74-85
C: 60-74
D: 50-60
F: 0-50
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
mark = eval(input())
if mark<0:
print("Invalid mark")
else: Solution with if and else
if mark<50:
print("F")
else:
if mark<60:
print("D")
else:
if mark<74:
print("C")
else:
if mark<85:
print("B")
else:
if mark<=100:
print("A")
else:
print("Invalid mark")
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
mark = eval(input())
if mark<0:
print("Invalid mark")
else:
if mark<50:
print("F")
else:
if mark<60:
print("D")
else:
if mark<74:
print("C")
else:
if mark<85:
print("B")
else:
if mark<=100:
print("A")
else:
print("Invalid mark")
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
mark = eval(input())
if mark<0:
print("Invalid mark") Solution with if and elif
elif mark<50:
print("F")
elif mark<60:
print("D")
elif mark<74:
print("C")
elif mark<85:
print("B")
elif mark<=100:
print("A")
else:
print("Invalid mark")
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Example :
Given an employee’s medical expenses for a full year,
write an algorithm that computes the amount of
refund base on the following insurance policy:
The insurance does not cover the first 100 LE of medical
expenses.
It refunds 90% of the remaining amount in the first 2000 LE
of expenses
It also refunds100% of any additional expenses above 2000.
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Nested if-statement – examples
Let’s assume a value for the expenses, say 3000
This 3000 contains three parts
3000 = 100 + 1900 + 1000
Refund Detailed Refund
Rule Expenses
rate Calculations Amount
Lower than 100 100 0% 100 * ( 0 / 100) 0
Above 100 and lower
1900 90% 1900 * (90/100) 1710
than 2000
Above 2000 1000 100% 1000 * (100/100) 1000
Total 3000 2710
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
LL = 100
UL = 2000
Expense = eval(input("Enter Expenses:"))
if (Expense < LL):
Refund = 0
else:
if (Expense < UL):
Refund = 0.9 * (Expense-LL)
else:
Refund = 0.90 * (UL-LL) + (Expense - UL)
print(Refund)
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
LL = 100
UL = 2000
Expense = eval(input("Enter Expenses:"))
if (Expense < LL):
Refund = 0
else:
if (Expense < UL):
Refund = 0.9 * (Expense-LL)
else:
Refund = 0.90 * (UL-LL) + (Expense - UL)
print(Refund)
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
LL = 100
UL = 2000
Expense = eval(input("Enter Expenses:"))
if (Expense < LL):
Refund = 0
else:
if (Expense < UL):
Refund = 0.9 * (Expense-LL)
else:
Refund = 0.90 * (UL-LL) + (Expense - UL)
print(Refund)
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
LL = 100
UL = 2000
Expense = eval(input("Enter Expenses:"))
if (Expense < LL):
Refund = 0
else:
if (Expense < UL):
Refund = 0.9 * (Expense-LL)
else:
Refund = 0.90 * (UL-LL) + (Expense - UL)
print(Refund)
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Summary:
In this Lecture:
We have learnt about the nested if
We used “elif” in some cases
We applied nested-if in a medical insurance case:
This is applicable to many layered systems, such as:
Calculation of taxes, given tax rates
Calculation of utility bills, such as:
Electricity bills
Water bills
Gas bills
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
Do we apply conditional statements in our daily life.
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
We can apply conditional statements in our daily life.
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
We can apply conditional statements in our daily life.
Door
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
We can apply conditional statements in our daily life.
Chair
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
We can apply conditional statements in our daily life.
Door
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Conditional algorithms
We can apply conditional statements in our daily life.
Chair
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Any Question?
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman