1 / 38

Decision Statements

Decision Statements. Website Production. Q: What is a decision?. Something that represents a branching point in a solution Outcomes are often dependent on initial conditions. Decisions in Programs. Without decision statements (or other dynamic control structures), programs are static

bowie
Download Presentation

Decision Statements

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. Decision Statements Website Production

  2. Q: What is a decision? • Something that represents a branching point in a solution • Outcomes are often dependent on initial conditions

  3. Decisions in Programs • Without decision statements (or other dynamic control structures), programs are static • Static programs do exactly the same things each time they are executed • Dynamic programs do not

  4. Boolean Algebra • Based on values that are either True or False • True and False values are often represented by 1’s and 0’s, respectively

  5. Logical Operations: And • AB • Expression is True iffA and B are both true

  6. Logical Operations: Or • AB • Expression is True if either A or B are True • Note: Also True when A and B are both True

  7. Logical Operations: Not • A • Expression returns the negation of A

  8. Logical Operations: Exercises A = True, B = True, C = False 1. AB 2. AC 3. ABC 4. (AB)  (AC)

  9. Relational Operations • A < B “A less than B” • A > B “A greater than B” • A = B “A equal to B” • A  B “A less than or equal to B” “A not greater than B” • A  B “A greater than or equal to B” “A not less than B” • A  B “A not equal to B” “A less than or greater than B”

  10. Relational Operations: Exercises A = 5, B = 3, C = -7 1. A < B 2. AC 3. (A < C)  (B < C)

  11. A  B A B A < B A > B A = B A  B A  B A  B A && B A || B A < B A > B A = = B A > = B A < = B A < > B Boolean Operations Traditional Javascript

  12. Try this! Problem: • You’d like to go see a movie. • The movie costs $15.00, a soda costs $4.00 and a large popcorn costs $7.50. • Based on the amount of money in your pocket, determine whether you could...(a) See the movie and buy a soda, (b) See the movie, and buy soda and popcorn, or(c) Stay home

  13. Know? • Movie costs $15.00 • Soda costs $4.00 • Popcorn costs $7.50 • How much money I have in my pocket

  14. Need? • Cost of movie and soda • Cost of movie, soda and popcorn • Way to select one of the three options(that is, make a decision!)

  15. Do? • Option (a) costs $19.00 • Option (b) costs $26.50 • Option (c) costs nothing • What next?

  16. How about a diagram? • This is called a flowchart Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda

  17. How about a diagram? • Boxes represent actions Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda

  18. How about a diagram? • Diamonds represent decision points Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda

  19. How about a diagram? • Arrows show flow Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda

  20. How about a diagram? • The arrow at the top tells us there were previous steps • The arrow at the bottom tells us there are subsequent steps Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda

  21. How would I write this? • Using Pseudocode • Wait! • What the CENSORED is Pseudocode?

  22. Pseudocode • Looks like a programming language • Has all the structure of a programming language • Has a verrrrrry loose syntax

  23. Pseudocode • Example:get x result  x2 + 5x + 7 print result • That’s it! • Sloppy, ain’t it?

  24. One more time! • Pseudocode...If (Money < $19.00) then Stay home else If (Money < $26.50) then Movie, soda else Movie, soda, popcorn

  25. How would I write this? • First, we need to decide how to organize our solution • Should we “hard code” the costs of the movie, soda and popcorn into the algorithm? • Should we input these values? • Let’s take another look at that problem!

  26. How would I write this? • The problem statement tells us the individual costs • So, let’s assume they’re fixed or constant • No need to ask the user for them • Problem: • You’d like to go see a movie. • The movie costs $15.00, a soda costs $4.00 and a large popcorn costs $7.50. • Based on the amount of money in your pocket, determine whether you could...(a) See the movie and buy a soda,(b) See the movie, and buy soda and popcorn, or(c) Stay home

  27. How would I write this? • Another question: Should we pre-compute the cost of each option? • Or, should we let the program do this? • Since we’ve already stated that the item costs are fixed, it would seem logical to pre-compute the cost of each option • Movie: $15.00 • Movie & soda: $19.00 • All three: $26.50

  28. How would I write this? • Next, we need to make sure we have a complete algorithm Input MoneyIf (Money < $19.00) then Display “Stay home.” else If (Money < $26.50) then Display “Go to a movie;buy a soda.” else Display “Go to a movie; buy a soda and popcorn.” • Almost done!

  29. Money: You can … Decide Reset How would I write this? • Create a form design • One textbox for each input • One textbox for each output • One button for each process • Implement this design using HTML

  30. if (condition) then-action else else-action if (a<5) {b = 1;c = 2;} elseb = 3; Complex statement Simple statement If Statement

  31. Program • Okay, now we get to use our algorithm and form design to create a webpage • Well, what are you waiting for? Do It!!!

  32. Multiway Branching • If statements can be used for multiway branching • That is, choosing one of n mutually exclusive outcomes • But what about n outcomes that are not totally unique?

  33. Multiway Branching • Consider the following problem:Each year, a local middle school requires students to purchase supplies based on their grade level. 6th graders need pencils and five notebooks. 7th graders also need a calculator. 8th graders add to this a 3-ring binder with loose leaf paper.

  34. Multiway Branching • We could use a nested If statement to handle this, but there is an alternative • Whenever we need to represent a decision step, with n possible outcomes, where • the outcomes form subsets of each other, and/or • the outcomes are chosen based upon unique scalar values for a control expression, • we can use a Case (switch) structure

  35. Multiway Branching • Case When Grade = 8th 3-ring binder loose leaf paper calculator pencils 5 notebooks When Grade = 7th calculator pencils 5 notebooks When Grade = 6th pencils 5 notebooks

  36. Multiway Branching • In Javascript ... switch (grade){ case 8: out = “3-ring binder, loose leaf, calculator, 5 notebooks, & pencils. “; break; case 7: out = “calculator, 5 notebooks, & pencils. “; break; case 6: out = “5 notebooks, & pencils. ”; } // End of switch • When the switch is encountered, control jumps to the matching case statement and continues until either a break is found or the end of the switch

  37. Multiway Branching • Here’s the same result without breaks … out = “”; switch (grade){ case 8: out += “3-ring binder, loose leaf, ”; case 7: out += “calculator, ”; case 6: out += “5 notebooks, & pencils. ”; } // End of switch

  38. Exercise: • Create a webpage that will inform the user which advisor they should go to based on their major code number Well? Get started!

More Related