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

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CIS101 Introduction to Computing Week 12 Spring 2004.
PHP and SQL Server: Queries IST2101. Project Report 4 SQL Queries Due Sunday, 4/5 at 11:59pm Instructions on how to access team webspace and SQL database.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
05/09/ Introducing Visual Basic Sequence Programming.
Unit 8.2 Learning Objectives How data can be used – The Money Ball Example The Money Ball Example Data Warehouses – The Role of Data Warehouses The Role.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
THE BIG PICTURE. How does JavaScript interact with the browser?
UNIT 9.2 Learning Objectives A Real world Application of Databases – The Money Ball Example The Money Ball Example Data Warehouses – The Role of Data Warehouses.
UNIT 9.2: Learning Objectives Agile Development – Bruce Feiler on Agile Programming Database access from code – Database Cycle Review – SQL Command Types.
Excel Spreadsheet basics. Excel Sheets and Books  Spreadsheet: tool to analyze, chart and manage data for personal, business and financial use Worksheet:
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
MIS 3200 – Unit 4.2 ListItem controls – CheckBoxList – ListBox.
Unit 8.3 Learning Objectives Insert users into the ASP.NET Membership system from code Capture data being sent to the database Capture Exceptions that.
MIS 3200 – Unit 3 What can computers do – Follow a sequence of instructions – Follow different paths of instructions based on decisions – Do things over.
Conditional Execution
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
MIS 3200 – Unit 5.3 Manipulating ListItem controls – Moving ListItems between ListItem controls – Removing ListItems from ListItem controls.
11 Project 2 Temperature Conversion (Not graded).
Blackboard 8: Grade Center This workshop is for existing users of Blackboard interested in keeping track of student grades online. Blackboard replaced.
CSC350: Learning Management Systems COMSATS Institute of Information Technology (Virtual Campus)
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
1 Project 3 The Kumquat Society Conference. 2 Conference Registration In this project you will write a program to handle a conference registration. The.
Unit 10 – JavaScript Validation Instructor: Brent Presley.
MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items.
31/01/ Selection If selection construct.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 27 Getting “Web-ified” (Web Applications) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Unit 8.2 Learning Objectives Data Warehouses – The Role of Data Warehouses The Role of Data Warehouses – Group Exercise Accessing Data in Views – Accessing.
11 Project 2 Temperature Conversion. 22 Project 2: Temperature Conversion Write an ASP.NET Web Forms app to convert temperatures from Fahrenheit to Celsius.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Working with ASP.NET Controls What is ASP.NET Using server controls in your pages Allowing users to create their own accounts Creating a login page Letting.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Unit 9.1 Learning Objectives Data Access in Code
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Unit 8.2 How data can be used Accessing Data in Views
MIS 3200 – Unit 3 What can computers do
CHAPTER FIVE Decision Structures.
MIS 3200 – Unit 4 Complex Conditional Statements else if switch.
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Unit 9.3 Learning Objectives Review database access in code
MIS 3200 – Unit 5.2 ListItem controls and accumulation
Items, Group Boxes, Check Boxes & Radio Buttons
ASSIGNMENT OBJECTIVES
MIS 3200 – Unit 5.1 Iteration (looping) Working with List Items
MIS 3200 – Unit 6.1 Moving between pages by redirecting
MIS 3200 – Unit 2.2 Outline What’s the problem with bad data?
Presentation transcript:

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

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…

Example, grade assignment Assume that decGrade is defined as a decimal variable and it contains a grade on a 90.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?

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 ||

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

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 }

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

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.

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)

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)

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%

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)

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.

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

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”

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

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 from the L1 (slide 12) to get the state and total sales values and initialize the other variables you will need for this problem

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

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).

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