1 / 21

MIS 3200 – Unit 4

MIS 3200 – Unit 4. Complex Conditional Statements else if switch. Decisions. One-sided questions: if true, do something Two-sided questions If true, do something if false, do something else Multiple-sided questions If true, do something otherwise, do something else

blenda
Download Presentation

MIS 3200 – Unit 4

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. MIS 3200 – Unit 4 Complex Conditional Statements else if switch

  2. Decisions • One-sided questions: • if true, do something • Two-sided questions • If true, do something • if false, do something else • Multiple-sided questions • If true, do something • otherwise, do something else • otherwise, do something else • otherwise, do something else

  3. if…else if • Some decisions require more than a true/false decisions, for example • Assigning letter grades based on a percentage • Assigning sales tax based on the state • One solution is an if…else if structure that allows you to ask a new question if the first question is false, for example…

  4. Example, grade assignment • Assume that decGrade is defined as a decimal variable and it contains a grade on a 90.0 – 100.0 scale • Assume that strLetterGrade is defined as a string Why do you not need to test for >=93.0 again? If the grade isn’t an A, then test to see if it is a A- If the grade isn’t an A-, then test to see if it is a B+ If the grade isn’t a B+, then test to see if it is a B

  5. When to use • else if is typically used when there are three or more related choices to consider, especially when those choices need to test for conditions other than equality • Letter grade assignment • Admission fees based on age group • In contrast, Nested if’s are good when you need to test two or more independent conditions and don’t want to use && or ||

  6. Switch • If you need to test a series of discrete values (e.g. OH, WV, PA, KY, IN, MI) and do something different for each then the switch may help • The example on the next slide shows the basic structure

  7. Switch example This is the string or numeric variable you need to test Each condition of interest appears in a case statement. This one is a test to see if strState is “OH” Note that the : is required The entire switch statement is one block of code The code for each case normally ends with a break statement. This is a test to see if strState is “KY” or “PA”. If either is true then the tax rate is set to 6.85%. This works because there is no break after the “KY” case and without the break execution just drops into the next case. The break statements effectively say, “get me out of here” and cause execution to transfer to the end of the switch statement -- to whatever comes after the closing }

  8. Time to try it out – L1 • The first part of the L1 deals with the if…else if structure • Create a new web page in Unit4 • Name the web page lastnameU4L1.aspx • Create a heading in the content area called UNIT 4 L1 LOOPING AND MORE DECISIONSand make it an h2 heading • Beneath the heading type IF…ELSE IFand make it an h3 heading

  9. L1 #2 • Under that heading type Total Sales • Add a TextBox after Total Sales and call it txtSales • Add an appropriately named and configured required field validator and a compare validator. The compare validator should make sure that total sales is a positive number (it should be able to accept decimal places) • Under Total Sales type State of Residence • Add a TextBox after Residence and call it txtState Change the width of the TextBox to 50px • After the TextBox place the following text: (use one of these state codes: OH, WV, PA, KY, IN) • Add an appropriately named and configured required field validator for txtState From now on, always use appropriate Validation. It will not be specifically instructed from now on.

  10. L1 #3 • Add a button on the next line • Give it an appropriate name • Change the Text to Calculate sales tax and total • Add a label under the button • Name it lblOutputElseif • Clear its Text • Double click on the button to create the click event method • Add appropriate comments to the method (this method is going to determine which sales tax rate to use, calculate sales tax and total due)

  11. L1 #4 • Create a decimal variable, decSales, and store the converted value of txtSales in it • Create a string variable, strState, and store the state code in it • Create 3 decimalvariables for the state sales tax rate, the calculated state sales tax and the total due • Create an if…else if structure to determine which state sales tax rate to use (see next slide)

  12. L1 #5 • If the state is Ohio, set the state sales tax rate to 6.5% • Else, if the state is West Virginia, set the state sales tax rate to 5% • Else, if the state is Pennsylvania, set the state sales tax rate to 7%

  13. L1 #6 • Continue for the other states • Set the KY rate to 5.5% • Set the IN rate to 8.25% • Add a new if statement (unrelated to the previous one) to see if the sales tax rate is 0(meaning the state did not match one of the five choices so the tax rate was not set) • If it is 0, display the entered state code and a message saying it was not recognized • If the tax rate is > 0 • Calculate the sales tax (tax rate * sales) • Calculate the total due (sales tax + sales)

  14. L1 #7 • Write several lines of output to the label. The output should include the state and state sales tax rate, the amount of the sale, the sales tax and the total amount due. Each value should be labeled and the output should look like an invoice (think of your Unit 2 L3). • Clear the values in the sales and state text boxes • Set focus on the sales text box • Add page level comments, create a link to this page from your MIS3200 page and copy everything to ASPNET and submit your MIS Portfolio URL to the drop box.

  15. Homework: L2 • In this exercise you will repeat the L1 but using a switch instead of the if…else if structure • You can do this exercise using the same page you created for your L1. Make a copy , replace L1 with L2 in the file name. • To make sure things will work properly here, go back and place all the L1 validators and the button in the same validation group (you could call it L1) – put the new validators and button into a ValidationGroup called L2

  16. L2 #2 • Create a new paragraph after L1 output label • Grab a Horizontal Rule from the HTML tab of the toolbox and drop it in the new paragraph • Create a new paragraph, type SWITCH and make it an H3 heading • Below the heading create a line that looks identical to the Total Sales line from L1 but with different names for the text box and the validators • Below that line type “State of Residence”

  17. L2 #3 • Drag a RadioButtonList from the Standard tab of the toolbox and drop it after the word Residence • A RadioButtonList provides a set of mutually exclusive choices, choices where only one thing can be true (use the Selected Value property to get the value) • Name the RadioButtonList rblState • Change its RepeatDirection to Horizontal • Change its RepeatLayout to Flow • Click on Items and open the ListItemCollection Editor • Add five items • The first should have Text and Value = OH • The second should have Text and Value = WV • The remaining items should be PA, KY and IN

  18. L2 #4 • Add a button below the state list • Give it an appropriate name • Change its Text to “Calculate sales tax and total” • Add a label named lblOutputSwitch below the button • Double click on the button to create a method • Add appropriate comments to the method • Repeat steps 15-17 from the L1 (slide 12) to get the state and total sales values and initialize the other variables you will need for this problem

  19. L2 #5 • Use a switch statement to determine the appropriate state tax rate • The switch is based on the state variable and you need one case for each state (note, this is not all of the code!) • Add additional cases for PA, KY and IN • The remaining code should be essentially identical to the calculations and output in L1 • Clean-up: reset the RadioButtonList, clear the TextBox and set the focus to the TextBox.

  20. L2 #6 • Add an L2 link to your MIS3200 page and submit your MIS Portfolio public URL • As always, verify that • Your page title is set • You have modified the comments for the L2 • The link you will supply in the ISMS dropbox is correct (no second chances if the link is incorrect).

  21. Think About It! • Why use an if…else if instead of a nested if statement? • What is a switch statement?

More Related