Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIS 3200 – Unit 3 What can computers do

Similar presentations


Presentation on theme: "MIS 3200 – Unit 3 What can computers do"— Presentation transcript:

1 MIS 3200 – Unit 3 What can computers do
Follow a sequence of instructions Follow different paths of instructions based on decisions Do things over and over again – loops This unit focuses mainly on decisions dealing with conditions if statement nesting if statements and / or shortcut operators

2 Basic program structures
Computers are good at doing a series of operations one after the other One way to view this is shown here The sequence has a starting point There are one or more steps Any number of steps is possible There is a stopping point For example The click event methods you have been working with have a sequence of steps that start with the open { and end with the closing } The instructions within a method are always executed top to bottom (or start to stop like you see above)

3 REALLY IMPORTANT! Common Errors
Using variables before they are declared Using the value of a variable before it has been assigned Outputting final results before they are calculated Normal processing starts at the top and moves towards the bottom – ALWAYS - the only “exception” is when you are in a loop!!

4 Program structures 2 Computers need to make decisions
They do that by testing conditions Is this person over 18? Is this sale taking place in Ohio? Is this package going to shipping zones 5 or 6? Did the Bobcats beat the Miami? Is this employee eligible for overtime pay and if they are did they work enough hours to earn it? Is this student and MIS major or a Finance major or a Marketing major?

5 Decisions 2 Some decisions are one-sided – if the condition is true do something (B), if not just skip to the next thing (C) One way to show this is If the condition is true Do the things at B Once B is complete move to C If the condition is false Go directly to C TRUE or FALSE: regardless of the condition, will you always end up at C?

6 Decisions 3 Some decisions are two-sided
Do one thing if the condition is true (B) Do something different (C) if the condition is false TRUE or FALSE: Regardless of which branch you take, will you always end up at (D)?

7 Program structures 3 Computers need to do some things over and over in a loop (the technical term is iteration) There are several ways to create loops but the most common one looks like this test a condition if the condition is true, do the things at B once finished with B go back and test the condition again if the condition is true go to B, if not, go to C What do you think will happen if the condition is false from the beginning?

8 Decision making in C# The most common structure in C# of making decisions is the if statement One sided decision if Two sided decision if else

9 Conditions Conditions, or logical expressions, have only two possible value: true and false Can be as simple as a Boolean variable (true/false) Can be a relational expression !!

10 Relational operators There are six types of relations that can be tested Relationship Relational operator Equality == Inequality (not equal) != Greater than > Greater than or equal >= Less than < Less than or equal <=

11 Relational expressions
Only have two possible values: true or false

12 Two-sided if statement
Sometimes you want to do one thing if a condition is true and something different if it is false

13 Nested if Sometimes we need to consider two or more conditions before we can act One approach Put one if statement inside another so you only look at it if the first one is true The if statement inside begins before the oen outside This is called nesting and the second if statement can be on either the true or false side of the first if statement

14 Nested if - example If the employee is not Hourly, you immediately skip to here You only get to this part if the employee is an Hourly employee

15 Another approach Nested if statements are often used when we need to test two or more conditions at the same time. Sometimes we need All conditions to be true before we do something Any one of several conditions to be true before we do something C# provides a way to test more than one condition in a single if statement

16 Multiple conditions, all true
&& which is called an AND operation Do something ONLY if all conditions are true Don’t do it if ANY condition is false For example, and employee is an hourly employee AND has worked more than 40 hours In C# the AND operation is represented by && Both sides of the && operator must be logical conditions – they must have or produce a value of either true or false

17 Multiple conditions, at least one of which is true
|| which is called an OR operation Do something if one or more of the conditions is true Don’t do it if ALL conditions are false For example, give a discount to a young person or an older person In C# the OR operation is indicated by || (note, this symbol is normally found over the \ key on most keyboards) Both sides of the || operator must be logical conditions – they must have or produce a value of either true or false

