logging in or signing up decisions Dolorada Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 151 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: February 07, 2008 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Introduction to Programming: Introduction to Programming Decision Statements Q: What is a decision?: Q: What is a decision? Something that represents a branching point in a solution Outcomes are often dependent on initial conditions Decisions in Programs: 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: 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: Logical Operations: And A B Expression is True iff A and B are both true Logical Operations: Or: 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 Logical Operations: Exercises: Logical Operations: Exercises A = True, B = True, C = False 1. A B 2. A C 3. A B C 4. (A B) (A C) Relational Operations: 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: Relational Operations: Exercises A = 5, B = 3, C = -7 1. A < B 2. A C 3. (A < C) (B < C) Boolean Operations: Java: 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!: 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?: Know? Movie costs $8.00 Soda costs $2.50 Popcorn costs $4.50 How much money I have in my pocket Need?: 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?: Do? Option (a) costs $10.50 Option (b) costs $15.00 Option (c) costs nothing What next? How about a diagram?: How about a diagram? This is called a flowchart Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: How about a diagram? Boxes represent actions Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: How about a diagram? Diamonds represent decision points Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: How about a diagram? Arrows show flow Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: 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 < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How would I write this?: How would I write this? Using Pseudocode Wait! Do you remember what the CENSORED Pseudocode is ? Pseudocode: Pseudocode Looks like a programming language Has all the structure of a programming language Has a verrrrrry loose syntax Pseudocode: Pseudocode Example: get x result <- x2 + 5x + 7 print result That’s it! Sloppy, ain’t it? One more time!: 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?: 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?: 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?: 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?: 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?: 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?: How would I write this? Here’s the prototypes for the methods int getMoney()void showResults(int myMoney) Program: 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: 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: 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: 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: Multiway Branching Case When Grade = 8th 3-ring binder loose leaf paper When Grade = 7th calculator When Grade = 6th pencils 5 notebooks Multiway Branching: 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: 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:: Exercise: Create a program that will inform the user which advisor they should go to based on their major code number Well? Get started! You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
decisions Dolorada Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 151 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: February 07, 2008 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Introduction to Programming: Introduction to Programming Decision Statements Q: What is a decision?: Q: What is a decision? Something that represents a branching point in a solution Outcomes are often dependent on initial conditions Decisions in Programs: 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: 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: Logical Operations: And A B Expression is True iff A and B are both true Logical Operations: Or: 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 Logical Operations: Exercises: Logical Operations: Exercises A = True, B = True, C = False 1. A B 2. A C 3. A B C 4. (A B) (A C) Relational Operations: 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: Relational Operations: Exercises A = 5, B = 3, C = -7 1. A < B 2. A C 3. (A < C) (B < C) Boolean Operations: Java: 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!: 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?: Know? Movie costs $8.00 Soda costs $2.50 Popcorn costs $4.50 How much money I have in my pocket Need?: 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?: Do? Option (a) costs $10.50 Option (b) costs $15.00 Option (c) costs nothing What next? How about a diagram?: How about a diagram? This is called a flowchart Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: How about a diagram? Boxes represent actions Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: How about a diagram? Diamonds represent decision points Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: How about a diagram? Arrows show flow Money < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How about a diagram?: 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 < $15.00 Stay home Movie & soda Movie, soda & popcorn Money < $10.50 How would I write this?: How would I write this? Using Pseudocode Wait! Do you remember what the CENSORED Pseudocode is ? Pseudocode: Pseudocode Looks like a programming language Has all the structure of a programming language Has a verrrrrry loose syntax Pseudocode: Pseudocode Example: get x result <- x2 + 5x + 7 print result That’s it! Sloppy, ain’t it? One more time!: 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?: 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?: 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?: 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?: 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?: 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?: How would I write this? Here’s the prototypes for the methods int getMoney()void showResults(int myMoney) Program: 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: 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: 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: 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: Multiway Branching Case When Grade = 8th 3-ring binder loose leaf paper When Grade = 7th calculator When Grade = 6th pencils 5 notebooks Multiway Branching: 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: 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:: Exercise: Create a program that will inform the user which advisor they should go to based on their major code number Well? Get started!