380 likes | 557 Views
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
E N D
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 • 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: Not • A • Expression returns the negation of A
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)
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
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
Know? • Movie costs $15.00 • Soda costs $4.00 • Popcorn costs $7.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 $19.00 • Option (b) costs $26.50 • Option (c) costs nothing • What next?
How about a diagram? • This is called a flowchart Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Boxes represent actions Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Diamonds represent decision points Money < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Arrows show flow Money < $19.00 Money < $26.50 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 < $19.00 Money < $26.50 Stay home Movie, soda & popcorn Movie & soda
How would I write this? • Using Pseudocode • Wait! • What the CENSORED is Pseudocode?
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 < $19.00) then Stay home else If (Money < $26.50) 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 $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
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
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!
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
if (condition) then-action else else-action if (a<5) {b = 1;c = 2;} elseb = 3; Complex statement Simple statement If Statement
Program • Okay, now we get to use our algorithm and form design to create a webpage • 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 calculator pencils 5 notebooks When Grade = 7th calculator pencils 5 notebooks When Grade = 6th pencils 5 notebooks
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
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
Exercise: • Create a webpage that will inform the user which advisor they should go to based on their major code number Well? Get started!