18 Web Controls for Decisions
Some are lists, while others are not! NON- LIST CONTROLS CheckBox RadioButton LIST CONTROLS CheckBoxList DropDownList ListBox RadioButtonList

19 Handling different controls
NON-LIST Use CheckChanged for method name Use .Checked in the if statement Used with checkboxes Values stored in .Text LIST Use SelectedIndexChanged for method name Use .SelectedValue in the if statement Used with CBL & RBL Values stored in Items

20 RadioButtonList: Adding
Similar to RadioButtons but easier to use when you have multiple choices Choices are represented by a series of ListItems ListItems are created using the EditItems… option of the smart menu An alternative to radio button lists is the standalone RadioButton. An alternative to the Check box lists is the Checkbox. Typically used to represent two or more mutually exclusive choices (only one can be true at a time) Each Checbox or Radiobutton has a Checked property If the button is pushed – Checked is true If the button is not pushed – Checked is false By default ALL RadioButtons on a page are NOT linked to each other and any number of them can be checked This behavior can be changed by putting the buttons in groups using the GroupName property RadioButtons in the same group can only be checked one at a time (clicking another RadioButton in the same group will un-check any other RadioButton in the group). This behavior is automatic in a RadioButtonList, and therefore it is advantageous to use RadioButton Lists instead of standalone Radio buttons. Checkbox and CheckboxLists on the otherhand are both used. The CheckChanged Method The default event for a RadioButton or Checkbox is CheckedChanged Once a RadioButton is selected it stays selected until another RadioButton in the same group is selected. This means you can’t unselect a RadioButton by clicking it. This is not the case for a standalone Checkbox. It can be selected and de=selected as needed. And this means the CheckedChanged event only fires when the RadioButton is selected. For the checkbox it means that it fires each time the state of checking it is changed. You need a separate CheckedChanged method for each RadioButton or Checkbox standalone items, but only one for the List Collection Items. New RadioButtonList Smart menu button RadioButtonList with expanded smart menu – click on Edit Items…

21 RadioButtonList: Editing
Completed RadioButtonList with its RepeatDirection property set to Vertical New ListItem with Text and Value set Click to add a new ListItem The Text property is what the user sees Completed RadioButtonList with its RepeatDirection property set to Horizontal The Value property is what the program sees and and the value used in decisions

22 RadioButtonList: Processing
There is only one event, a SelectedIndexChanged event, for the entire RadioButtonList Once the event fires there are several ways to determine which button is clicked

23 Shortcut operators Shortcut operator are of two types Accumulation
A value is added to the value & stored in another variable And the result is stored back in the same variable Moving by one value When the new value only changes by ONE (+1 or -1) A little history on arithmetic operators… C# belongs to a family of languages that began with a language called C At the time C was created programs were written on VERY slow devices (like teletype terminals that could print at most 10 characters per second) Because of this environment C, and all C family languages, has a number of shortcuts designed to shorten typing and printing time. More Shortcut Operators One shortcut was developed because programs often have statements similar to this where A value is added to the value & stored in another variable And the result is stored back in the same variable (this particular operation is know as accumulation) The shortcut version of this is The + is moved to the left of the = The variable receiving the new value is not repeated If we say that decTotalSales originally = 5 and decNewSale = 1 then, decTotalSales += decNewSale sets decTotalSales = 6

24 Accumulation Standard operation intCnt = intCnt + 1;
intDaysLeft = intDaysLeft – 1; decDoubleSeries = decDoubleSeries * 2.0M; decDiminish = decDiminish / M; strNameList = strNameList + “, ” + strNew shortcut intCnt += 1; intDaysLeft -= 1; decDoubleSeries *= 2.0M; decDiminish /= M; strNameList += “, ” + strNew It works for string concatenation (i.e. accumulation) too!

