370 likes | 472 Vues
Understand decision statements in programming exploring Boolean algebra, logical and relational operations. Solve exercises to grasp concepts. Learn to write pseudocode for making decisions based on conditions and cost scenarios.
E N D
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 • 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
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
Logical Operations: And • AB • Expression is True iffA and B are both true
Logical Operations: Or • AB • Expression is True if either A or B are True • Note: Also True when A and B are both True
Logical Operations: Exercises A = True, B = True, C = False 1. AB 2. AC 3. ABC 4. (AB) (AC)
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”
Relational Operations: Exercises A = 5, B = 3, C = -7 1. A < B 2. AC 3. (A < C) (B < C)
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
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
Know? • Movie costs $8.00 • Soda costs $2.50 • Popcorn costs $4.50 • How much money I have in my pocket
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!)
Do? • Option (a) costs $10.50 • Option (b) costs $15.00 • Option (c) costs nothing • What next?
How about a diagram? • This is called a flowchart Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Boxes represent actions Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Diamonds represent decision points Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Arrows show flow Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
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
How would I write this? • Using Pseudocode • Wait! • Do you remember what the CENSORED Pseudocode is ?
Pseudocode • Looks like a programming language • Has all the structure of a programming language • Has a verrrrrry loose syntax
Pseudocode • Example: get x result <- x2 + 5x + 7 print result • That’s it! • Sloppy, ain’t it?
One more time! • Pseudocode...If (Money < $10.50) then Stay home else If (Money < $15.00) then Movie, soda else Movie, soda, popcorn
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!
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
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
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!
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
How would I write this? • Here’s the prototypes for the methods int getMoney()void showResults(int myMoney)
Program • Okay, now we get to use our algorithm and program design to create a program • Well, what are you waiting for? Do It!!!
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?
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.
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
Multiway Branching • Case When Grade = 8th 3-ring binder loose leaf paper When Grade = 7th calculator When Grade = 6th pencils 5 notebooks
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
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
Exercise: • Create a program that will inform the user which advisor they should go to based on their major code number Well? Get started!