1 / 23

CS 177 Week 5 Recitation

CS 177 Week 5 Recitation. Booleans and Control Flow. Announcements. Contents. Booleans Decision structures While loop Project 2 preview. Booleans. Boolean (logical) expressions: An expression that can assume only the true or false value

Télécharger la présentation

CS 177 Week 5 Recitation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 177 Week 5 Recitation Booleans and Control Flow

  2. Announcements

  3. Contents Booleans Decision structures While loop Project 2 preview

  4. Booleans • Boolean (logical) expressions: • An expression that can assume only the true or false value • We use logical expression in everyday language: • If today is raining, then bring an umbrella when you go out. • “today is raining” is a logical expression: its value can be either true or false. • Other examples: assume x=4 assume str=“abc” • x>3 type(str)==int • (true) (false)

  5. Booleans • Boolean operators: • And, or, not a and b a and true x > 0 and x <=2 y > 0 and y >= 3(overlapped)

  6. Booleans • Boolean operators: • And, or, not a or b a or true x <= 0 or x > 2 x > 5 or x < 10 (always true)

  7. Booleans • Boolean operators: • And, or, not not a not (not a) not x > 3 DeMorgan’s law not (a or b) == (not a) and (not b) not (a and b) == (not a) or (not b)

  8. Booleans • (P and (not Q)) or ((not P) and Q) • It has a name: XOR • Can you do this?

  9. Booleans • Does the result look familiar? • How about this: Let T=1, F=0 • Yes, it is the sum of binary numbers

  10. Decision Structures x = 5 if x>1: print(“print something”) The string is printed x = 0 if x>1: print(“print something”) Does nothing! • If statement • An if statement takes a logical expression and evaluates it. • If it is true, the statements in if block are executed, otherwise, they are not executed. Simple decision

  11. Decision Structures a = 45 if a < 100: print(“a is small”) else: print(“a is large”) >>> a is small a = 153 if a < 100: print(“a is small”) else: print(“a is large”) >>> a is large • Two-way decision

  12. Decision Structures no yes a < 100? “a is large” “a is small” • Two-way decision

  13. Decision Structures a = 1.5 if a > 2: print(“a>2”) else: if a > 1: print(“1<a<=2”) else: print(“a<=1”) >>> 1<a<=2 a = 1.5 if a > 2: print(“a>2”) elif a > 1: print(“1<a<=2”) else: print(“a<=1”) >>> 1<a<=2 • Multi-way decision

  14. Decision Structures no yes a >2 ? no yes a >1 ? “a>2” “a<1” “1<a<=2” Multi-way decision

  15. For vs While Loop • “How long do I have to shower?” You have two replies you could make: • a) 10 minutes • b) Until you are clean • a) When programming, the first answer would be portrayed as a for-loop because we focus on exactly how long the shower will continue: for minutes in range (0,9): shower • b) The second answer would be portrayed as a while-loop because the length of the shower is undetermined; we instead focus on the condition of cleanliness: while you are not clean: shower

  16. For vs While Loop • for count in range(0,9): • print(“The count is:”, count) • print(“for loop ended”) • count = 0 • while count < 9: • print(“The count is:”,count) • count = count + 1 • print(“while loop ended”)

  17. Rules of While Loops • Command name: while • Boolean condition (in the previous example: count < 9) • determines the termination of loop • A colon (“:”) • And a block (the indented lines of code)

  18. Interactive loop • count = 0 • str = “Yes” • while str == “Yes”: • print(“The count is:”,count) • count = count + 1 • str = input(“continue? Yes or No:”) • print(“while loop ended”) Using while loop, we can write interactive loops

  19. Project 2 Preview • Population Growth Simulation • The Geometric growth model • No limitation on the population • For loop • Show the population in characters • Limitation on the population • While loop • The limitation is the stopping criterion • Show the population in graphs

  20. Project 2 Preview Main function Function 1 Function 2 . . . The structure of a project should be like this:

  21. An Example from the Book def main(): sing("Fred") print() sing("Lucy") def sing(person): happy() happy() print("Happy birthday, dear", person + ".“) happy() def happy(): print("Happy birthday to you!")

  22. An Example from the Book f1 f2 parameter return value parameter return value f1 f2 • Functions link to each other by their parameters and return values • What does f2(f1()) mean? • Pass the function f1 to f2? No • Pass the return value of f1 to f2? Yes • v=f1() • f2(v)

  23. Questions?

More Related