25 Adding or Subtracting by ONE
This is a simplification of Accumulation intCnt +=1;  intCnt++; intCnt -=1;  intCnt--; Many shortcuts are used to do the first two operations illustrated on the previous slide: add one to (increment) or subtract one from (decrement) a variable. This is so common that C family languages have special increment and decrement operators. Increment and decrement operators can be used in arithmetic expressions with other operators. Increment and decrement operators are ALWAYS written immediately next to the variable they modify with no spaces. Increment and decrement operators can come either before or after a variable. If they come after the variable the operation is called a “post-increment” operation. That means that the current value of the variable is used in the equation and then the variable it is incremented (after, or post use). If the increment operator comes before the variable the operation is called a “pre-increment (or decrement)”. That means that one is added to (or subtracted from) the current value of the variable and the new value is used in the equation (the variable is incremented/decremented before, or pre use). We will not use shortcut operators or increment/decrement operators very frequently but … You will see them in sample code and examples and… There are some places where they are very useful and you would likely have a need for them: Concatenating to a string or Label Controlling loops (Unit 4)

26 Try it – L1 This exercise will give you a chance to use if statements and test different conditions Be sure ASPPub is on your desktop, open VWD and open ASPPub as your website Select Unit3 under MIS3200 and add a new web page called yourLastNameU3L1.aspx Click in the MainContent area and press Enter several times In the top paragraph type UNIT 3 – L1 – USING IF STATEMENTS If you have problems with any of this, please refer back to L1 instructions in previous units.

27 L1 #2 Convert the text to an h2 heading and make it have white letters on a dark green background Add a table to the second paragraph. The table should have 8 rows and 3 columns In the first row first column, type Enter a state abbreviation Drop a TextBox immediately after the text Change the (ID) to txtState Drop a button in the second column Name the (ID) to btnState Change the Text to Test State

28 L1 #3 Drop a Label in the third column
Change the (ID) to lblState Clear the Text Double-click the Test State button Add appropriate method level comments Add the following code within the button’s method {} Run the page and test with different values in the text box

29 L1 #4 In the second row, first column
Type Enter a negative whole number Drop a TextBox after the words Change its (ID) to txtNegative Drop a RequiredFieldValidator after the TextBox Change the (ID) to rfvNegativeNumber Change the ControlToValidate to txtNegative Change the ErrorMessage to The negative number is required Change the Text to * Change the ForeColor to Red Change the ValidationGroup to group1 Note: Validation controls are normally in a single group. If you want to have a few controls work together you put them in a ValidationGroup much like can place radio buttons in a group.

30 L1 #5 Drop a CompareValidator after the RequiredFieldValidator
Set the (ID) to cvNegative Set the ControlToValidate to txtNegative Set ErrorMessage to The negative number must be a whole number Set the Text to * Set ForeColor to Red Set Operator to DataTypeCheck Set Type on Integer Set ValidationGroup to group1 Drop a ValidationSummary after the CompareValidator Set the (ID) to vsNegative Set ShowMessageBox to true Set ShowSummary to false

31 L1 #6 In the second column of the second row
Drop a Button Set the (ID) to btnNegative Set Text to Test Number Set ValidationGroup to group1 In the third column of the second row Drop a Label Set the (ID) to lblNegative Clear the text Double-click the Test Number button Convert the contents of txtNegative to an integer Test to see if the value is negative and display a message (see next slide) Related Validators, ValidationSummaries and Buttons all have to be in the same ValidationGroup

32 L1 #7 Add appropriate method level comments
Run the page and test to be sure everything is working

33 L1 #8 In the first column of the third row
Type Enter a number that is less than 18 or greater than 64 Drop a TextBox after the message Set the (ID) to txtOrTest In the middle column of the third row Drop a Button Set the (ID) to btnOrTest Set the Text to Test Number In the third column Drop a Label Set the (ID) to lblOrTest Clear the Text (NOTE: you would normally use Validators here to assure good data but we are omitting them for now to save time. Just remember that L3 assignments always require validation!)

