control structure
play

Control Structure: Multiple Selections 01204111 Computers and - PowerPoint PPT Presentation

Control Structure: Multiple Selections 01204111 Computers and Programming Chal halermsak Chat hatdokmaip ipra rai Depart De rtment of of Com omputer r Eng ngineerin ing Kas asetsart Uni niversity Cliparts are taken from


  1. Control Structure: Multiple Selections 01204111 Computers and Programming Chal halermsak Chat hatdokmaip ipra rai Depart De rtment of of Com omputer r Eng ngineerin ing Kas asetsart Uni niversity Cliparts are taken from http://openclipart.org Revised 2018/07/11

  2. Outline • Introduction to multiple selections • Nested Conditionals • Chained Conditionals • Programming examples 2

  3. Review : Basic Selection s A single if statement A single if-else statement # in Python if condition : selects whether or not selects one of two statements Statement1 (or blocks) to be executed. a code block is to be else : executed Statement2 Statement3 T F condition Statement4 F condition Statement1 Statement2 T Statement 1 Statement3 Statement 2 Statement 4 # in Python if condition : Statement 3 Statement1 There are only two possible Statement2 paths of execution in both Statement3 constructs. 3

  4. Path #3 Path #1 Introduction T F Path #2 x > y • Multiple selection m = x m = y selects one of three F or more paths of z = x-y z > m execution. T m = 1 Example r = x+z-m How many possible paths of execution? 4

  5. How to do multiple selections in Python Mul ultiple tiple sel elections ections Cha hained ined Nested sted Con ondit ditio iona nals ls Con ondit ditio iona nals ls 5

  6. Outline • Introduction to multiple selections • Nested Conditionals • Chained conditionals • Programming examples 6

  7. How nested conditionals are possible in Python Each of these yellow boxes is actually a single statement . x = y+1 if x > y: pass print('hi') if x > y: if x > y: if x > y: m = x m = x m = x m = x z = x+y z = x+y z = x+y else: else: m = y m = y z = x-y so each can be put here: if condition : statement 1 statement 2 statement 2 else: statement 3 or here: statement 4 statement 4 statement 5 7

  8. Example in Python if x > 0: i = 2 When an if or if-else if y > 1: statement is put within k = 2 another if or if-else else: statement , we calls it if z < 10: a nested conditional k = 5 construct . if y > 5: k = x+y else: k = x-y In Python, indentation is very, very important ! 8

  9. Nested conditionals start as just a single statement Flow of execution A single statement T F if x > 0: x > 0 if x > 0: i = 2 F i = 2 z < 10 if y > 1: Code Block 1 i = 2 Statement if y > 1: k = 2 T k = 2 k = 5 F else: else: y > 1 if z < 10: if z < 10: T T F k = 5 y > 5 k = 5 k = 2 if y > 5: Code Block 2 if y > 5: k=x+y k=x-y k = x+y Code block k = x+y Statement else: else: k = x-y k = x-y Recall that a code block follows the line that ends with a colon. 9

  10. Path #3 Flow-of of-Control Path #1 Path #2 T F Example x > y m = x m = y F z = x-y # in Python z > m if x > y : T m = x m = 1 if z > m : m = 1 else: m = y z = x-y r = x+z-m r = x+z-m 10

  11. 11

  12. Task: The maximum of three numbers • Write a program that ➢ reads three numbers. ➢ computes and prints the maximum of the three. Sample Enter 1st number: 25.5 Sample Run Enter 2nd number: 30 Run Enter 3rd number: 20.2 The max is 30 Enter 1st number: 50 Enter 1st number: 0 Sample Enter 2nd number: 5 Enter 2nd number: -10 Enter 3rd number: 50 Run Enter 3rd number: -7 The max is 50 The max is 0 12

  13. The maximum of three numbers - Ideas ❖ What is the maximum of 3 numeric values? • Answer : The value that is not less than the other two. • Therefore, to find the maximum is to look for a value that is not less than the other two . • It’s OK if some of them are equal, or even all of them are equal. 13

  14. Topmost level ❖ The main routine: • Reads three numbers. • Computes the max by calling max_of_three() • Prints the max. # --- main --- # x = float ( input ( "Enter 1st number: " )) y = float ( input ( "Enter 2nd number: " )) z = float ( input ( "Enter 3rd number: " )) max = max_of_three ( x , y , z ) print ( f"The maximum number is {max}" ) 14

  15. The function max_of_three() () – Design • Now it’s time to write the function max_of_three(). • There are many ways to write it. • We’ll show a few different ways so as to demonstrate the use of nested conditionals. 15

  16. The function max_of_three() () – Version 1 if ( a is not less than the other two) Algorithm a is the max else # a is not the max Compete b with c for the max def max_of_three( a , b , c ): if a >= b and a >= c: # check a return a Efficiency: else: # a is not the max 2 or 3 comparisons if b > c: depending on return b the inputs else: return c 16

  17. The function max_of_three() () – Version 2 if ( a > b ) # so a may be the max Algorithm Compete a with c for the max else # so b may be the max Compete b with c for the max def max_of_three( a , b , c ): if a > b : # a may be the max if a > c: return a else: return c Efficiency: else : # b may be the max exactly two comparisons if b > c: in all cases return b else: return c 17

  18. The function max_of_three() () – Version 3 • Let max be the value of a • if ( b > max ) then Algorithm Let max be the value of b • if ( c > max ) then Let max be the value of c def max_of_three( a , b , c ): This is actually a max = a sequence of two if statements, not a if b > max: nested if construct. max = b if c > max: max = c; Efficiency: This version can be return max exactly two easily extended to comparisons 4 or more numbers. in all cases How? 18

  19. The function max_of_three() () – Version 4 • No if - or if-else statements used. • No need to write the function max_of_three(). • Throw away the main routine. Hmm…? • In fact, no need to write a program at all! Amitta Buddh …??? 19

  20. Nammo Amitta Pythonic Buddha! >>> max(5,6) 6 >>> max(5,6,4) 6 >>> max(5,7,10,3) 10 >>> max(3,70,5,8,10,15,75,8,40) 75 >>> type(max) <class 'builtin_function_or_method'> 20

  21. Outline • Introduction to multiple selections • Nested conditionals • Chained conditionals • Programming examples 21

  22. Chained Conditionals • What is a chained conditional ? The use of an orderly sequence of k conditions (k  2) to select one of k+1 code blocks to execute. ❖ It is also informally called the if-elseif-else control structure . 22

  23. Example F T Today is Use 3 conditions Friday to select one of F the four sets of T Play Today is planned activities Saturday football Go F T Today is swimming Sunday Watch a movie Play Go Go to a basketball Jogging party What are the planned activities on Monday, Tuesday, Wednesday, or Thursday? 23

  24. Chained conditionals in Python implemented by nested conditionals Use 3 conditions Flow of to select one of execution F T 4 code blocks cond 1 if cond 1 : F T code cond 2 code_block 1 block 1 else: F T code if cond 2 : cond 3 block 2 code_block 2 code code else: block 4 block 3 if cond 3 : code_block 3 else: code_block 4 Note that this whole box is actually a single Python statement . 24

  25. Then, Chained conditionals in Python rearrange implemented by if if-elif-else statements the flowchart accordingly else: followed by if if can be F T replaced by Python keyword elif T cond 1 code cond 1 block 1 if if cond 1 : cond 1 : code F T F cond 2 code_block 1 block 1 code_block 1 T code else : cond 2 elif cond 2 : code F T block 2 if cond 2 : cond 3 block 2 F code_block 2 code_block 2 code T code code else : elif cond 3 : cond 3 block 4 block 3 block 3 if cond 3 : code_block 3 F code_block 3 else : code else : block 4 code_block 4 code_block 4 Then you must re-indent them to become an if if-eli elif-els else statement 25

  26. The fl flow charts of both implementations show that F T T code cond 1 cond 1 block 1 F F T code cond 2 T block 1 code cond 2 Both work block 2 F T code cond 3 exactly F block 2 T code code the same. code cond 3 block 3 block 4 block 3 F code block 4 if cond 1 : if cond 1 : Therefore, code_block 1 code_block 1 these two elif cond 2 : else : Python code_block 2 if cond 2 : elif cond 3 : code_block 2 constructs code_block 3 else : work exactly if cond 3 : else : the same too. code_block 4 code_block 3 else : code_block 4 26

  27. Example: Check how an in integer is is divided by 5 • Write a function divfive() to check how an integer is divided by 5: >>> divfive(50) 50 is divisible by 5 Sample >>> divfive(54) 54 is not divisible by 5 Run the remainder is 4 >>> divfive(53) 53 is not divisible by 5 the remainder is 3 >>> divfive(52) 52 is not divisible by 5 the remainder is 2 >>> divfive(51) 51 is not divisible by 5 the remainder is 1 27

  28. div ivfive() () - version 1 This version is to show that you can have as many elif-clauses as def divfive( d ): # version 1 you need. rem = d % 5 if rem == 1 : print ( d , 'is not divisible by 5' ) print ( 'the remainder is 1' ) elif rem == 2 : print ( d , 'is not divisible by 5' ) print ( 'the remainder is 2' ) elif rem == 3 : print ( d , 'is not divisible by 5' ) print ( 'the remainder is 3' ) elif rem == 4 : print ( d , 'is not divisible by 5' ) print ( 'the remainder is 4' ) else : print ( d , 'is divisible by 5' ) 28

Recommend


More recommend