Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do...
Transcript of Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do...
Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham OthmanSlides are based on: Prof. Dr. Slim Abdennadher’s slides
German University Cairo, Department of Media Engineering and Technology
How to construct an algorithm Identify the input of the algorithm (if needed)
Introduce variables for
Input
(intermediate) results
Analyze the task into steps
Provide for detailed output
Objectives By the end of this lecture, you should be able to:
Design algorithms using conditional operations
Algorithms: operations Algorithms can be constructed by the following
operations:
Sequential Operations
Conditional Operations
Iterative Operations
Conditional operation – idea Decision
name_of_speaker = input(“Tell who is the speaker:”)
if name_of_speaker == “Einstein" :
print("I will go home")
else:
print("I will stay at the GUC")
Conditional operation – principle Rationale
Determines whether or not a condition is True; and based on whether or not it is True; selects the next step to do
Notation Use the same primitives as before plus the following:
Execution Evaluate <condition> expression to see whether it is True or False. If True, then execute operations in if-part Otherwise, execute operations in else-part
if condition:
# <operations for the if-part>
else:
# <operations for the else-part>
Conditional operation – diagram Example:
Write an algorithm that takes a number as an input and prints out a message indicating whether the number is negative or positive.
Conditional operation – diagram Example:
Write an algorithm that takes a number as an input and prints out a message indicating whether the number is negative or positive.
Conditional operation – diagram
solution:
num = eval(input())
if num < 0:
print("The number is negative")
else:
print("The number is positive")
print("Good-bye for now")
Conditional operation – examples Example 1:
Write an algorithm to compute the absolute value of a given number.
solution:
Number = int(input())
if (Number >= 0):
Value = Number
else:
Value = (-1) * Number
print(Value)
Conditional operation – examples Example 2:
Give the user a choice of seeing the area or the circumference of a circle given its radius.
solution:
Conditional operation – examples Example 2:
Give the user a choice of seeing the area or the circumference of a circle given its radius.
solution:
radius = eval(input(“Enter radius:”))
response = input("Type A for area or C for circumference:")
if (response == "A"):
area = (radius * radius * 3.14)
print(area)
else:
circumference = (2 * radius * 3.14)
print(circumference)
Conditional operation – examples Example 3:
Write an algorithm to convert Euro (EUR) to Egyptian Pound (EGP) and Egyptian Pound to Euro. The inputs to your algorithm are the following:
Amount of money to be converted
Conversion Type, i. e.:
1 for EUR to EGP
2 for EGP to EUR
Exchange Rate (i. e., a EUR is equivalent to 20 EGP)
Conditional operation – examples Solution 3:
amount, type, rate = eval(input()), eval(input()),
eval(input())
if type == 1:
amount = amount * rate
else:
amount = amount / rate
print(amount)
Compounded conditions Conditions may be compounded using:
AND, OR, NOT
E1 or E2: True if at least one of them is True; False otherwise.
E1 and E2: True if both are True; False otherwise.
not E: True if E is False and False if E is True.
Compounded conditions –examples Example 4:
Find the sum of three positive numbers
solution:
A, B, C = eval(input()), eval(input()), eval(input())
if (A > 0) and (B > 0) and (C > 0):
Sum = (A+B+C)
print(Sum)
Any Question?