34 L1 #9 Double-click the Test Number button in this row
Enter appropriate method level comments Convert the context of txtOrTest to an integer Test it to see if it meets the criteria and display a message In this example we have two disconnected values that are acceptable, either less than 18 OR greater than 64, so the test we need to use is the OR operator Verify - run the page and test to be sure you get the correct results

35 L1 #10 In the first column of the fourth row
Type Enter a number that is between 18 and 64 inclusive Drop a TextBox after the message Set the (ID) to txtAndTest In the middle column of the fourth row Drop a Button Set the (ID) to btnAndTest Set the Text to Test Number In the third column Drop a Label Set the (ID) to lblAndTest Clear the Text

36 L1 #11 Double-click the Test Number button in this row
Enter appropriate method level comments Convert the context of txtAndTest to an integer Test it to see if it meets the criteria and display a message In this example we have a continuous range of acceptable values so the test we need to use is the AND operator Run the page and test to be sure you get the correct results

37 L1 #12 Double check to be sure everything works
Open your MIS3200 home page and and a link to this L1 assignment Copy ASPPub back to your ASPNET account and submit your MIS Portfolio public URL to the Unit 3.1 L1 drop box.

38 L2 Open ASPPub Copy your xU3L1.aspx file, paste it back in the Unit3 directory and rename it yourLastNameU3L2.aspx In the first column of row 5 of the new file Drop a CheckBox Set the (ID) to ckbAge Set AutoPostBack to true Set the Text to 18 and over Skip the the right-most column Drop a Label Change (ID) to lblAge Clear the Text We will talk about this more during the next class, but the AutoPostBack property needs to be set to true if we want to process the CheckBox as soon as its value changes

39 L2 #2 Double-click the CheckBox
This method needs to see if the CheckBox is checked and output an appropriate message

40 L2 #3 In the first column of row 6 In the right-most column of row 6
Type Gender with Radio Buttons Drop a RadioButton after the text Set (ID) to rbMale Set Text to Male Set AutoPostBack to true and set the GroupName to gender Drop another RadioButton after the first one Set (ID) to rbFemale Set Text to Female In the right-most column of row 6 Drop a Label Set (ID) to lblGender1 Clear the Text

41 L2 #4 Double-click rbMale
Enter code to determine if the button is checked and display an appropriate message it if is Double-click rbFemale Run the page and test to be sure the new row works correctly (Notice any weird behavior? How could we address it? This is typically why we will use RadioButtonLists instead of RadioButtons)

42 L2 #5 In the left-most column of row seven
Type Gender with a Radio Button List Drop a RadioButtonList after the text Set (ID) to rblGender Set AutoPostBack to True Click the smart menu (see slide 20) and select Edit Items… Add an item with Text = Male and Value = Male and Add a 2nd item with Text = Female and Value = Female In the right-most column of row seven Drop a Label Set (ID) to lblGender2 Clear the Text

43 L2 #6 Double-click the RadioButtonList
Write code to see which button is selected and output an appropriate message Run the page and test to see that the new code works properly (did we really need to nest the 2nd if statement? In this case, no,

44 L2 #7 In the first cell of row eight Double-click the button
Drop a Button Set (ID) to btnClearEverything Set Text to Clear Everything Double-click the button Write code that clears the Text of all the Labels and TextBoxes Write code to set the Checked property of all CheckBoxes and RadioButtons to false (this unselects everything) Write code to set the SelectedIndex property of the RadioButtonList to -1 (-1 indicates that no value is selected in the list)

45 L2 #8 Test everything again Link this page to your MIS3200 homepage
Copy ASPPub back to your ASPNet account and submit your MIS Portfolio public URL to the Unit 3.1 L2 drop box

46 Think About It! What does an if statement allow us to do?
What is a nested if statement? What function is performed by these shortcut operators? += -= *= /= What about relational operators? == != > >= < <= How are increment/decrement operators used? ++ --


Download ppt "MIS 3200 – Unit 3 What can computers do"

Similar presentations


Ads by Google