1 / 37

Introduction to Programming

Introduction to Programming. Decision Statements. Q: What is a decision?. Something that represents a branching point in a solution Outcomes are often dependent on initial conditions. Decisions in Programs.

lundy
Download Presentation

Introduction to Programming

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. Introduction to Programming Decision Statements

  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: Exercises A = True, B = True, C = False 1. AB 2. AC 3. ABC 4. (AB)  (AC)

  8. 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”

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

  10. Boolean Operations: Java • 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

  11. Try this! Problem: • You’d like to go see a movie. • The movie costs $8.00, a soda costs $2.50 and a large popcorn costs $4.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

  12. Know? • Movie costs $8.00 • Soda costs $2.50 • Popcorn costs $4.50 • How much money I have in my pocket

  13. 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!)

  14. Do? • Option (a) costs $10.50 • Option (b) costs $15.00 • Option (c) costs nothing • What next?

  15. How about a diagram? • This is called a flowchart Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  16. How about a diagram? • Boxes represent actions Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  17. How about a diagram? • Diamonds represent decision points Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  18. How about a diagram? • Arrows show flow Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  19. 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 < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  20. How would I write this? • Using Pseudocode • Wait! • Do you remember what the CENSORED Pseudocode is ?

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

  22. Pseudocode • Example: get x result <- x2 + 5x + 7 print result • That’s it! • Sloppy, ain’t it?

  23. One more time! • Pseudocode...If (Money < $10.50) then Stay home else If (Money < $15.00) then Movie, soda else Movie, soda, popcorn

  24. 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!

  25. 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 $8.00, a soda costs $2.50 and a large popcorn costs $4.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

  26. 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: $8.00 • Movie & soda: $10.50 • All three: $15.00

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

  28. How would I write this? • Determine how we wish to organize our program • Do we want one method? • Or, should we create a few methods? • Let’s two functions: One to input Money from the user • And a second to determine the outcome

  29. How would I write this? • Here’s the prototypes for the methods int getMoney()void showResults(int myMoney)

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

  31. 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?

  32. 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.

  33. 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

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

  35. Multiway Branching • In Java... switch (grade){ case 8: System.out.print(“3-ring binder, loose leaf, “); case 7: System.out.print(“ calculator, “); case 6: System.out.println(“ 5 notebooks, & pencils.”); } • 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

  36. Multiway Branching • Here’s an example with a few break’s System.out.println(“ Your lunch period comes “; switch (grade) { case 8: System.out.println(“ first.”); break; case 7: System.out.println(“ second.”); break; case 6: System.out.println(“ third.”); } No final break

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

More Related