Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.

Similar presentations


Presentation on theme: "MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch."— 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

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 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 Why do you not need to test for >=93.0 again?

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 The entire switch statement is one block of code 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 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 1.Create a new web page in Unit4 2.Name the web page lastnameU4L1.aspx 3.Create a heading in the content area called UNIT 4 L1 LOOPING AND MORE DECISIONS and make it an h2 heading 4.Beneath the heading type IF…ELSE IF and make it an h3 heading

9 L1 #2 5.Under that heading type Total Sales 6.Add a TextBox after Total Sales and call it txtSales 7.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) 8.Under Total Sales type State of Residence 9.Add a TextBox after Residence and call it txtState Change the width of the TextBox to 50px 10.After the TextBox place the following text: (use one of these state codes: OH, WV, PA, KY, IN) 11.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 11.Add a button on the next line Give it an appropriate name Change the Text to Calculate sales tax and total 12.Add a label under the button Name it lblOutputElseif Clear its Text 13.Double click on the button to create the click event method 14.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 15.Create a decimal variable, decSales, and store the converted value of txtSales in it 16.Create a string variable, strState, and store the state code in it 17.Create 3 decimal variables for the state sales tax rate, the calculated state sales tax and the total due 18.Create an if…else if structure to determine which state sales tax rate to use (see next slide)

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

13 L1 #6 22.Continue for the other states Set the KY rate to 5.5% Set the IN rate to 8.25% 23.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 24.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). 25.Clear the values in the sales and state text boxes 26.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 1.Create a new paragraph after L1 output label 2.Grab a Horizontal Rule from the HTML tab of the toolbox and drop it in the new paragraph 3.Create a new paragraph, type SWITCH and make it an H3 heading 4.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 5.Below that line type “State of Residence”

17 L2 #3 6.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 7.Add a button below the state list Give it an appropriate name Change its Text to “Calculate sales tax and total” 8.Add a label named lblOutputSwitch below the button 9.Double click on the button to create a method 10.Add appropriate comments to the method 11.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 12.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 13.The remaining code should be essentially identical to the calculations and output in L1 14.Clean-up: reset the RadioButtonList, clear the TextBox and set the focus to the TextBox. L2 #5

20 L2 #6 15.Add an L2 link to your MIS3200 page and submit your MIS Portfolio public URL 16.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?


Download ppt "MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch."

Similar presentations


Ads by Google