control structure
play

Control structure: Selections 01204111 Computers and Programming - PowerPoint PPT Presentation

Control structure: Selections 01204111 Computers and Programming Cha hale lermsak Cha hatdokmaip ipra rai De Depart rtment of of Com omputer r Eng ngineerin ing Kas asetsart Uni niversity Revised 2018/07/31 Cliparts are taken


  1. Control structure: Selections 01204111 Computers and Programming Cha hale lermsak Cha hatdokmaip ipra rai De Depart rtment of of Com omputer r Eng ngineerin ing Kas asetsart Uni niversity Revised 2018/07/31 Cliparts are taken from http://openclipart.org

  2. Outline • Boolean Type and Expressions • Fundamental Flow Controls • Flowcharts: Graphical Representation of Controls • Basic Selections: if statements • Basic Selections: if-else statements • Programming Examples 2

  3. Python’s Boolean Type: bool • Type bool have two possible values: True and False >>> print(True) Values can be printed out. True >>> print(False) False Values can be assigned >>> x = True to variables. >>> y = False >>> print(x) True You can >>> type(True) >>> print(y) <class 'bool'> False check their >>> type(False) types. <class 'bool'> >>> type(x) <class 'bool'> 3

  4. Boolean Expressions • In Mathematics, a Boolean expression is an expression whose value is either True or False . ◦ 20 > 10 ◦ 5 is a factor of 153 ◦ 18 is not a prime number and is divisible by 3 ◦ x > 5 or x < -5 • Evaluating a Boolean expression is just like answering a yes/no question in human languages: ◦ Do you want the coffee? (yes/no) ◦ Have you found the answer? (yes/no) ◦ Is 20 greater than 10? (yes/no) ◦ Is 5 a factor of 153? (yes/no) 4

  5. Boolean Values Yes True No No False [Images reproduced by kind permission of Chaiporn Jaikaeo & Jittat Fakcharoenphol] 5

  6. Boolean Expressions in Python • In Python, a Boolean expression is an expression of type bool , which is evaluated to either True or False . You can use print() to evaluate a boolean expression and print >>> print( 5 > 3 ) the result. True >>> print( 5 < 3 ) False In interactive mode, >>> print( 5 > 3 and 'pig' != 'rat' ) print() can be omitted. True >>> x = 5 >>> pet = 'pig' >>> x > 3 >>> print( x > 3 and pet != 'rat' ) True True >>> x > 10 or x < 0 >>> print( x*2 > 100 or x+2 > 100 ) False False 6

  7. Watch Out! • Python is case- sensitive so … ◦ False and false are not the same. • Python’s bool constants are written precisely as: ◦ True , or ◦ False [This page is reproduced by kind permission of Chaiporn Jaikaeo & Jittat Fakcharoenphol] 7

  8. How to write a Boolean expression in Python • We can use a relational operator to compare two things: Operator Meaning == Equal != Not equal > Greater than >= Greater than or equal < Less than <= Less than or equal 8

  9. How to write a Boolean expression in Python • We can use logical operators to combine two or more Boolean expressions: Operator Meaning an and Boolean AND or or Boolean OR not not Boolean NOT 9

  10. Quick Review p q p and q p not p True True True True False True False False False True False True False False False False p q p or q True True True True False True George Boole, 1815-1864 False True True An English mathematician The founder of Boolean Algebra False False False [Image via http://www.storyofmathematics.com/19th_boole.html] 10

  11. Hands-On Examples >>> i = 10 >>> j = 15 >>> print( j < i ) A boolean expression can be False assigned to a variable. >>> r = i+2 >= 10 >>> print(r) Both expressions True are logically equivalent. >>> print( (i%2) != 0 ) False >>> print( not ((i%2) == 0) ) Both expressions False are logically equivalent. >>> print( i+j >= 5 and i+j <= 25 ) True You can nest >>> print( 5 <= i+j <= 25 ) them if you True know what you >>> print( ( not r) or (i > 20 and i <= j) ) mean. False 11

  12. Python Operator Precedence • From the highest precedence to the lowest down the table. • Operators on the same row have the same precedence. Category Operators Associativity a[x] f(x) x.attribute Subscription, call, attribute left to right Exponentiation ** right to left +x -x Unary sign left to right Multiplicative * / // % left to right Additive + - left to right == != < > <= >= Relational (comparison) left to right Boolean NOT not left to right and Boolean AND left to right Boolean OR or left to right 12

  13. Operator Precedence: Examples passed = i/j**3-2 < 10 or math. sqrt (i*j)>=20 Operator ** is right-to-left The result is the associative. >>> 4**2**3 value assigned to 65536 the variable passed >>> (4**2)**3 4096 >>> 4**(2**3) 65536 13

  14. More Example ❖ Write a function to check if a given number is a root of the equation X 2 + 3X - 10 = 0 Define a function >>> def isroot(x): to do the task. return x**2 + 3*x - 10 == 0 Call the function >>> print(isroot(2)) to check if the True given number is >>> isroot(-3) a root. False >>> isroot(-5) In interactive mode, True print() can be omitted. >>> isroot(0) False 14

  15. Outline • Boolean Data Type and Expressions • Fundamental Flow Controls • Flowcharts: Graphical Representation of Controls • Basic Selections: if statements • Basic Selections: if-else statements • Programming Examples 15

  16. Fundamental Flow Controls You have already • Sequence learned and used these two control • Subroutine structures. • Selection (or Branching ) • Repetition (or Iteration or Loop ) 16

  17. Schematic View of Flow Controls Sequence Subroutine Repetition Selection 17

  18. Outline • Boolean Data Type and Expressions • Fundamental Flow Controls • Flowcharts: Graphical Representation of Controls • Basic Selections: if statements • Basic Selections: if-else statements • Programming Examples 18

  19. Flowcharts: Graphical Representation of Controls Basic flowchart symbols: Terminator Process Input/output Condition Connector Flow line 19

  20. Example: Try to run this flowchart start with the input sequence: nOdd = 0 5, 1, 4, 9, 8 nEven = 0 true End of write input ? nOdd, nEven false end read k true false k%2 == 0 Can you figure out nEven = nEven+1 nOdd = nOdd+1 what task this flowchart represents? 20

  21. Outline • Boolean Data Type and Expressions • Fundamental Flow Controls • Flowcharts: Graphical Representation of Controls • Basic Selections: if statements • Basic Selections: if-else statements • Programming Examples 21

  22. Normal Sequential Flow • This is the default program flow unless specified otherwise. x = int ( input ()) y = int ( input ()) print ( x + y ) print ( "Hello" , x ) z = x * y + 10 [Images reproduced by kind permission of print ( z ) Chaiporn Jaikaeo & Jittat Fakcharoenphol] 22

  23. Selection flow with if if-statement • Also called conditional execution True height <= 140 price = 40 When height When height if height <= 140 : is 120 is 160 120 160 print( 'Hello kids!' ) price = 0 print( 'price =' , price ) [Images reproduced by kind permission of 23 Chaiporn Jaikaeo & Jittat Fakcharoenphol]

  24. Basic Selection: if statement • if statement is used to decide whether a code block is to be executed or not, depending on a condition . • The statements in the code block will be executed only if the condition is True . 24

  25. Basic Selection: if statement Semantics Condition must be a Boolean expression. Syntax False condition A Code Block True if condition : statement 1 statement 1 statement 2 statement 2 . . statement n statement n 25

  26. Example price = 40 False False height <= 140 height <= 140 True True print 'Hello kids!' print 'Hello kids!' price = 0 price = 0 price = 40 if height <= 140 : if height <= 140 : print( 'Hello kids!' ) print( 'Hello kids!' ) print price price = 0 price = 0 print( 'price =' , price ) [Image: courtesy of Mass Rapid Transit Authority of Thailand] 26

  27. Code Blocks • In Python, a line that ends 1st level of A code block indentation of 3 statements with : (colon) indicates that the next line starts a new code block . def mrt_fee( height ) : price = 40 • A code block consists of if height <= 140 : one or more statements print ( 'Hello kids!' ) that are indented equally price = 0 deeply from the left. print ( 'price =' , price ) 2nd level of A code block indentation of 2 statements 27

  28. Be Careful • Python uses the concept of blocks extensively. • Thus, you must be very careful about indentation. Fdskfjsdlkfslkdjfdsff Fdskfjsdlkfslkdjfdsff fdskfsdflksdlkfdsf: fdskfsdflksdlkfdsf: fddslfldskf fddslfldskf fdsfkdsfdsfd fdsfkdsfdsfd fdkfddfdfd fdkfddfdfd Bad Good fdkfdlf fdkfdlf fdslkdskslkdjsld fdslkdskslkdjsld [Images reproduced by kind permission of Chaiporn Jaikaeo & Jittat Fakcharoenphol] 28

  29. pass-statement for an empty block • In Python, we cannot have an empty block. if height <= 140: print("I'm here") X • If you want a block that does nothing , use the pass statement. if height <= 140: pass print("I'm here") [This page is adapted and reproduced by kind permission of Chaiporn Jaikaeo & Jittat Fakcharoenphol] 29

Recommend


More recommend