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

Slides:



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

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.
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
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.
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.
MIS 3200 – Unit 4.2 ListItem controls – CheckBoxList – ListBox.
MIS 3200 – Unit 3 What can computers do – Follow a sequence of instructions – Follow different paths of instructions based on decisions – Do things over.
Flow of Control Part 1: Selection
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.
Go to your school’s web locker site Your user name is the first letter of your first name, the first four letters of.
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.
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.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
BIT116: Scripting Lecture 05
MIS 3200 – C# (C Sharp)
Precedence Operators Error Types
Unit 9.1 Learning Objectives Data Access in Code
CHAPTER 10 JAVA SCRIPT.
Downloading and Preparing a StudentVoice File for SPSS
Unit C: Expressions Lesson 03: Mathematical Properties with Variables
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Unit 8.2 How data can be used Accessing Data in Views
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
JavaScript: Control Statements.
Using Procedures and Exception Handling
Unit 9.2 Database access from code Database Cycle Review
Web Programming– UFCFB Lecture 17
Windows Desktop Applications
MIS 3200 – Unit 3 What can computers do
Unit 27 - Web Server Scripting
CHAPTER FIVE Decision Structures.
Control Statement Examples
More Selections BIS1523 – Lecture 9.
MODULE 7 Microsoft Access 2010
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Unit 9.3 Learning Objectives Review database access in code
Using the Multiple Choice Template
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSC530 Data Structure - Decision
MIS 3200 – Unit 5.2 ListItem controls and accumulation
T. Jumana Abu Shmais – AOU - Riyadh
Items, Group Boxes, Check Boxes & Radio Buttons
Programming in JavaScript
Iteration: Beyond the Basic PERFORM
CIS 16 Application Development Programming with Visual Basic
ASSIGNMENT OBJECTIVES
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Programming in JavaScript
MIS 3200 – Unit 5.1 Iteration (looping) Working with List Items
Introduction to Excel 2007 Part 3: Bar Graphs and Histograms
MIS 3200 – Unit 6.1 Moving between pages by redirecting
MIS 3200 – Unit 2.2 Outline What’s the problem with bad data?
3.2 Working with Data Scope of variables 29/07/2019.
Intro to Programming (in JavaScript)
Switch Case Structures
Presentation transcript:

MIS 3200 – Unit 4 Complex Conditional Statements else if switch

Decisions One-sided questions: Two-sided questions if true, do something Two-sided questions If true, do something if false, do something else Multiple-sided questions 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 – 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

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

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 DECISIONS and make it an h2 heading Beneath the heading type IF…ELSE IF and make it an h3 heading

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.

L1 #3 Add a button on the next line Add a label under the button 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)

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 decimal variables 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)

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%

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)

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.

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

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

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

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! Note: don’t forget the closing “}” of the switch statement 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.

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

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