Fundamentals of Programming (Python) Control Structures Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python: How to Program 1 st Edition
Outline 1. Control Structures 2. Boolean Expressions 3. Conditionals 4. While Loop 5. For Loop 2
Control Structures Sequential order Statements are executed in the order they are written Transfer of control A program executes a statement other than the following one Do using control structures 3 control structures Sequential structure Selection structure (Conditionals) Repetition structure (Loops) 3
Boolean Expressions Operation x == y x!= y x > y x < y x >= y x <= y Operation x and y x or y not x Meaning Tests if x is equal to y... x is not equal to y... x is greater than y... x is less than y... x is greater than or equal to y... x is less than or equal to y Meaning Tests if both x and y are True... either x or y are True... x is False 4
Conditionals if structure It is a single entry, single exit structure Allows a program to perform an action only if a statement is true Otherwise the action is skipped Condition false true Action(s) 5
Conditionals Example Find min and max of two numbers x = input("enter first number: ") min = float(x) y = input("enter second number: ") max = float(y) if x > y: min, max = max, min print("min:", min) print("max:", max) 6
Conditionals if/else structure Double selection statement Allows the programmer to perform an action when a condition is true An alternate action is preformed when the action is false false Action(s) of False Case Condition true Action(s) of True Case 7
Conditionals Example Find min and max of two numbers x = input("enter first number: ") x = float(x) y = input("enter second number: ") y = float(y) if x < y: print("min:", x) print("max:", y) else: print("min:", y) print("max:", x) 8
Conditionals if/elif/else structure Multiple selection statement This is used in place of nested if/else statements The final else statement is optional condition a false condition b false true true case a action(s) case b action(s) It is used as a default action should all other statements be false... condition z true case z action(s) false default action(s) 9
Conditionals Example Find min and max of two numbers (check for equality) x = input("enter first number: ") x = float(x) y = input("enter second number: ") y = float(y) if x < y: print("min:", x) print("max:", y) elif x > y: print("min:", y) print("max:", x) else: print("min=max=", x) 10
Repetition Structures Repetition Structures (Loops) Allow a program to repeat an action while a statement is true Deterministic Repetition Implemented using for loop Non-Deterministic Repetition Implemented using while loop 11
Repetition Structures while loop The action is contained within the body of the loop Can be one or more than one action Condition should evaluate to false at some point Creates a infinite loop and program hangs Condition False True Action(s) 12
While Loop Example Class average grade (end with sentinel value) total = 0.0 counter = 0 grade = float(input("enter next grade: ")) while grade >= 0: total += grade counter += 1 grade = float(input("enter next grade: ")) average = total / counter print("average Grade: %.2f" % average) 13
While Loop The break statement Used to make a loop stop looping The loop is exited and no more loop code is executed total = 0.0 counter = 0 while True: grade = float(input("enter next grade: ")) if grade < 0: break total += grade counter += 1 average = total / counter print("average Grade: %.2f" % average) 14
Repetition Structures Counter-Controlled Repetition The counter A named variable to control the loop Initial value That which the counter starts at Increment Modifying the counter to make the loop eventually terminate Condition The test that the counter must pass in order to continue looping 15
Repetition Structures for loop Used to implement countercontrolled repetition structure for counter in range(initial, max, step): # Action(s) counter initial counter < max False True Action(s) counter counter + step 16
For Loop Example Class average grade (with known number of grades) total = 0.0 number = int(input("enter number of grades: ")) for counter in range(0, number, 1): grade = float(input("enter grade %d: " % (counter + 1))) total += grade average = total / number print("average Grade: %.2f" % average) 17
For Loop The continue statement Used to continue the looping process All following actions in the loop are not executed But the loop will continue to run Example Even numbers between 0 and 50 not dividable by 3 for counter in range(0, 51, 2): if counter % 3 == 0: continue print(counter, end=", ") 18